package main import ( "fmt" "golang.org/x/net/html" "net/http" "regexp" ) type GogStruct struct { url string baseUrl string idPrefix string deals DealsMap } func newGogApi() GogStruct { return GogStruct{ url: "https://www.gog.com/en", baseUrl: "https://www.gog.com/en/game/", idPrefix: "gog-", deals: make(map[string]Deal), } } func (e GogStruct) 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 } resStore, err := client.Do(reqStore) if err != nil { return err } bodyStore := html.NewTokenizer(resStore.Body) regexAppid, err := regexp.Compile(`/en/game/([-\w]+)`) if err != nil { return err } var appIDs []string func() { for { tt := bodyStore.Next() switch { case tt == html.ErrorToken: // file end or error return case tt == html.StartTagToken: t := bodyStore.Token() if t.Data != "a" { continue } for _, a := range t.Attr { if !(a.Key == "id" && a.Val == "giveaway") { continue } for _, attr := range t.Attr { if attr.Key != "ng-href" { continue } appID := regexAppid.FindStringSubmatch(attr.Val) if len(appID) < 1 { continue } appIDs = append(appIDs, appID[1]) } } } } }() for _, appID := range appIDs { reqGame, err := http.NewRequest("GET", fmt.Sprintf("%v%v", e.baseUrl, appID), nil) if err != nil { return err } resGame, err := client.Do(reqGame) if err != nil { return err } bodyGame := html.NewTokenizer(resGame.Body) func() { for { tt := bodyGame.Next() switch { case tt == html.ErrorToken: // file end or error return case tt == html.StartTagToken: t := bodyGame.Token() if t.Data != "h1" { continue } for _, a := range t.Attr { if !(a.Key == "class" && a.Val == "productcard-basics__title") { } if tt = bodyGame.Next(); tt != html.TextToken { continue } id := fmt.Sprintf("%v%v", e.idPrefix, appID) title := bodyGame.Token().Data url := fmt.Sprintf("%v%v", e.baseUrl, appID) e.deals[id] = Deal{ Id: id, Title: title, Url: url, } } } } }() } return nil } func (e GogStruct) get() []Deal { var deals []Deal for _, deal := range e.deals { deals = append(deals, deal) } return deals }