From 5ea90bda550836dfe57abd6e8ff5ef53cf223678 Mon Sep 17 00:00:00 2001 From: Rushil Perera Date: Sat, 5 Oct 2024 15:52:38 -0400 Subject: [PATCH] feat: add more 429 checks --- src/controllers/auth/anilist/getUser.ts | 1 + .../internal/upcoming-titles/anilist.ts | 2 +- src/controllers/search/anilist.ts | 30 ++++++++++++++++++- src/libs/anilist/getNextEpisodeAiringAt.ts | 4 +++ src/libs/anilist/getTitle.ts | 11 +++++++ 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/controllers/auth/anilist/getUser.ts b/src/controllers/auth/anilist/getUser.ts index b6584fb..8d69580 100644 --- a/src/controllers/auth/anilist/getUser.ts +++ b/src/controllers/auth/anilist/getUser.ts @@ -55,6 +55,7 @@ export async function getUser(aniListToken: string): Promise { if (err.response?.status === 401) { return null; } else if (err.response?.status === 429) { + console.log("429, retrying in", err.response.headers.get("Retry-After")); return sleep( Number(err.response.headers.get("Retry-After")!) * 1000, ).then(() => getUser(aniListToken)); diff --git a/src/controllers/internal/upcoming-titles/anilist.ts b/src/controllers/internal/upcoming-titles/anilist.ts index d96fd81..10a5023 100644 --- a/src/controllers/internal/upcoming-titles/anilist.ts +++ b/src/controllers/internal/upcoming-titles/anilist.ts @@ -88,7 +88,7 @@ export async function getUpcomingTitlesFromAnilist(env: Env, req: HonoRequest) { shouldContinue = pageInfo?.hasNextPage ?? false; } while (shouldContinue); - await Promise.allSettled( + await Promise.all( Array.from(plannedToWatchTitles).map((titleId) => maybeScheduleNextAiringEpisode(env, req, titleId), ), diff --git a/src/controllers/search/anilist.ts b/src/controllers/search/anilist.ts index 0a53e44..55f57ea 100644 --- a/src/controllers/search/anilist.ts +++ b/src/controllers/search/anilist.ts @@ -1,6 +1,8 @@ import { graphql } from "gql.tada"; import { GraphQLClient } from "graphql-request"; +import { sleep } from "~/libs/sleep"; + const SearchQuery = graphql(` query Search($query: String!, $page: Int!, $limit: Int!) { Page(page: $page, perPage: $limit) { @@ -27,7 +29,7 @@ export async function fetchSearchResultsFromAnilist( query: string, page: number, limit: number, -) { +): Promise { const client = new GraphQLClient("https://graphql.anilist.co/"); return client @@ -40,5 +42,31 @@ export async function fetchSearchResultsFromAnilist( const { media: results, pageInfo } = page; return { results, hasNextPage: pageInfo?.hasNextPage }; + }) + .catch((err) => { + const response = err.response; + if (response.status === 429) { + console.log("429, retrying in", response.headers.get("Retry-After")); + return sleep(Number(response.headers.get("Retry-After")!) * 1000).then( + () => fetchSearchResultsFromAnilist(query, page, limit), + ); + } + + throw err; }); } + +type SearchResultsResponse = { + results: + | ({ + id: number; + title: { userPreferred: string | null; english: string | null } | null; + coverImage: { + extraLarge: string | null; + large: string | null; + medium: string | null; + } | null; + } | null)[] + | null; + hasNextPage: boolean | null | undefined; +}; diff --git a/src/libs/anilist/getNextEpisodeAiringAt.ts b/src/libs/anilist/getNextEpisodeAiringAt.ts index c538cd4..bd0fa39 100644 --- a/src/libs/anilist/getNextEpisodeAiringAt.ts +++ b/src/libs/anilist/getNextEpisodeAiringAt.ts @@ -44,6 +44,10 @@ export async function getNextEpisodeTimeUntilAiring( return { status, nextAiring }; } catch (error) { if (error.response.status === 429) { + console.log( + "429, retrying in", + error.response.headers.get("Retry-After"), + ); await sleep(Number(error.response.headers.get("Retry-After")!) * 1000); return getNextEpisodeTimeUntilAiring(aniListId); } diff --git a/src/libs/anilist/getTitle.ts b/src/libs/anilist/getTitle.ts index 9ac116b..64e88d8 100644 --- a/src/libs/anilist/getTitle.ts +++ b/src/libs/anilist/getTitle.ts @@ -4,6 +4,8 @@ import { GraphQLClient } from "graphql-request"; import type { Title } from "~/types/title"; import { MediaFragment } from "~/types/title/mediaFragment"; +import { sleep } from "../sleep"; + const GetTitleQuery = graphql( ` query GetTitle($id: Int!) { @@ -32,6 +34,15 @@ export async function fetchTitleFromAnilist( if (error.message.includes("Not Found")) { return undefined; } + if (error.response.status === 429) { + console.log( + "429, retrying in", + error.response.headers.get("Retry-After"), + ); + return sleep( + Number(error.response.headers.get("Retry-After")!) * 1000, + ).then(() => fetchTitleFromAnilist(id, token)); + } throw error; });