refactor: move fetchEpisodes in to subfolder
This commit is contained in:
45
src/controllers/episodes/getByAniListId/consumet.ts
Normal file
45
src/controllers/episodes/getByAniListId/consumet.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { ANIME, META } from "@consumet/extensions";
|
||||
import fetchAdapter from "@haverstack/axios-fetch-adapter";
|
||||
|
||||
import { Episode, EpisodesResponse } from "./episode";
|
||||
|
||||
export async function getEpisodesFromConsumet(
|
||||
aniListId: number,
|
||||
): Promise<EpisodesResponse | null> {
|
||||
const gogoAnime = new ANIME.Gogoanime(undefined, undefined, fetchAdapter);
|
||||
const aniList = new META.Anilist(gogoAnime, undefined, fetchAdapter);
|
||||
|
||||
try {
|
||||
const episodes: Episode[] = await aniList
|
||||
.fetchEpisodesListById(aniListId.toString())
|
||||
.then((episodes) =>
|
||||
episodes.map(
|
||||
({ id, number, title, image: img, description }): Episode => ({
|
||||
id,
|
||||
number,
|
||||
title,
|
||||
img,
|
||||
description,
|
||||
rating: undefined,
|
||||
updatedAt: 0,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (!episodes || episodes.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { providerId: "consumet", episodes };
|
||||
} catch (error: any) {
|
||||
if (!error.message.includes("failed with status code")) {
|
||||
console.error(
|
||||
new Error(
|
||||
`Error trying to load episodes from consumet; aniListId: ${aniListId}`,
|
||||
{ cause: error },
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user