diff options
| -rw-r--r-- | src/lib/Data/AniList/schedule.ts | 55 |
1 files changed, 18 insertions, 37 deletions
diff --git a/src/lib/Data/AniList/schedule.ts b/src/lib/Data/AniList/schedule.ts index eb70a03b..474f0e92 100644 --- a/src/lib/Data/AniList/schedule.ts +++ b/src/lib/Data/AniList/schedule.ts @@ -57,28 +57,32 @@ const schedulePage = async ( type Season = "WINTER" | "SPRING" | "SUMMER" | "FALL"; -export const scheduleMediaListCollection = async ( +const collectAllSchedulePages = async ( year: number, season: Season, - includeLastSeason = false, + 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); + while (true) { + const currentPage = await schedulePage(page, year, season); - while (currentPage["data"]["Page"]["pageInfo"]["hasNextPage"]) { - for (const candidate of currentPage.data.Page.media) - scheduledMedia.push(candidate); + for (const candidate of currentPage.data.Page.media) into.push(candidate); + + if (!currentPage.data.Page.pageInfo.hasNextPage) break; page += 1; - currentPage = await schedulePage(page, year, 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); if (includeLastSeason) { const lastSeason = { @@ -86,34 +90,11 @@ export const scheduleMediaListCollection = async ( SPRING: "WINTER", SUMMER: "SPRING", FALL: "SUMMER", - }[season]; + }[season] as Season; const lastSeasonYear = season === "WINTER" ? year - 1 : year; - let page = 1; - let currentPage = await schedulePage( - page, - lastSeasonYear, - lastSeason as Season, - ); - - for (const candidate of currentPage.data.Page.media) - scheduledMedia.push(candidate); - - while (currentPage["data"]["Page"]["pageInfo"]["hasNextPage"]) { - for (const candidate of currentPage.data.Page.media) - scheduledMedia.push(candidate); - - page += 1; - currentPage = await schedulePage( - page, - lastSeasonYear, - lastSeason as Season, - ); - } - - for (const candidate of currentPage.data.Page.media) - scheduledMedia.push(candidate); + await collectAllSchedulePages(lastSeasonYear, lastSeason, scheduledMedia); } return scheduledMedia as Partial<Media[]>; |