add more image urls

This commit is contained in:
Seraphim Strub 2024-05-30 22:01:13 +00:00
parent 0b97f282e2
commit 3a076998f9
5 changed files with 74 additions and 75 deletions

View file

@ -6,7 +6,6 @@ import (
"log/slog" "log/slog"
"net/http" "net/http"
"regexp" "regexp"
"strings"
) )
type GogStruct struct { type GogStruct struct {
@ -67,7 +66,7 @@ func (e GogStruct) Load() error {
return return
case tt == html.StartTagToken: case tt == html.StartTagToken:
t := bodyStore.Token() t := bodyStore.Token()
if t.Data != "a" { if t.Data != "a" { // only <a> tag
continue continue
} }
for _, a := range t.Attr { for _, a := range t.Attr {
@ -105,42 +104,58 @@ func (e GogStruct) Load() error {
} }
bodyGame := html.NewTokenizer(resGame.Body) bodyGame := html.NewTokenizer(resGame.Body)
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() { func() {
for { for {
tt := bodyGame.Next() tt := htmlBody.Next()
switch { switch {
case tt == html.ErrorToken: case tt == html.ErrorToken:
// file end or error // file end or error
return return
case tt == html.StartTagToken: case tt == html.StartTagToken:
t := bodyGame.Token() t := htmlBody.Token()
if t.Data != "h1" { if t.Data != "meta" {
continue continue
} }
for _, a := range t.Attr { for _, a := range t.Attr {
if !(a.Key == "class" && a.Val == "productcard-basics__title") { if a.Key == "property" && a.Val == "og:title" {
continue for _, c := range t.Attr {
if c.Key == "content" {
title = c.Val
} }
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,
} }
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
return nil
} }
func (e GogStruct) Get() []Deal { func (e GogStruct) Get() []Deal {

View file

@ -6,7 +6,6 @@ import (
"log/slog" "log/slog"
"net/http" "net/http"
"regexp" "regexp"
"strings"
) )
type GogFrontStruct struct { type GogFrontStruct struct {
@ -67,7 +66,7 @@ func (e GogFrontStruct) Load() error {
return return
case tt == html.StartTagToken: case tt == html.StartTagToken:
t := bodyStore.Token() t := bodyStore.Token()
if t.Data != "a" { if t.Data != "a" { // only <a> tag
continue continue
} }
for _, a := range t.Attr { for _, a := range t.Attr {
@ -105,40 +104,16 @@ func (e GogFrontStruct) Load() error {
} }
bodyGame := html.NewTokenizer(resGame.Body) bodyGame := html.NewTokenizer(resGame.Body)
func() { title, image := gogParseMeta(bodyGame)
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) url := fmt.Sprintf("%v%v", e.baseUrl, appID)
id := fmt.Sprintf("%v%v", e.idPrefix, appID)
e.deals[id] = Deal{ e.deals[id] = Deal{
Id: id, Id: id,
Title: title, Title: title,
Url: url, Url: url,
Image: image,
} }
} }
}
}
}()
}
return nil return nil
} }

View file

@ -20,6 +20,7 @@ type HumbleBundleStruct struct {
} }
func NewHumbleBundleApi(logger *slog.Logger) HumbleBundleStruct { func NewHumbleBundleApi(logger *slog.Logger) HumbleBundleStruct {
// TODO: does this still work and if yes, get image
humbleBundle := HumbleBundleStruct{ humbleBundle := HumbleBundleStruct{
url: "https://www.humblebundle.com/", url: "https://www.humblebundle.com/",
baseUrl: "https://www.humblebundle.com/store/", baseUrl: "https://www.humblebundle.com/store/",
@ -80,7 +81,7 @@ func (e HumbleBundleStruct) Load() error {
return nil return nil
case tt == html.StartTagToken: case tt == html.StartTagToken:
t := bodyStore.Token() t := bodyStore.Token()
if t.Data != "script" { if t.Data != "script" { // only <script> tag
continue continue
} }
for _, a := range t.Attr { for _, a := range t.Attr {

View file

@ -42,6 +42,7 @@ type steamApiBodyGame struct {
Type string `json:"type"` Type string `json:"type"`
Name string `json:"name"` Name string `json:"name"`
IsFree bool `json:"is_free"` IsFree bool `json:"is_free"`
HeaderImage string `json:"header_image"`
PriceOverview struct { PriceOverview struct {
Currency string `json:"currency"` Currency string `json:"currency"`
Initial int `json:"initial"` Initial int `json:"initial"`
@ -95,7 +96,7 @@ func (e SteamStruct) Load() error {
return return
case tt == html.StartTagToken: case tt == html.StartTagToken:
t := bodyStore.Token() t := bodyStore.Token()
if t.Data != "a" { if t.Data != "a" { // only <a> tag
continue continue
} }
for _, a := range t.Attr { for _, a := range t.Attr {
@ -139,6 +140,9 @@ func (e SteamStruct) Load() error {
if game.Data.Type != "game" { if game.Data.Type != "game" {
continue continue
} }
image := game.Data.HeaderImage
id := fmt.Sprintf("%v%v", e.idPrefix, appID) id := fmt.Sprintf("%v%v", e.idPrefix, appID)
title := strings.TrimSpace(game.Data.Name) title := strings.TrimSpace(game.Data.Name)
url := fmt.Sprintf("%v%v", e.baseUrl, appID) url := fmt.Sprintf("%v%v", e.baseUrl, appID)
@ -147,6 +151,7 @@ func (e SteamStruct) Load() error {
Id: id, Id: id,
Title: title, Title: title,
Url: url, Url: url,
Image: image,
} }
} }
} }

View file

@ -39,6 +39,7 @@ type ubisoftApiBody struct {
News []struct { News []struct {
Type string `json:"type"` Type string `json:"type"`
Title string `json:"title"` Title string `json:"title"`
MediaUrl string `json:"mediaURL"`
Links []struct { Links []struct {
Param string `json:"param"` Param string `json:"param"`
} `json:"links"` } `json:"links"`
@ -128,11 +129,13 @@ func (e UbisoftStruct) Load() error {
id := fmt.Sprintf("%v%v", e.idPrefix, idFromUrl[1]) id := fmt.Sprintf("%v%v", e.idPrefix, idFromUrl[1])
url := news.Links[0].Param url := news.Links[0].Param
image := news.MediaUrl
e.deals[id] = Deal{ e.deals[id] = Deal{
Id: id, Id: id,
Title: title, Title: title,
Url: url, Url: url,
Image: image,
} }
} }