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; 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( function getAniwatchId(
animeTitle: Partial<{ english: string; userPreferred: string }>, animeTitle: Partial<{ english: string; userPreferred: string }>,
): Promise<string | undefined> { ): Promise<string | undefined> {
return fetch( 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(
.then(({ animes }) => { (res) =>
res.json() as Promise<{
success: boolean;
data: AniwatchSearchResponse;
}>,
)
.then(({ success, data: { animes } }) => {
if (!success) {
return;
}
const bestMatchingTitle = findBestMatchingTitle( const bestMatchingTitle = findBestMatchingTitle(
animeTitle, animeTitle,
animes.map((anime) => ({ animes.map((anime) => ({

View File

@@ -6,14 +6,21 @@ export async function getSourcesFromAniwatch(
): Promise<FetchUrlResponse | null> { ): Promise<FetchUrlResponse | null> {
console.log(`Fetching sources from aniwatch for ${watchId}`); console.log(`Fetching sources from aniwatch for ${watchId}`);
const { source, intro, outro, subtitles } = await fetch( 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<AniwatchEpisodeUrlResponse>()) .then(
.then(({ intro, outro, sources, tracks }) => { (res) =>
if (!sources || sources.length === 0) { res.json() as Promise<{
success: boolean;
data: AniwatchEpisodeUrlResponse;
}>,
)
.then(({ success, data }) => {
if (!success || !data.sources || data.sources.length === 0) {
return { source: null }; return { source: null };
} }
const { intro, outro, sources, tracks } = data;
return { return {
intro: convertSkipTime(intro), intro: convertSkipTime(intro),
outro: convertSkipTime(outro), outro: convertSkipTime(outro),