From 882f9b184362708ef9d85b8aca37667911397c22 Mon Sep 17 00:00:00 2001 From: Rushil Perera Date: Thu, 24 Oct 2024 08:51:26 -0400 Subject: [PATCH] chore: update aniwatch calls aniwatch API had breaking changes --- .../episodes/getByAniListId/aniwatch.ts | 59 ++++++++++++++++++- .../episodes/getEpisodeUrl/aniwatch.ts | 15 +++-- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/src/controllers/episodes/getByAniListId/aniwatch.ts b/src/controllers/episodes/getByAniListId/aniwatch.ts index 3d78249..64802c1 100644 --- a/src/controllers/episodes/getByAniListId/aniwatch.ts +++ b/src/controllers/episodes/getByAniListId/aniwatch.ts @@ -71,14 +71,67 @@ export async function getEpisodesFromAniwatch( return null; } +async function fetchEpisodes( + aniwatchId: string, + aniListId: number, +): Promise< + | { + number: number; + id: string; + updatedAt: number; + description?: string | null | undefined; + title?: string | null | undefined; + img?: string | null | undefined; + rating?: number | null | undefined; + }[] + | null +> { + return await fetch( + `https://aniwatch.up.railway.app/api/v2/hianime/anime/${aniwatchId}/episodes`, + ) + .then( + (res) => + res.json() as Promise<{ + success: boolean; + data: AniwatchEpisodesResponse; + }>, + ) + .then(({ success, data }) => { + if (!success || data.totalEpisodes === 0) { + console.error( + `Error trying to load episodes from aniwatch; aniListId: ${aniListId}, totalEpisodes: ${totalEpisodes}`, + ); + return null; + } + const { totalEpisodes, episodes } = data; + + return episodes.map(({ episodeId, number, title }) => ({ + id: episodeId, + number, + title, + updatedAt: 0, + })); + }); +} + function getAniwatchId( animeTitle: Partial<{ english: string; userPreferred: string }>, ): Promise { return fetch( - `https://aniwatch.up.railway.app/anime/search?q=${encodeURIComponent(animeTitle.english ?? animeTitle.userPreferred!)}`, + `https://aniwatch.up.railway.app/api/v2/hianime/search?q=${encodeURIComponent(animeTitle.english ?? animeTitle.userPreferred!)}`, ) - .then((res) => res.json()) - .then(({ animes }) => { + .then( + (res) => + res.json() as Promise<{ + success: boolean; + data: AniwatchSearchResponse; + }>, + ) + .then(({ success, data: { animes } }) => { + if (!success) { + return; + } + const bestMatchingTitle = findBestMatchingTitle( animeTitle, animes.map((anime) => ({ diff --git a/src/controllers/episodes/getEpisodeUrl/aniwatch.ts b/src/controllers/episodes/getEpisodeUrl/aniwatch.ts index 67b9ca0..1c8ddb7 100644 --- a/src/controllers/episodes/getEpisodeUrl/aniwatch.ts +++ b/src/controllers/episodes/getEpisodeUrl/aniwatch.ts @@ -6,14 +6,21 @@ export async function getSourcesFromAniwatch( ): Promise { console.log(`Fetching sources from aniwatch for ${watchId}`); const { source, intro, outro, subtitles } = await fetch( - `https://aniwatch.up.railway.app/anime/episode-srcs?id=${encodeURIComponent(watchId)}`, + `https://aniwatch.up.railway.app/api/v2/hianime/episode/sources?animeEpisodeId=${encodeURIComponent(watchId)}`, ) - .then((res) => res.json()) - .then(({ intro, outro, sources, tracks }) => { - if (!sources || sources.length === 0) { + .then( + (res) => + res.json() as Promise<{ + success: boolean; + data: AniwatchEpisodeUrlResponse; + }>, + ) + .then(({ success, data }) => { + if (!success || !data.sources || data.sources.length === 0) { return { source: null }; } + const { intro, outro, sources, tracks } = data; return { intro: convertSkipTime(intro), outro: convertSkipTime(outro),