From e0a719ee93c7f932f760e429a3d3997dbf781aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Thu, 16 Nov 2023 11:41:52 -0600 Subject: [PATCH] fix: patient JSON parsing --- .vscode/settings.json | 1 + scripts/buildRatingsLeaderboard.ts | 21 ++++++++------------- src/core/blitzkrieg/patientFetchJSON.ts | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 src/core/blitzkrieg/patientFetchJSON.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index b89dc3f62..543f4b3be 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,7 @@ "Everytime", "fullstats", "fuzzysort", + "immer", "kranvagn", "Leaderboard", "leaderboards", diff --git a/scripts/buildRatingsLeaderboard.ts b/scripts/buildRatingsLeaderboard.ts index a2b2fe43f..564bffb66 100644 --- a/scripts/buildRatingsLeaderboard.ts +++ b/scripts/buildRatingsLeaderboard.ts @@ -6,7 +6,7 @@ import { RatingsNeighbors, RatingsPlayer, } from '../src/commands/ratings'; -import { patientFetch } from '../src/core/blitzkrieg/patientFetch'; +import { patientFetchJSON } from '../src/core/blitzkrieg/patientFetchJSON'; import commitMultipleFiles from './commitMultipleFiles.js'; /* @@ -29,11 +29,9 @@ const server = argv.find((arg) => arg.startsWith('--region='))?.split('=')[1]; if (!server) throw new Error('Region parameter not specified'); const NEIGHBORS = 1024; -const PLAYERS = await patientFetch( +const PLAYERS = await patientFetchJSON( `https://${server}.wotblitz.com/en/api/rating-leaderboards/season/`, -) - .then((response) => response.json() as Promise) - .then((data) => (data.detail === undefined ? data.count : undefined)); +).then((data) => (data.detail === undefined ? data.count : undefined)); if (PLAYERS === undefined) { throw new Error('No current ratings season running'); @@ -44,9 +42,9 @@ const leaderboard: BlitzkriegRatingsLeaderboard = []; let registered = 0; async function branchFromPlayer(id: number) { - const { neighbors } = await patientFetch( + const { neighbors } = await patientFetchJSON( `https://${server}.wotblitz.com/en/api/rating-leaderboards/user/${id}/?neighbors=${NEIGHBORS}`, - ).then((response) => response.json() as Promise); + ); const firstPlayer = neighbors[0]; const lastPlayer = neighbors[neighbors.length - 1]; @@ -75,9 +73,9 @@ async function branchFromPlayer(id: number) { } async function branchFromLeague(id: number) { - const { result } = await patientFetch( + const { result } = await patientFetchJSON<{ result: RatingsPlayer[] }>( `https://${server}.wotblitz.com/en/api/rating-leaderboards/league/${id}/top/`, - ).then((response) => response.json() as Promise<{ result: RatingsPlayer[] }>); + ); const firstPlayer = result[0]; const lastPlayer = result[result.length - 1]; @@ -113,11 +111,8 @@ const leaderboardJSON = JSON.stringify(leaderboard); if (publish) { const octokit = new Octokit({ auth: env.GH_TOKEN }); - const info = await patientFetch( + const info = await patientFetchJSON( `https://${server}.wotblitz.com/en/api/rating-leaderboards/season/`, - ).then( - (response) => - response.json() as Promise, ); const normalizedServer = server === 'na' ? 'com' : server; diff --git a/src/core/blitzkrieg/patientFetchJSON.ts b/src/core/blitzkrieg/patientFetchJSON.ts new file mode 100644 index 000000000..c96b4dc83 --- /dev/null +++ b/src/core/blitzkrieg/patientFetchJSON.ts @@ -0,0 +1,23 @@ +export async function patientFetchJSON( + url: string, + timeout = 1000, + options?: Partial<{ cache: 'no-store' }>, +) { + let patientJSON: Type; + + while (patientJSON! === undefined) { + try { + patientJSON = (await fetch(url, options).then(({ json }) => + json(), + )) as Type; + } catch (error) { + console.warn( + `Failed to fetch ${url} as JSON; retrying in ${timeout}ms...`, + error, + ); + await new Promise((resolve) => setTimeout(resolve, timeout)); + } + } + + return patientJSON; +}