From 1fbe80f0b95d6c33990877026a16cebed9608aee Mon Sep 17 00:00:00 2001 From: nite07 Date: Sat, 23 Nov 2024 03:16:49 +0800 Subject: [PATCH] fix GetSteam250 --- crawler/igdb.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ crawler/steam250.go | 5 ++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/crawler/igdb.go b/crawler/igdb.go index a5d0888..e41f568 100644 --- a/crawler/igdb.go +++ b/crawler/igdb.go @@ -113,6 +113,55 @@ func GetIGDBAppParent(id int) (int, error) { 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) { if config.Config.RedisAvaliable { key := fmt.Sprintf("igdb_parent:%d", id) diff --git a/crawler/steam250.go b/crawler/steam250.go index b52501d..a81fd98 100644 --- a/crawler/steam250.go +++ b/crawler/steam250.go @@ -44,7 +44,10 @@ func GetSteam250(url string) ([]*model.GameInfo, error) { if err != nil { return nil, err } - return infos[:10], nil + if len(infos) > 10 { + return infos[:10], nil + } + return infos, nil } func GetSteam250Top250() ([]*model.GameInfo, error) {