diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2025-07-24 14:40:22 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2025-07-24 14:40:22 +0300 |
| commit | b0d199936b546c75d4b19d99591237f0bf97fe55 (patch) | |
| tree | e9391880e7db2bd1ea8ff25d91aeea8dd98f186e /rankings/fetch.go | |
| parent | fix/frontend: fixed sidebar title size, removed unnecessary imports (diff) | |
| parent | feat/backend: add newrelic integration (#274) (diff) | |
| download | lphub-css-overhaul.tar.gz lphub-css-overhaul.tar.bz2 lphub-css-overhaul.zip | |
Merge branch 'main' into css-overhaulcss-overhaul
Diffstat (limited to 'rankings/fetch.go')
| -rw-r--r-- | rankings/fetch.go | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/rankings/fetch.go b/rankings/fetch.go index 7e63427..7e9a449 100644 --- a/rankings/fetch.go +++ b/rankings/fetch.go | |||
| @@ -3,6 +3,7 @@ package main | |||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | "encoding/xml" | 5 | "encoding/xml" |
| 6 | "errors" | ||
| 6 | "fmt" | 7 | "fmt" |
| 7 | "io" | 8 | "io" |
| 8 | "log" | 9 | "log" |
| @@ -12,7 +13,12 @@ import ( | |||
| 12 | "strings" | 13 | "strings" |
| 13 | ) | 14 | ) |
| 14 | 15 | ||
| 15 | func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, useCache bool) map[SteamID]*Player { | 16 | var ( |
| 17 | errLb error = errors.New("leaderboards error") | ||
| 18 | errPi error = errors.New("playerinfo error") | ||
| 19 | ) | ||
| 20 | |||
| 21 | func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, useCache bool) (map[SteamID]*Player, error) { | ||
| 16 | log.Println("fetching leaderboard") | 22 | log.Println("fetching leaderboard") |
| 17 | players := map[SteamID]*Player{} | 23 | players := map[SteamID]*Player{} |
| 18 | // first init players map with records from portal gun and doors | 24 | // first init players map with records from portal gun and doors |
| @@ -21,7 +27,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us | |||
| 21 | end := 5000 | 27 | end := 5000 |
| 22 | 28 | ||
| 23 | for fetchAnotherPage { | 29 | for fetchAnotherPage { |
| 24 | portalGunEntries := fetchRecordsFromMap(47459, 0, 5000, useCache) | 30 | portalGunEntries, err := fetchRecordsFromMap(47459, 0, 5000, useCache) |
| 31 | if err != nil { | ||
| 32 | return nil, err | ||
| 33 | } | ||
| 25 | fetchAnotherPage = portalGunEntries.needsAnotherPage(&records[0]) | 34 | fetchAnotherPage = portalGunEntries.needsAnotherPage(&records[0]) |
| 26 | if fetchAnotherPage { | 35 | if fetchAnotherPage { |
| 27 | start = end + 1 | 36 | start = end + 1 |
| @@ -50,7 +59,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us | |||
| 50 | end = 5000 | 59 | end = 5000 |
| 51 | 60 | ||
| 52 | for fetchAnotherPage { | 61 | for fetchAnotherPage { |
| 53 | doorsEntries := fetchRecordsFromMap(47740, start, end, useCache) | 62 | doorsEntries, err := fetchRecordsFromMap(47740, start, end, useCache) |
| 63 | if err != nil { | ||
| 64 | return nil, err | ||
| 65 | } | ||
| 54 | fetchAnotherPage = doorsEntries.needsAnotherPage(&records[51]) | 66 | fetchAnotherPage = doorsEntries.needsAnotherPage(&records[51]) |
| 55 | if fetchAnotherPage { | 67 | if fetchAnotherPage { |
| 56 | start = end + 1 | 68 | start = end + 1 |
| @@ -94,7 +106,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us | |||
| 94 | end := 5000 | 106 | end := 5000 |
| 95 | 107 | ||
| 96 | for fetchAnotherPage { | 108 | for fetchAnotherPage { |
| 97 | entries := fetchRecordsFromMap(record.MapID, start, end, useCache) | 109 | entries, err := fetchRecordsFromMap(record.MapID, start, end, useCache) |
| 110 | if err != nil { | ||
| 111 | return nil, err | ||
| 112 | } | ||
| 98 | fetchAnotherPage = entries.needsAnotherPage(&record) | 113 | fetchAnotherPage = entries.needsAnotherPage(&record) |
| 99 | if fetchAnotherPage { | 114 | if fetchAnotherPage { |
| 100 | start = end + 1 | 115 | start = end + 1 |
| @@ -137,10 +152,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us | |||
| 137 | } | 152 | } |
| 138 | 153 | ||
| 139 | } | 154 | } |
| 140 | return players | 155 | return players, nil |
| 141 | } | 156 | } |
| 142 | 157 | ||
| 143 | func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderboard { | 158 | func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) (*Leaderboard, error) { |
| 144 | var filename string | 159 | var filename string |
| 145 | if useCache { | 160 | if useCache { |
| 146 | filename := fmt.Sprintf("./cache/lb_%d_%d_%d.xml", mapID, start, end) | 161 | filename := fmt.Sprintf("./cache/lb_%d_%d_%d.xml", mapID, start, end) |
| @@ -152,25 +167,27 @@ func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderbo | |||
| 152 | if err != nil { | 167 | if err != nil { |
| 153 | log.Fatalln("failed to unmarshal cache.", err.Error()) | 168 | log.Fatalln("failed to unmarshal cache.", err.Error()) |
| 154 | } | 169 | } |
| 155 | return &leaderboard | 170 | return &leaderboard, nil |
| 156 | } | 171 | } |
| 157 | } | 172 | } |
| 158 | 173 | ||
| 159 | url := fmt.Sprintf("https://steamcommunity.com/stats/Portal2/leaderboards/%d?xml=1&start=%d&end=%d", mapID, start, end) | 174 | url := fmt.Sprintf("https://steamcommunity.com/stats/Portal2/leaderboards/%d?xml=1&start=%d&end=%d", mapID, start, end) |
| 160 | resp, err := http.Get(url) | 175 | resp, err := http.Get(url) |
| 161 | log.Println("fetched", url, ":", resp.StatusCode) | ||
| 162 | if err != nil { | 176 | if err != nil { |
| 163 | log.Fatalln("failed to fetch leaderboard.", err.Error()) | 177 | log.Println("failed to fetch leaderboard.", err.Error()) |
| 178 | return nil, errLb | ||
| 164 | } | 179 | } |
| 165 | respBytes, err := io.ReadAll(resp.Body) | 180 | respBytes, err := io.ReadAll(resp.Body) |
| 166 | if err != nil { | 181 | if err != nil { |
| 167 | log.Fatalln("failed to read leadeboard body.", err.Error()) | 182 | log.Println("failed to read leadeboard body.", err.Error()) |
| 183 | return nil, errLb | ||
| 168 | } | 184 | } |
| 169 | leaderboard := Leaderboard{} | 185 | leaderboard := Leaderboard{} |
| 170 | err = xml.Unmarshal(respBytes, &leaderboard) | 186 | err = xml.Unmarshal(respBytes, &leaderboard) |
| 171 | if err != nil { | 187 | if err != nil { |
| 172 | log.Println(string(respBytes)) | 188 | log.Println(string(respBytes)) |
| 173 | log.Fatalln("failed to unmarshal leaderboard.", err.Error()) | 189 | log.Println("failed to unmarshal leaderboard.", err.Error()) |
| 190 | return nil, errLb | ||
| 174 | } | 191 | } |
| 175 | 192 | ||
| 176 | if useCache { | 193 | if useCache { |
| @@ -179,10 +196,10 @@ func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderbo | |||
| 179 | } | 196 | } |
| 180 | } | 197 | } |
| 181 | 198 | ||
| 182 | return &leaderboard | 199 | return &leaderboard, nil |
| 183 | } | 200 | } |
| 184 | 201 | ||
| 185 | func fetchPlayerInfo(players []*Player) { | 202 | func fetchPlayerInfo(players []*Player) error { |
| 186 | log.Println("fetching info for", len(players), "players") | 203 | log.Println("fetching info for", len(players), "players") |
| 187 | 204 | ||
| 188 | ids := make([]string, len(players)) | 205 | ids := make([]string, len(players)) |
| @@ -193,11 +210,13 @@ func fetchPlayerInfo(players []*Player) { | |||
| 193 | url := fmt.Sprintf("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=%s&steamids=%s", os.Getenv("API_KEY"), strings.Join(ids, ",")) | 210 | url := fmt.Sprintf("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=%s&steamids=%s", os.Getenv("API_KEY"), strings.Join(ids, ",")) |
| 194 | resp, err := http.Get(url) | 211 | resp, err := http.Get(url) |
| 195 | if err != nil { | 212 | if err != nil { |
| 196 | log.Fatalln(err.Error()) | 213 | log.Println(err.Error()) |
| 214 | return errPi | ||
| 197 | } | 215 | } |
| 198 | body, err := io.ReadAll(resp.Body) | 216 | body, err := io.ReadAll(resp.Body) |
| 199 | if err != nil { | 217 | if err != nil { |
| 200 | log.Fatalln(err.Error()) | 218 | log.Println(err.Error()) |
| 219 | return errPi | ||
| 201 | } | 220 | } |
| 202 | type PlayerSummary struct { | 221 | type PlayerSummary struct { |
| 203 | SteamID SteamID `json:"steamid"` | 222 | SteamID SteamID `json:"steamid"` |
| @@ -223,4 +242,5 @@ func fetchPlayerInfo(players []*Player) { | |||
| 223 | } | 242 | } |
| 224 | } | 243 | } |
| 225 | } | 244 | } |
| 245 | return nil | ||
| 226 | } | 246 | } |