fix GetSteam250
All checks were successful
docker / prepare-and-build (push) Successful in 2m46s
release / goreleaser (push) Successful in 23m3s

This commit is contained in:
Nite07 2024-11-23 03:16:49 +08:00
parent d91ca35a83
commit 1fbe80f0b9
2 changed files with 53 additions and 1 deletions

View File

@ -113,6 +113,55 @@ func GetIGDBAppParent(id int) (int, error) {
return id, nil return id, nil
} }
func GetIGDBAppParetns(ids []int) (map[int]int, error) {
var err error
if TwitchToken == "" {
TwitchToken, err = LoginTwitch()
if err != nil {
return nil, err
}
}
idsStr := make([]string, len(ids))
for i, id := range ids {
idsStr[i] = strconv.Itoa(id)
}
resp, err := utils.Fetch(utils.FetchConfig{
Url: constant.IGDBGameURL,
Headers: map[string]string{
"Client-ID": config.Config.Twitch.ClientID,
"Authorization": "Bearer " + TwitchToken,
"User-Agent": "",
"Content-Type": "text/plain",
},
Data: fmt.Sprintf(`where id=(%s) ;fields version_parent;`, strings.Join(idsStr, ",")),
Method: "POST",
})
if err != nil {
return nil, err
}
var data []struct {
ID int `json:"id"`
VersionParent int `json:"version_parent"`
}
if err = json.Unmarshal(resp.Data, &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w, %s", err, debug.Stack())
}
parents := make(map[int]int)
for _, item := range data {
if item.VersionParent != 0 {
pid, err := GetIGDBAppParentCache(item.VersionParent)
if err != nil {
parents[item.ID] = item.ID
} else {
parents[item.ID] = pid
}
} else {
parents[item.ID] = item.ID
}
}
return parents, nil
}
func GetIGDBAppParentCache(id int) (int, error) { func GetIGDBAppParentCache(id int) (int, error) {
if config.Config.RedisAvaliable { if config.Config.RedisAvaliable {
key := fmt.Sprintf("igdb_parent:%d", id) key := fmt.Sprintf("igdb_parent:%d", id)

View File

@ -44,8 +44,11 @@ func GetSteam250(url string) ([]*model.GameInfo, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(infos) > 10 {
return infos[:10], nil return infos[:10], nil
} }
return infos, nil
}
func GetSteam250Top250() ([]*model.GameInfo, error) { func GetSteam250Top250() ([]*model.GameInfo, error) {
return GetSteam250(constant.Steam250Top250URL) return GetSteam250(constant.Steam250Top250URL)