diff options
Diffstat (limited to 'src/lib/Data/AniList/schedule.ts')
| -rw-r--r-- | src/lib/Data/AniList/schedule.ts | 145 |
1 files changed, 70 insertions, 75 deletions
diff --git a/src/lib/Data/AniList/schedule.ts b/src/lib/Data/AniList/schedule.ts index 18f02579..474f0e92 100644 --- a/src/lib/Data/AniList/schedule.ts +++ b/src/lib/Data/AniList/schedule.ts @@ -1,44 +1,44 @@ -import type { Media, MediaTitle } from './media'; +import type { Media, MediaTitle } from "./media"; interface SchedulePage { - data: { - Page: { - media: { - title: MediaTitle; - synonyms: string[]; - id: number; - idMal: number; - episodes: number; - nextAiringEpisode?: { - episode: number; - airingAt?: number; - }; - coverImage: { - extraLarge: string; - medium: string; - }; - }[]; - pageInfo: { - hasNextPage: boolean; - }; - }; - }; + data: { + Page: { + media: { + title: MediaTitle; + synonyms: string[]; + id: number; + idMal: number; + episodes: number; + nextAiringEpisode?: { + episode: number; + airingAt?: number; + }; + coverImage: { + extraLarge: string; + medium: string; + }; + }[]; + pageInfo: { + hasNextPage: boolean; + }; + }; + }; } const schedulePage = async ( - page: number, - year: number, - season: 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL' + page: number, + year: number, + season: "WINTER" | "SPRING" | "SUMMER" | "FALL", ): Promise<SchedulePage> => - await ( - await fetch('https://graphql.anilist.co', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Accept: 'application/json' - }, - body: JSON.stringify({ - query: `{ + await ( + await fetch("https://graphql.anilist.co", { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + }, + body: JSON.stringify({ + query: `{ Page(page: ${page}) { pageInfo { hasNextPage @@ -50,57 +50,52 @@ const schedulePage = async ( coverImage { extraLarge medium } } } -}` - }) - }) - ).json(); +}`, + }), + }) + ).json(); -type Season = 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'; +type Season = "WINTER" | "SPRING" | "SUMMER" | "FALL"; -export const scheduleMediaListCollection = async ( - year: number, - season: Season, - includeLastSeason = false +const collectAllSchedulePages = async ( + year: number, + season: Season, + into: SchedulePage["data"]["Page"]["media"], ) => { - const scheduledMedia = []; - let page = 1; - let currentPage = await schedulePage(page, year, season); - - for (const candidate of currentPage.data.Page.media) scheduledMedia.push(candidate); + let page = 1; - while (currentPage['data']['Page']['pageInfo']['hasNextPage']) { - for (const candidate of currentPage.data.Page.media) scheduledMedia.push(candidate); + while (true) { + const currentPage = await schedulePage(page, year, season); - page += 1; - currentPage = await schedulePage(page, year, season); - } + for (const candidate of currentPage.data.Page.media) into.push(candidate); - for (const candidate of currentPage.data.Page.media) scheduledMedia.push(candidate); + if (!currentPage.data.Page.pageInfo.hasNextPage) break; - if (includeLastSeason) { - const lastSeason = { - WINTER: 'FALL', - SPRING: 'WINTER', - SUMMER: 'SPRING', - FALL: 'SUMMER' - }[season]; - - const lastSeasonYear = season === 'WINTER' ? year - 1 : year; + page += 1; + } +}; - let page = 1; - let currentPage = await schedulePage(page, lastSeasonYear, lastSeason as Season); +export const scheduleMediaListCollection = async ( + year: number, + season: Season, + includeLastSeason = false, +) => { + const scheduledMedia: SchedulePage["data"]["Page"]["media"] = []; - for (const candidate of currentPage.data.Page.media) scheduledMedia.push(candidate); + await collectAllSchedulePages(year, season, scheduledMedia); - while (currentPage['data']['Page']['pageInfo']['hasNextPage']) { - for (const candidate of currentPage.data.Page.media) scheduledMedia.push(candidate); + if (includeLastSeason) { + const lastSeason = { + WINTER: "FALL", + SPRING: "WINTER", + SUMMER: "SPRING", + FALL: "SUMMER", + }[season] as Season; - page += 1; - currentPage = await schedulePage(page, lastSeasonYear, lastSeason as Season); - } + const lastSeasonYear = season === "WINTER" ? year - 1 : year; - for (const candidate of currentPage.data.Page.media) scheduledMedia.push(candidate); - } + await collectAllSchedulePages(lastSeasonYear, lastSeason, scheduledMedia); + } - return scheduledMedia as Partial<Media[]>; + return scheduledMedia as Partial<Media[]>; }; |