From 59a7a5f8d48b6dd181ef452abe7885daf27ae458 Mon Sep 17 00:00:00 2001 From: Rushil Perera Date: Thu, 24 Oct 2024 08:51:46 -0400 Subject: [PATCH] fix: tweak aniwatch call to support tower of god s2 being split into 2 --- .../episodes/getByAniListId/aniwatch.ts | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/controllers/episodes/getByAniListId/aniwatch.ts b/src/controllers/episodes/getByAniListId/aniwatch.ts index 64802c1..f29a2d5 100644 --- a/src/controllers/episodes/getByAniListId/aniwatch.ts +++ b/src/controllers/episodes/getByAniListId/aniwatch.ts @@ -25,29 +25,36 @@ export async function getEpisodesFromAniwatch( return null; } - const episodes: Episode[] | null = await fetch( - `https://aniwatch.up.railway.app/anime/episodes/${aniwatchId}`, - ) - .then((res) => res.json()) - .then(({ totalEpisodes, episodes }) => { - if (totalEpisodes === 0) { - console.error( - `Error trying to load episodes from aniwatch; aniListId: ${aniListId}, totalEpisodes: ${totalEpisodes}`, - ); - return null; - } - - return episodes.map(({ episodeId, number, title }) => ({ - id: episodeId, - number, - title, - updatedAt: 0, - })); - }); + const episodes: Episode[] | null = await fetchEpisodes( + aniwatchId, + aniListId, + ); if (!episodes || episodes.length === 0) { return null; } + // Tower of God S2 + if (aniListId == 153406) { + const aniwatchId = await getAniwatchId({ + english: "Tower of God Season 2: Workshop Battle", + }); + if (aniwatchId) { + const lastEpisodeOfPreviousTitle = episodes.at(-1)!!.number; + return { + providerId: "aniwatch", + episodes: await fetchEpisodes(aniwatchId, aniListId).then( + (extraEpisodes) => + episodes.concat( + extraEpisodes?.map(({ number, ...episode }) => ({ + ...episode, + number: number + lastEpisodeOfPreviousTitle, + })) ?? [], + ), + ), + }; + } + } + return { providerId: "aniwatch", episodes }; } catch (error) { if (shouldRetry && "response" in error && error.response.status === 429) {