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

@@ -1,60 +0,0 @@
import { Episode, type EpisodesResponse } from "./episode";
export async function getEpisodesFromAmvstrm(
aniListId: number,
): Promise<EpisodesResponse | null> {
try {
const episodes: Episode[] | null = await fetch(
`https://amvstrm.up.railway.app/api/v2/episode/${aniListId}`,
)
.then((res) => res.json<AmvstrmEpisodesResponse>())
.then(({ code, message, episodes }) => {
if (code >= 400) {
console.error(
`Error trying to load episodes from amvstrm; aniListId: ${aniListId}, code: ${code}, message: ${message}`,
);
return null;
}
return episodes.map<Episode>(
({ id, description, image, title, episode, airDate }) => ({
id,
number: episode,
description,
img: image,
title,
updatedAt: airDate ?? 0,
}),
);
});
if (!episodes || episodes.length === 0) {
return null;
}
return { providerId: "amvstrm", episodes };
} catch (error) {
console.error(
new Error(
`Error trying to load episodes from amvstrm; aniListId: ${aniListId}`,
{ cause: error },
),
);
}
return null;
}
interface AmvstrmEpisodesResponse {
code: number;
message: string;
episodes: AmvstrmEpisode[];
}
interface AmvstrmEpisode {
id: string;
title: string;
description: string | null;
episode: number;
image: string;
airDate: null;
}

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",
}

View File

@@ -59,8 +59,8 @@ app.openapi(route, async (c) => {
getEpisodesFromConsumet(aniListId),
),
() =>
import("./amvstrm").then(({ getEpisodesFromAmvstrm }) =>
getEpisodesFromAmvstrm(aniListId),
import("./aniwatch").then(({ getEpisodesFromAniwatch }) =>
getEpisodesFromAniwatch(aniListId),
),
]);