From 3a076998f982cd321a946458a05f55f968c0d56f Mon Sep 17 00:00:00 2001 From: Seraphim Strub Date: Thu, 30 May 2024 22:01:13 +0000 Subject: [PATCH] add more image urls --- cmd/dealsbot/api/gog.go | 85 +++++++++++++++++++------------- cmd/dealsbot/api/gogfront.go | 45 ++++------------- cmd/dealsbot/api/humblebundle.go | 3 +- cmd/dealsbot/api/steam.go | 7 ++- cmd/dealsbot/api/ubisoft.go | 9 ++-- 5 files changed, 74 insertions(+), 75 deletions(-) diff --git a/cmd/dealsbot/api/gog.go b/cmd/dealsbot/api/gog.go index aeae9e6..93cde8f 100644 --- a/cmd/dealsbot/api/gog.go +++ b/cmd/dealsbot/api/gog.go @@ -6,7 +6,6 @@ import ( "log/slog" "net/http" "regexp" - "strings" ) type GogStruct struct { @@ -67,7 +66,7 @@ func (e GogStruct) Load() error { return case tt == html.StartTagToken: t := bodyStore.Token() - if t.Data != "a" { + if t.Data != "a" { // only tag continue } for _, a := range t.Attr { @@ -105,44 +104,60 @@ func (e GogStruct) Load() error { } 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") { - continue - } - if tt = bodyGame.Next(); tt != html.TextToken { - continue - } - id := fmt.Sprintf("%v%v", e.idPrefix, appID) - title := strings.TrimSpace(bodyGame.Token().Data) - url := fmt.Sprintf("%v%v", e.baseUrl, appID) - - e.deals[id] = Deal{ - Id: id, - Title: title, - Url: url, - } - } - } - } - }() + title, image := gogParseMeta(bodyGame) + url := fmt.Sprintf("%v%v", e.baseUrl, appID) + id := fmt.Sprintf("%v%v", e.idPrefix, appID) + e.deals[id] = Deal{ + Id: id, + Title: title, + Url: url, + Image: image, + } } return nil } +func gogParseMeta(htmlBody *html.Tokenizer) (string, string) { + var title, image string + func() { + for { + tt := htmlBody.Next() + + switch { + case tt == html.ErrorToken: + // file end or error + return + case tt == html.StartTagToken: + t := htmlBody.Token() + if t.Data != "meta" { + continue + } + for _, a := range t.Attr { + if a.Key == "property" && a.Val == "og:title" { + for _, c := range t.Attr { + if c.Key == "content" { + title = c.Val + } + } + } + if a.Key == "property" && a.Val == "og:image" { + for _, c := range t.Attr { + if c.Key == "content" { + image = c.Val + } + } + } + } + if title != "" && image != "" { + return + } + } + } + }() + return title, image +} + func (e GogStruct) Get() []Deal { var deals []Deal for _, deal := range e.deals { diff --git a/cmd/dealsbot/api/gogfront.go b/cmd/dealsbot/api/gogfront.go index 95324ae..8941dc1 100644 --- a/cmd/dealsbot/api/gogfront.go +++ b/cmd/dealsbot/api/gogfront.go @@ -6,7 +6,6 @@ import ( "log/slog" "net/http" "regexp" - "strings" ) type GogFrontStruct struct { @@ -67,7 +66,7 @@ func (e GogFrontStruct) Load() error { return case tt == html.StartTagToken: t := bodyStore.Token() - if t.Data != "a" { + if t.Data != "a" { // only tag continue } for _, a := range t.Attr { @@ -105,39 +104,15 @@ func (e GogFrontStruct) Load() error { } 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") { - continue - } - if tt = bodyGame.Next(); tt != html.TextToken { - continue - } - id := fmt.Sprintf("%v%v", e.idPrefix, appID) - title := strings.TrimSpace(bodyGame.Token().Data) - url := fmt.Sprintf("%v%v", e.baseUrl, appID) - - e.deals[id] = Deal{ - Id: id, - Title: title, - Url: url, - } - } - } - } - }() + title, image := gogParseMeta(bodyGame) + url := fmt.Sprintf("%v%v", e.baseUrl, appID) + id := fmt.Sprintf("%v%v", e.idPrefix, appID) + e.deals[id] = Deal{ + Id: id, + Title: title, + Url: url, + Image: image, + } } return nil diff --git a/cmd/dealsbot/api/humblebundle.go b/cmd/dealsbot/api/humblebundle.go index c721357..2cdda73 100644 --- a/cmd/dealsbot/api/humblebundle.go +++ b/cmd/dealsbot/api/humblebundle.go @@ -20,6 +20,7 @@ type HumbleBundleStruct struct { } 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/", @@ -80,7 +81,7 @@ func (e HumbleBundleStruct) Load() error { return nil case tt == html.StartTagToken: t := bodyStore.Token() - if t.Data != "script" { + if t.Data != "script" { // only