chore: update aniwatch calls

aniwatch API had breaking changes
This commit is contained in:
2024-10-24 08:51:26 -04:00
parent f3bd6eb9cc
commit 882f9b1843
2 changed files with 67 additions and 7 deletions

View File

@@ -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<Episode>(({ episodeId, number, title }) => ({
id: episodeId,
number,
title,
updatedAt: 0,
}));
});
}
function getAniwatchId(
animeTitle: Partial<{ english: string; userPreferred: string }>,
): Promise<string | undefined> {
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<AniwatchSearchResponse>())
.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) => ({