mirror of
https://github.com/wahyd4/scraper.git
synced 2026-08-09 13:16:40 +10:00
61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
const (
|
|
oneMillion = 1000000
|
|
oneThousand = 1000
|
|
maxBid = ` max bi\`
|
|
)
|
|
|
|
func ToMoney(priceString string) int {
|
|
if !strings.HasPrefix(priceString, "$") {
|
|
return 0
|
|
}
|
|
priceWithoutDollar := priceString[1:]
|
|
|
|
priceWithoutUnit := priceWithoutDollar[:len(priceWithoutDollar)-1]
|
|
|
|
if strings.HasSuffix(priceWithoutDollar, maxBid) {
|
|
priceWithoutUnit = priceWithoutUnit[:len(priceWithoutUnit)-len(maxBid)]
|
|
}
|
|
fmt.Println(priceWithoutUnit)
|
|
floatPrice, err := strconv.ParseFloat(priceWithoutUnit, 10)
|
|
if err != nil {
|
|
log.Error().Msgf("cannot convert string to price data due to %v", err)
|
|
return 0
|
|
}
|
|
|
|
if strings.HasSuffix(priceWithoutUnit, "m") {
|
|
return int(floatPrice * oneMillion)
|
|
}
|
|
|
|
if strings.HasSuffix(priceWithoutUnit, "k") {
|
|
return int(floatPrice * oneThousand)
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
func ToBeds(bedsString string) int {
|
|
if strings.HasSuffix(bedsString, "beds") {
|
|
beds, err := strconv.Atoi(bedsString[:len(bedsString)-5])
|
|
if err != nil {
|
|
|
|
return 0
|
|
}
|
|
return beds
|
|
}
|
|
|
|
if strings.HasSuffix(bedsString, "bed") {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|