Files
scraper/utils/utils.go
T
2020-03-03 17:15:16 +11:00

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
}