feat: add more 429 checks
This commit is contained in:
@@ -55,6 +55,7 @@ export async function getUser(aniListToken: string): Promise<User> {
|
|||||||
if (err.response?.status === 401) {
|
if (err.response?.status === 401) {
|
||||||
return null;
|
return null;
|
||||||
} else if (err.response?.status === 429) {
|
} else if (err.response?.status === 429) {
|
||||||
|
console.log("429, retrying in", err.response.headers.get("Retry-After"));
|
||||||
return sleep(
|
return sleep(
|
||||||
Number(err.response.headers.get("Retry-After")!) * 1000,
|
Number(err.response.headers.get("Retry-After")!) * 1000,
|
||||||
).then(() => getUser(aniListToken));
|
).then(() => getUser(aniListToken));
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ export async function getUpcomingTitlesFromAnilist(env: Env, req: HonoRequest) {
|
|||||||
shouldContinue = pageInfo?.hasNextPage ?? false;
|
shouldContinue = pageInfo?.hasNextPage ?? false;
|
||||||
} while (shouldContinue);
|
} while (shouldContinue);
|
||||||
|
|
||||||
await Promise.allSettled(
|
await Promise.all(
|
||||||
Array.from(plannedToWatchTitles).map((titleId) =>
|
Array.from(plannedToWatchTitles).map((titleId) =>
|
||||||
maybeScheduleNextAiringEpisode(env, req, titleId),
|
maybeScheduleNextAiringEpisode(env, req, titleId),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { graphql } from "gql.tada";
|
import { graphql } from "gql.tada";
|
||||||
import { GraphQLClient } from "graphql-request";
|
import { GraphQLClient } from "graphql-request";
|
||||||
|
|
||||||
|
import { sleep } from "~/libs/sleep";
|
||||||
|
|
||||||
const SearchQuery = graphql(`
|
const SearchQuery = graphql(`
|
||||||
query Search($query: String!, $page: Int!, $limit: Int!) {
|
query Search($query: String!, $page: Int!, $limit: Int!) {
|
||||||
Page(page: $page, perPage: $limit) {
|
Page(page: $page, perPage: $limit) {
|
||||||
@@ -27,7 +29,7 @@ export async function fetchSearchResultsFromAnilist(
|
|||||||
query: string,
|
query: string,
|
||||||
page: number,
|
page: number,
|
||||||
limit: number,
|
limit: number,
|
||||||
) {
|
): Promise<SearchResultsResponse | undefined> {
|
||||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
const client = new GraphQLClient("https://graphql.anilist.co/");
|
||||||
|
|
||||||
return client
|
return client
|
||||||
@@ -40,5 +42,31 @@ export async function fetchSearchResultsFromAnilist(
|
|||||||
|
|
||||||
const { media: results, pageInfo } = page;
|
const { media: results, pageInfo } = page;
|
||||||
return { results, hasNextPage: pageInfo?.hasNextPage };
|
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;
|
||||||
|
};
|
||||||
|
|||||||
@@ -44,6 +44,10 @@ export async function getNextEpisodeTimeUntilAiring(
|
|||||||
return { status, nextAiring };
|
return { status, nextAiring };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.response.status === 429) {
|
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);
|
await sleep(Number(error.response.headers.get("Retry-After")!) * 1000);
|
||||||
return getNextEpisodeTimeUntilAiring(aniListId);
|
return getNextEpisodeTimeUntilAiring(aniListId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { GraphQLClient } from "graphql-request";
|
|||||||
import type { Title } from "~/types/title";
|
import type { Title } from "~/types/title";
|
||||||
import { MediaFragment } from "~/types/title/mediaFragment";
|
import { MediaFragment } from "~/types/title/mediaFragment";
|
||||||
|
|
||||||
|
import { sleep } from "../sleep";
|
||||||
|
|
||||||
const GetTitleQuery = graphql(
|
const GetTitleQuery = graphql(
|
||||||
`
|
`
|
||||||
query GetTitle($id: Int!) {
|
query GetTitle($id: Int!) {
|
||||||
@@ -32,6 +34,15 @@ export async function fetchTitleFromAnilist(
|
|||||||
if (error.message.includes("Not Found")) {
|
if (error.message.includes("Not Found")) {
|
||||||
return undefined;
|
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;
|
throw error;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user