Add handlers for AniList rate limit (#92)
This commit is contained in:
@@ -131,3 +131,8 @@
|
|||||||
await page.close()
|
await page.close()
|
||||||
return {resized, mime}
|
return {resized, mime}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Wait */
|
||||||
|
export async function wait(seconds) {
|
||||||
|
await new Promise(solve => setTimeout(solve), seconds*1000)
|
||||||
|
}
|
||||||
@@ -51,11 +51,22 @@
|
|||||||
let page = 1
|
let page = 1
|
||||||
let next = false
|
let next = false
|
||||||
do {
|
do {
|
||||||
|
try {
|
||||||
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (favorites ${type}s - page ${page})`)
|
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (favorites ${type}s - page ${page})`)
|
||||||
const {data:{data:{User:{favourites:{[type]:{nodes, pageInfo:cursor}}}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, page}, query:queries.anilist.favorites({type})})
|
const {data:{data:{User:{favourites:{[type]:{nodes, pageInfo:cursor}}}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, page}, query:queries.anilist.favorites({type})})
|
||||||
page++
|
page++
|
||||||
next = cursor.hasNextPage
|
next = cursor.hasNextPage
|
||||||
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) {
|
||||||
|
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)
|
||||||
|
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
|
||||||
@@ -75,6 +86,7 @@
|
|||||||
let page = 1
|
let page = 1
|
||||||
let next = false
|
let next = false
|
||||||
do {
|
do {
|
||||||
|
try {
|
||||||
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (favorites characters - page ${page})`)
|
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (favorites characters - page ${page})`)
|
||||||
const {data:{data:{User:{favourites:{characters:{nodes, pageInfo:cursor}}}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, page}, query:queries.anilist.characters()})
|
const {data:{data:{User:{favourites:{characters:{nodes, pageInfo:cursor}}}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, page}, query:queries.anilist.characters()})
|
||||||
page++
|
page++
|
||||||
@@ -83,6 +95,16 @@
|
|||||||
console.debug(`metrics/compute/${login}/plugins > anilist > processing ${name}`)
|
console.debug(`metrics/compute/${login}/plugins > anilist > processing ${name}`)
|
||||||
characters.push({name, artwork:artwork ? await imports.imgb64(artwork) : "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mOcOnfpfwAGfgLYttYINwAAAABJRU5ErkJggg=="})
|
characters.push({name, artwork:artwork ? await imports.imgb64(artwork) : "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mOcOnfpfwAGfgLYttYINwAAAABJRU5ErkJggg=="})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (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)
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user