package api import ( "encoding/json" "fmt" "golang.org/x/net/html" "log/slog" "net/http" "regexp" "strings" ) type HumbleBundleStruct struct { url string baseUrl string idPrefix string headers map[string]string deals DealsMap logger *slog.Logger } func NewHumbleBundleApi(logger *slog.Logger) HumbleBundleStruct { // TODO: does this still work and if yes, get image humbleBundle := HumbleBundleStruct{ url: "https://www.humblebundle.com/", baseUrl: "https://www.humblebundle.com/store/", idPrefix: "humblebundle-", headers: make(map[string]string), deals: make(map[string]Deal), logger: logger, } humbleBundle.headers["Accept-Language"] = "en" humbleBundle.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0" return humbleBundle } type humblebundleJsonBody struct { Mosaic []struct { Products []struct { ProductUrl string `json:"product_url,omitempty"` Highlights []string `json:"highlights,omitempty"` TileName string `json:"tile_name,omitempty"` ProductTitle *string `json:"product_title,omitempty"` Type string `json:"type"` Category string `json:"category,omitempty"` EndDateDatetime string `json:"end_date|datetime,omitempty"` OperatingSystems []string `json:"operating_systems,omitempty"` Platforms []string `json:"platforms,omitempty"` } `json:"products"` } `json:"mosaic"` } func (e HumbleBundleStruct) Load() error { client := &http.Client{} // might have to add a cookie at a later time but currently works without // "Cookie", "gog_lc=GB_GBP_en-US" or "Accept-Language", "en" reqStore, err := http.NewRequest("GET", e.url, nil) if err != nil { return err } for key, value := range e.headers { reqStore.Header.Set(key, value) } resStore, err := client.Do(reqStore) if err != nil { return err } bodyStore := html.NewTokenizer(resStore.Body) var data humblebundleJsonBody err = func() error { for { tt := bodyStore.Next() switch { case tt == html.ErrorToken: // file end or error return nil case tt == html.StartTagToken: t := bodyStore.Token() if t.Data != "script" { // only