mirror of
https://github.com/wahyd4/scraper.git
synced 2026-08-09 05:06:22 +10:00
88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
const (
|
|
oneMillion = 1000000
|
|
oneThousand = 1000
|
|
maxBid = ` max bid`
|
|
priceWithhold = "Price withheld"
|
|
dollarString = "$"
|
|
percentCharacter = "%"
|
|
)
|
|
|
|
func ToMoney(priceString string) int {
|
|
if strings.Contains(priceString, priceWithhold) {
|
|
return -1
|
|
}
|
|
|
|
if !strings.HasPrefix(priceString, dollarString) {
|
|
return 0
|
|
}
|
|
|
|
//remove $
|
|
priceWithoutDollar := strings.TrimPrefix(priceString, dollarString)
|
|
|
|
// remove max bid
|
|
priceWithoutBid := strings.TrimSuffix(priceWithoutDollar, maxBid)
|
|
|
|
var purePriceString string
|
|
if strings.HasSuffix(priceWithoutBid, "m") || strings.HasSuffix(priceWithoutBid, "k") {
|
|
purePriceString = priceWithoutBid[:len(priceWithoutBid)-1]
|
|
} else {
|
|
purePriceString = strings.ReplaceAll(priceWithoutBid, ",", "")
|
|
}
|
|
|
|
floatPrice, err := strconv.ParseFloat(purePriceString, 10)
|
|
|
|
if err != nil {
|
|
log.Error().Msgf("cannot convert string to price data due to %v", err)
|
|
return 0
|
|
}
|
|
|
|
if strings.HasSuffix(priceWithoutBid, "m") {
|
|
return int(math.Round(floatPrice * oneMillion))
|
|
}
|
|
|
|
if strings.HasSuffix(priceWithoutBid, "k") {
|
|
return int(math.Round(floatPrice * oneThousand))
|
|
}
|
|
|
|
return int(floatPrice)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func PercentStringToFloat(percent string) float64 {
|
|
|
|
percentWithoutPercent := strings.TrimSuffix(percent, percentCharacter)
|
|
|
|
percentFloat, err := strconv.ParseFloat(percentWithoutPercent, 10)
|
|
|
|
if err != nil {
|
|
log.Error().Msgf("cannot cask %s to float with error %v", percent, err)
|
|
return 0
|
|
}
|
|
|
|
return percentFloat
|
|
}
|