diff --git a/cart-grain.go b/cart-grain.go index 67b1814..817a97b 100644 --- a/cart-grain.go +++ b/cart-grain.go @@ -4,11 +4,9 @@ import ( "encoding/json" "fmt" "log" - "net/http" "time" messages "git.tornberg.me/go-cart-actor/proto" - "github.com/matst80/slask-finder/pkg/index" ) type CartId [16]byte @@ -58,32 +56,28 @@ func (c *CartGrain) GetLastChange() int64 { } func getItemData(sku string) (*messages.AddItem, error) { - res, err := http.Get("https://slask-finder.tornberg.me/api/get/" + sku) + item, err := FetchItem(sku) if err != nil { return nil, err } - defer res.Body.Close() - var item index.DataItem - err = json.NewDecoder(res.Body).Decode(&item) - if err != nil { - return nil, err - } - price := item.GetPrice() - if price == 0 { - priceField, ok := item.GetFields()[4] - if ok { + price := 0 + priceField, ok := item.Fields[4] + if ok { - priceFloat, ok := priceField.(float64) + priceFloat, ok := priceField.(float64) + if !ok { + price, ok = priceField.(int) if !ok { - price, ok = priceField.(int) - if !ok { - return nil, fmt.Errorf("invalid price type") - } - } else { - price = int(priceFloat) + return nil, fmt.Errorf("invalid price type") } + } else { + price = int(priceFloat) } } + if price == 0 { + return nil, fmt.Errorf("invalid price") + } + return &messages.AddItem{ Quantity: 1, Price: int64(price), diff --git a/go.mod b/go.mod index 61be3ee..441defa 100644 --- a/go.mod +++ b/go.mod @@ -5,22 +5,5 @@ go 1.23.0 toolchain go1.23.2 require ( - cloud.google.com/go/compute/metadata v0.3.0 // indirect - github.com/beorn7/perks v1.0.1 // indirect - github.com/cespare/xxhash/v2 v2.3.0 // indirect - github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect - github.com/golang-jwt/jwt v3.2.2+incompatible // indirect - github.com/gorilla/schema v1.4.1 // indirect - github.com/klauspost/compress v1.17.9 // indirect - github.com/matst80/slask-finder v0.0.0-20241104074525-3365cb1531ac // indirect - github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/prometheus/client_golang v1.20.4 // indirect - github.com/prometheus/client_model v0.6.1 // indirect - github.com/prometheus/common v0.55.0 // indirect - github.com/prometheus/procfs v0.15.1 // indirect - github.com/rabbitmq/amqp091-go v1.10.0 // indirect - github.com/redis/go-redis/v9 v9.5.3 // indirect - golang.org/x/oauth2 v0.23.0 // indirect - golang.org/x/sys v0.22.0 // indirect google.golang.org/protobuf v1.34.2 // indirect ) diff --git a/product-fetcher.go b/product-fetcher.go new file mode 100644 index 0000000..bab199b --- /dev/null +++ b/product-fetcher.go @@ -0,0 +1,76 @@ +package main + +import ( + "encoding/json" + "net/http" +) + +type EnergyRating struct { + Value string `json:"value,omitempty"` + Min string `json:"min,omitempty"` + Max string `json:"max,omitempty"` +} + +type PriceTuple struct { + IncVat int `json:"inc"` + ExVat int `json:"exl"` +} + +type OutletItem struct { + ArticleNumber string `json:"sku,opmitempty"` + Price PriceTuple `json:"price,omitempty"` + Title string `json:"title"` +} + +type ItemProp struct { + Url string `json:"url"` + Disclaimer string `json:"disclaimer,omitempty"` + ReleaseDate string `json:"releaseDate,omitempty"` + SaleStatus string `json:"saleStatus"` + MarginPercent float64 `json:"mp,omitempty"` + PresaleDate string `json:"presaleDate,omitempty"` + Restock string `json:"restock,omitempty"` + AdvertisingText string `json:"advertisingText,omitempty"` + Img string `json:"img,omitempty"` + BadgeUrl string `json:"badgeUrl,omitempty"` + EnergyRating *EnergyRating `json:"energyRating,omitempty"` + BulletPoints string `json:"bp,omitempty"` + LastUpdate int64 `json:"lastUpdate,omitempty"` + Created int64 `json:"created,omitempty"` + Buyable bool `json:"buyable"` + Description string `json:"description,omitempty"` + BuyableInStore bool `json:"buyableInStore"` + BoxSize string `json:"boxSize,omitempty"` + CheapestBItem *OutletItem `json:"bItem,omitempty"` + AItem *OutletItem `json:"aItem,omitempty"` +} + +type BaseItem struct { + ItemProp + StockLevel string `json:"stockLevel,omitempty"` + Stock LocationStock `json:"stock"` + Id uint `json:"id"` + Sku string `json:"sku"` + Title string `json:"title"` +} + +type DataItem struct { + *BaseItem + Fields map[uint]interface{} `json:"values"` +} + +type LocationStock []struct { + Id string `json:"id"` + Level string `json:"level"` +} + +func FetchItem(sku string) (*DataItem, error) { + res, err := http.Get("https://slask-finder.tornberg.me/api/get/" + sku) + if err != nil { + return nil, err + } + defer res.Body.Close() + var item DataItem + err = json.NewDecoder(res.Body).Decode(&item) + return &item, err +}