refactor: replace amvstrm source with aniwatch
This commit is contained in:
107
src/controllers/episodes/getByAniListId/aniwatch.ts
Normal file
107
src/controllers/episodes/getByAniListId/aniwatch.ts
Normal 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",
|
||||
}
|
||||
Reference in New Issue
Block a user