Handler AniList rate-limit
This commit is contained in:
@@ -13,7 +13,8 @@
|
|||||||
const result = {user:{stats:null, genres:[]}, lists:Object.fromEntries(medias.map(type => [type, {}])), characters:[], sections}
|
const result = {user:{stats:null, genres:[]}, lists:Object.fromEntries(medias.map(type => [type, {}])), characters:[], sections}
|
||||||
|
|
||||||
//User statistics
|
//User statistics
|
||||||
{
|
for (let retried = false; !retried; retried = true) {
|
||||||
|
try {
|
||||||
//Query API
|
//Query API
|
||||||
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (user statistics)`)
|
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (user statistics)`)
|
||||||
const {data:{data:{User:{statistics:stats}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user}, query:queries.anilist.statistics()})
|
const {data:{data:{User:{statistics:stats}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user}, query:queries.anilist.statistics()})
|
||||||
@@ -21,10 +22,16 @@
|
|||||||
result.user.stats = stats
|
result.user.stats = stats
|
||||||
result.user.genres = [...new Set([...stats.anime.genres.map(({genre}) => genre), ...stats.manga.genres.map(({genre}) => genre)])]
|
result.user.genres = [...new Set([...stats.anime.genres.map(({genre}) => genre), ...stats.manga.genres.map(({genre}) => genre)])]
|
||||||
}
|
}
|
||||||
|
catch (error) {
|
||||||
|
await retry({login, imports, error})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Medias lists
|
//Medias lists
|
||||||
if ((sections.includes("watching"))||(sections.includes("reading"))) {
|
if ((sections.includes("watching"))||(sections.includes("reading"))) {
|
||||||
for (const type of medias) {
|
for (const type of medias) {
|
||||||
|
for (let retried = false; !retried; retried = true) {
|
||||||
|
try {
|
||||||
//Query API
|
//Query API
|
||||||
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (medias lists - ${type})`)
|
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (medias lists - ${type})`)
|
||||||
const {data:{data:{MediaListCollection:{lists}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, type:type.toLocaleUpperCase()}, query:queries.anilist.medias()})
|
const {data:{data:{MediaListCollection:{lists}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, type:type.toLocaleUpperCase()}, query:queries.anilist.medias()})
|
||||||
@@ -40,6 +47,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (error) {
|
||||||
|
await retry({login, imports, error})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Favorites anime/manga
|
//Favorites anime/manga
|
||||||
@@ -59,14 +71,9 @@
|
|||||||
list.push(...await Promise.all(nodes.map(media => format({media:{progess:null, score:null, media}, imports}))))
|
list.push(...await Promise.all(nodes.map(media => format({media:{progess:null, score:null, media}, imports}))))
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
if ((error.isAxiosError)&&(error.response.status === 429)) {
|
if (await retry({login, imports, error}))
|
||||||
const delay = Number(error.response.headers["retry-after"])+5
|
|
||||||
console.debug(`metrics/compute/${login}/plugins > anilist > reached requests limit, retrying in ${delay}s`)
|
|
||||||
await imports.wait(delay)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
throw error
|
|
||||||
}
|
|
||||||
} while (next)
|
} while (next)
|
||||||
//Format and save results
|
//Format and save results
|
||||||
result.lists[type].favorites = shuffle ? imports.shuffle(list) : list
|
result.lists[type].favorites = shuffle ? imports.shuffle(list) : list
|
||||||
@@ -97,14 +104,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
if ((error.isAxiosError)&&(error.response.status === 429)) {
|
if (await retry({login, imports, error}))
|
||||||
const delay = Number(error.response.headers["retry-after"])+5
|
|
||||||
console.debug(`metrics/compute/${login}/plugins > anilist > reached requests limit, retrying in ${delay}s`)
|
|
||||||
await imports.wait(delay)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
throw error
|
|
||||||
}
|
|
||||||
} while (next)
|
} while (next)
|
||||||
//Format and save results
|
//Format and save results
|
||||||
result.characters = shuffle ? imports.shuffle(characters) : characters
|
result.characters = shuffle ? imports.shuffle(characters) : characters
|
||||||
@@ -143,3 +145,14 @@
|
|||||||
artwork:artwork ? await imports.imgb64(artwork) : "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mOcOnfpfwAGfgLYttYINwAAAABJRU5ErkJggg==",
|
artwork:artwork ? await imports.imgb64(artwork) : "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mOcOnfpfwAGfgLYttYINwAAAABJRU5ErkJggg==",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**Rate-limiter handler */
|
||||||
|
async function retry({login, imports, error}) {
|
||||||
|
if ((error.isAxiosError)&&(error.response.status === 429)) {
|
||||||
|
const delay = Number(error.response.headers["retry-after"])+5
|
||||||
|
console.debug(`metrics/compute/${login}/plugins > anilist > reached requests limit, retrying in ${delay}s`)
|
||||||
|
await imports.wait(delay)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
throw error
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user