less deps

This commit is contained in:
matst80
2024-11-08 23:10:07 +01:00
parent 1b75f81119
commit 251a0b7bc7
3 changed files with 90 additions and 37 deletions

View File

@@ -4,11 +4,9 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"net/http"
"time" "time"
messages "git.tornberg.me/go-cart-actor/proto" messages "git.tornberg.me/go-cart-actor/proto"
"github.com/matst80/slask-finder/pkg/index"
) )
type CartId [16]byte type CartId [16]byte
@@ -58,19 +56,12 @@ func (c *CartGrain) GetLastChange() int64 {
} }
func getItemData(sku string) (*messages.AddItem, error) { 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 { if err != nil {
return nil, err return nil, err
} }
defer res.Body.Close() price := 0
var item index.DataItem priceField, ok := item.Fields[4]
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 { if ok {
priceFloat, ok := priceField.(float64) priceFloat, ok := priceField.(float64)
@@ -83,7 +74,10 @@ func getItemData(sku string) (*messages.AddItem, error) {
price = int(priceFloat) price = int(priceFloat)
} }
} }
if price == 0 {
return nil, fmt.Errorf("invalid price")
} }
return &messages.AddItem{ return &messages.AddItem{
Quantity: 1, Quantity: 1,
Price: int64(price), Price: int64(price),

17
go.mod
View File

@@ -5,22 +5,5 @@ go 1.23.0
toolchain go1.23.2 toolchain go1.23.2
require ( 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 google.golang.org/protobuf v1.34.2 // indirect
) )

76
product-fetcher.go Normal file
View File

@@ -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
}