refactor: replace amvstrm source with aniwatch

This commit is contained in:
2024-08-18 21:37:13 -04:00
parent 0bcc547ede
commit 1a06eb51eb
14 changed files with 179 additions and 262 deletions

View File

@@ -0,0 +1,107 @@
import { Episode, type EpisodesResponse } from "./episode";
export async function getEpisodesFromAniwatch(
aniListId: number,
): Promise<EpisodesResponse | null> {
try {
const animeTitle = await import("~/controllers/title/anilist")
.then(({ fetchTitleFromAnilist }) =>
fetchTitleFromAnilist(aniListId, undefined),
)
.then((title) => title?.title?.userPreferred ?? title?.title?.english);
if (!animeTitle) {
return null;
}
const aniwatchId = await getAniwatchId(animeTitle);
if (!aniwatchId) {
return null;
}
const episodes: Episode[] | null = await fetch(
`https://aniwatch.up.railway.app/anime/episodes/${aniwatchId}`,
)
.then((res) => res.json<AniwatchEpisodesResponse>())
.then(({ totalEpisodes, episodes }) => {
if (totalEpisodes === 0) {
console.error(
`Error trying to load episodes from aniwatch; aniListId: ${aniListId}, totalEpisodes: ${totalEpisodes}`,
);
return null;
}
return episodes.map<Episode>(({ episodeId, number, title }) => ({
id: episodeId,
number,
title,
updatedAt: 0,
}));
});
if (!episodes || episodes.length === 0) {
return null;
}
return { providerId: "aniwatch", episodes };
} catch (error) {
console.error(
new Error(
`Error trying to load episodes from aniwatch; aniListId: ${aniListId}`,
{ cause: error },
),
);
}
return null;
}
function getAniwatchId(animeTitle: string): Promise<string | undefined> {
return fetch(
`https://aniwatch.up.railway.app/anime/search?q=${encodeURIComponent(animeTitle)}`,
)
.then((res) => res.json<AniwatchSearchResponse>())
.then(({ animes }) => animes[0]?.id);
}
export interface AniwatchEpisodesResponse {
totalEpisodes: number;
episodes: AniwatchEpisode[];
}
export interface AniwatchEpisode {
title: string;
episodeId: string;
number: number;
isFiller: boolean;
}
export interface AniwatchSearchResponse {
animes: Anime[];
currentPage: number;
hasNextPage: boolean;
totalPages: number;
}
interface Anime {
id: string;
name: string;
jname: string;
poster: string;
duration: string;
type: Type;
rating: null | string;
episodes: Episodes;
}
interface Episodes {
sub: number | null;
dub: number | null;
}
enum Type {
Movie = "Movie",
Ona = "ONA",
Ova = "OVA",
Special = "Special",
Tv = "TV",
}