mirror of
https://github.com/wahyd4/scraper.git
synced 2026-08-09 05:06:22 +10:00
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
const (
|
|
oneMillion = 1000000
|
|
oneThousand = 1000
|
|
maxBid = ` max bid`
|
|
priceWithhold = "Price withheld"
|
|
)
|
|
|
|
func ToMoney(priceString string) int {
|
|
if strings.Contains(priceString, priceWithhold) {
|
|
return -1
|
|
}
|
|
|
|
if !strings.HasPrefix(priceString, "$") {
|
|
return 0
|
|
}
|
|
//remove $
|
|
priceWithoutDollar := priceString[1:]
|
|
|
|
priceWithoutDollar = strings.TrimSuffix(priceWithoutDollar, maxBid)
|
|
|
|
priceWithoutUnit := priceWithoutDollar[:len(priceWithoutDollar)-1]
|
|
|
|
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(priceWithoutDollar, "m") {
|
|
return int(math.Round(floatPrice * oneMillion))
|
|
}
|
|
|
|
if strings.HasSuffix(priceWithoutDollar, "k") {
|
|
return int(math.Round(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
|
|
}
|