discordBots/cmd/dealsbot/api/gog.go

149 lines
3 KiB
Go
Raw Normal View History

2023-03-06 19:53:07 +00:00
package api
2023-03-04 11:54:08 +00:00
import (
"fmt"
"golang.org/x/net/html"
"net/http"
"regexp"
)
type GogStruct struct {
url string
baseUrl string
idPrefix string
2023-03-06 18:02:40 +00:00
headers map[string]string
2023-03-04 11:54:08 +00:00
deals DealsMap
}
2023-03-06 19:53:07 +00:00
func NewGogApi() GogStruct {
2023-03-06 18:02:40 +00:00
gog := GogStruct{
2023-07-17 12:49:35 +00:00
url: "https://www.gog.com/en/games?priceRange=0,0&discounted=true",
2023-03-06 18:02:40 +00:00
baseUrl: "https://www.gog.com/game/",
2023-03-04 11:54:08 +00:00
idPrefix: "gog-",
2023-03-06 18:02:40 +00:00
headers: make(map[string]string),
2023-03-04 11:54:08 +00:00
deals: make(map[string]Deal),
}
2023-03-06 18:02:40 +00:00
gog.headers["Accept-Language"] = "en"
gog.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0"
return gog
2023-03-04 11:54:08 +00:00
}
2023-03-06 19:53:07 +00:00
func (e GogStruct) Load() error {
2023-03-04 11:54:08 +00:00
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
}
2023-03-06 18:02:40 +00:00
for key, value := range e.headers {
reqStore.Header.Set(key, value)
}
2023-03-04 11:54:08 +00:00
resStore, err := client.Do(reqStore)
if err != nil {
return err
}
bodyStore := html.NewTokenizer(resStore.Body)
2023-03-06 18:02:40 +00:00
regexAppid, err := regexp.Compile(`/\w{2}/game/([-\w]+)`)
2023-03-04 11:54:08 +00:00
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 {
2023-07-17 12:49:35 +00:00
if !(a.Key == "class" && a.Val == "product-tile product-tile--grid") {
2023-03-04 11:54:08 +00:00
continue
}
for _, attr := range t.Attr {
2023-07-17 12:49:35 +00:00
if attr.Key != "href" {
2023-03-04 11:54:08 +00:00
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
}
2023-03-06 18:02:40 +00:00
for key, value := range e.headers {
reqGame.Header.Set(key, value)
}
2023-03-04 11:54:08 +00:00
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") {
2023-07-17 12:49:35 +00:00
continue
2023-03-04 11:54:08 +00:00
}
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
}
2023-03-06 19:53:07 +00:00
func (e GogStruct) Get() []Deal {
2023-03-04 11:54:08 +00:00
var deals []Deal
for _, deal := range e.deals {
deals = append(deals, deal)
}
return deals
}