refactor: move fetchEpisodes in to subfolder
This commit is contained in:
60
src/controllers/episodes/getByAniListId/amvstrm.ts
Normal file
60
src/controllers/episodes/getByAniListId/amvstrm.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { Episode, type EpisodesResponse } from "./episode";
|
||||
|
||||
export async function getEpisodesFromAmvstrm(
|
||||
aniListId: number,
|
||||
): Promise<EpisodesResponse | null> {
|
||||
try {
|
||||
const episodes: Episode[] | null = await fetch(
|
||||
`https://api-amvstrm.nyt92.eu.org/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;
|
||||
}
|
||||
107
src/controllers/episodes/getByAniListId/anify.ts
Normal file
107
src/controllers/episodes/getByAniListId/anify.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { PromiseTimedOutError, promiseTimeout } from "~/libs/promiseTimeout";
|
||||
import { sortByProperty } from "~/libs/sortByProperty";
|
||||
|
||||
import type { EpisodesResponse } from "./episode";
|
||||
|
||||
export async function getEpisodesFromAnify(
|
||||
isAnifyEnabled: boolean,
|
||||
aniListId: number,
|
||||
): Promise<EpisodesResponse | null> {
|
||||
if (shouldSkipAnify(isAnifyEnabled, aniListId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let response: AnifyEpisodesResponse[] | null = null;
|
||||
const abortController = new AbortController();
|
||||
try {
|
||||
response = await promiseTimeout(
|
||||
fetch(`https://api.anify.tv/episodes/${aniListId}`, {
|
||||
signal: abortController.signal,
|
||||
}).then((res) => res.json<AnifyEpisodesResponse[]>()),
|
||||
30 * 1000,
|
||||
);
|
||||
} catch (e) {
|
||||
if (e instanceof PromiseTimedOutError) {
|
||||
abortController.abort("Loading episodes from Anify timed out");
|
||||
}
|
||||
console.error(
|
||||
new Error(
|
||||
`Error trying to load episodes from anify; aniListId: ${aniListId}`,
|
||||
{ cause: e },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (!response || response.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const sourcePriority = {
|
||||
gogoanime: 1,
|
||||
};
|
||||
const filteredEpisodesData = response
|
||||
.filter(({ providerId }) => {
|
||||
if (providerId === "9anime") {
|
||||
return false;
|
||||
}
|
||||
if (aniListId == 166873 && providerId === "zoro") {
|
||||
// Mushoku Tensei: Job Reincarnation S2 Part 2 returns incorrect mapping for Zoro only
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
.sort(sortByProperty(sourcePriority, "providerId"));
|
||||
|
||||
const selectedEpisodeData = filteredEpisodesData[0];
|
||||
return {
|
||||
providerId: selectedEpisodeData.providerId,
|
||||
episodes: selectedEpisodeData.episodes.map(
|
||||
({ id, number, description, img, rating, title, updatedAt }) => ({
|
||||
id,
|
||||
number,
|
||||
description,
|
||||
img,
|
||||
rating,
|
||||
title,
|
||||
updatedAt: updatedAt ?? 0,
|
||||
}),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
export function shouldSkipAnify(
|
||||
isAnifyEnabled: boolean,
|
||||
aniListId: number,
|
||||
): boolean {
|
||||
if (!isAnifyEnabled) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Some mappings on Anify are incorrect so they return episodes from a similar title
|
||||
if (
|
||||
[
|
||||
158927, // Spy x Family S2
|
||||
166873, // Mushoku Tensei: Jobless Reincarnation S2 part 2
|
||||
].includes(aniListId)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
interface AnifyEpisodesResponse {
|
||||
providerId: string;
|
||||
episodes: {
|
||||
id: string;
|
||||
isFiller: boolean | undefined;
|
||||
number: number;
|
||||
title: string;
|
||||
img: string | null;
|
||||
hasDub: boolean;
|
||||
description: string | null;
|
||||
rating: number | null;
|
||||
updatedAt: number | undefined;
|
||||
}[];
|
||||
}
|
||||
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;
|
||||
}
|
||||
20
src/controllers/episodes/getByAniListId/episode.ts
Normal file
20
src/controllers/episodes/getByAniListId/episode.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { EpisodeNumberSchema } from "~/types/schema";
|
||||
|
||||
export type Episode = z.infer<typeof Episode>;
|
||||
export const Episode = z.object({
|
||||
id: z.string(),
|
||||
number: EpisodeNumberSchema,
|
||||
title: z.string().nullish(),
|
||||
img: z.string().nullish(),
|
||||
description: z.string().nullish(),
|
||||
rating: z.number().int().nullish(),
|
||||
updatedAt: z.number().int().default(0).openapi({ format: "int64" }),
|
||||
});
|
||||
|
||||
export type EpisodesResponse = z.infer<typeof EpisodesResponse>;
|
||||
export const EpisodesResponse = z.object({
|
||||
providerId: z.string(),
|
||||
episodes: z.array(Episode),
|
||||
});
|
||||
221
src/controllers/episodes/getByAniListId/index.spec.ts
Normal file
221
src/controllers/episodes/getByAniListId/index.spec.ts
Normal file
@@ -0,0 +1,221 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
|
||||
import app from "~/index";
|
||||
import { server } from "~/mocks";
|
||||
import { mockConsumet } from "~/mocks/consumet";
|
||||
|
||||
server.listen();
|
||||
mockConsumet();
|
||||
|
||||
describe('requests the "/episodes" route', () => {
|
||||
it("with list of episodes from Anify", async () => {
|
||||
const response = await app.request(
|
||||
"/episodes/1",
|
||||
{},
|
||||
{
|
||||
ENABLE_ANIFY: "true",
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.json()).resolves.toEqual({
|
||||
success: true,
|
||||
result: {
|
||||
providerId: "zoro",
|
||||
episodes: [
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=103233",
|
||||
number: 1,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "Mission: Forgetter I",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=103632",
|
||||
number: 2,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "Mission: Forgetter II",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=104244",
|
||||
number: 3,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "Mission: Forgetter III",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=104620",
|
||||
number: 4,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "Mission: Forgetter IV",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=104844",
|
||||
number: 5,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "File: Glint",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=105761",
|
||||
number: 6,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "File: Dreamspeaker Thea",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=106135",
|
||||
number: 7,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "File: Forgetter Annette",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=106518",
|
||||
number: 8,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "Mission: Dreamspeaker I",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=106606",
|
||||
number: 9,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "Mission: Dreamspeaker II",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=106981",
|
||||
number: 10,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "Mission: Dreamspeaker III",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=107176",
|
||||
number: 11,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "Mission: Dreamspeaker IV",
|
||||
updatedAt: 0,
|
||||
},
|
||||
{
|
||||
id: "/watch/spy-classroom-season-2-18468?ep=107247",
|
||||
number: 12,
|
||||
description: null,
|
||||
img: null,
|
||||
rating: null,
|
||||
title: "File: Flower Garden Lily",
|
||||
updatedAt: 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("Anify ID filtered out, returns no episode list from Anify", async () => {
|
||||
const response = await app.request(
|
||||
"/episodes/158927",
|
||||
{},
|
||||
{
|
||||
ENABLE_ANIFY: "true",
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.json()).resolves.toEqual({ success: false });
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
|
||||
it("Anify is disabled, returns no episode list from Anify", async () => {
|
||||
const response = await app.request(
|
||||
"/episodes/2",
|
||||
{},
|
||||
{
|
||||
ENABLE_ANIFY: "false",
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.json()).resolves.toEqual({ success: false });
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
|
||||
it("with list of episodes from Consumet", async () => {
|
||||
const response = await app.request(
|
||||
"/episodes/3",
|
||||
{},
|
||||
{
|
||||
ENABLE_ANIFY: "true",
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.json()).resolves.toEqual({
|
||||
success: true,
|
||||
result: {
|
||||
providerId: "consumet",
|
||||
episodes: [
|
||||
{
|
||||
id: "consumet-1",
|
||||
number: 1,
|
||||
title: "Consumet 1",
|
||||
updatedAt: 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("with list of episodes from Amvstrm", async () => {
|
||||
const response = await app.request(
|
||||
"/episodes/4",
|
||||
{},
|
||||
{
|
||||
ENABLE_ANIFY: "true",
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.json()).resolves.toEqual({
|
||||
success: true,
|
||||
result: {
|
||||
providerId: "amvstrm",
|
||||
episodes: [
|
||||
{
|
||||
id: "amvstrm-1",
|
||||
number: 1,
|
||||
title: "EP 1",
|
||||
updatedAt: 0,
|
||||
img: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("with no episodes from all sources", async () => {
|
||||
const response = await app.request("/episodes/-1");
|
||||
|
||||
expect(response.json()).resolves.toEqual({ success: false });
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
||||
77
src/controllers/episodes/getByAniListId/index.ts
Normal file
77
src/controllers/episodes/getByAniListId/index.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||
|
||||
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
|
||||
import type { Env } from "~/types/env";
|
||||
import {
|
||||
AniListIdQuerySchema,
|
||||
ErrorResponse,
|
||||
ErrorResponseSchema,
|
||||
SuccessResponseSchema,
|
||||
} from "~/types/schema";
|
||||
|
||||
import { getEpisodesFromAnify } from "./anify";
|
||||
import { EpisodesResponse } from "./episode";
|
||||
|
||||
const EpisodesResponseSchema = SuccessResponseSchema(EpisodesResponse);
|
||||
|
||||
const route = createRoute({
|
||||
tags: ["aniplay", "episodes"],
|
||||
summary: "Fetch episodes for a title",
|
||||
operationId: "fetchEpisodes",
|
||||
method: "get",
|
||||
path: "/{aniListId}",
|
||||
request: {
|
||||
params: z.object({ aniListId: AniListIdQuerySchema }),
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: EpisodesResponseSchema,
|
||||
},
|
||||
},
|
||||
description: "Returns a list of episodes",
|
||||
},
|
||||
404: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: ErrorResponseSchema,
|
||||
},
|
||||
},
|
||||
description: "Returns an empty list because episodes not found",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const app = new OpenAPIHono<Env>();
|
||||
|
||||
app.openapi(route, async (c) => {
|
||||
const aniListId = Number(c.req.param("aniListId"));
|
||||
|
||||
const episodes = await fetchFromMultipleSources([
|
||||
() =>
|
||||
getEpisodesFromAnify(
|
||||
JSON.parse((c.env?.["ENABLE_ANIFY"] ?? "true") as string),
|
||||
aniListId,
|
||||
),
|
||||
() =>
|
||||
import("./consumet").then(({ getEpisodesFromConsumet }) =>
|
||||
getEpisodesFromConsumet(aniListId),
|
||||
),
|
||||
() =>
|
||||
import("./amvstrm").then(({ getEpisodesFromAmvstrm }) =>
|
||||
getEpisodesFromAmvstrm(aniListId),
|
||||
),
|
||||
]);
|
||||
|
||||
if (!episodes) {
|
||||
return c.json(ErrorResponse, { status: 404 });
|
||||
}
|
||||
|
||||
return c.json({
|
||||
success: true,
|
||||
result: episodes,
|
||||
});
|
||||
});
|
||||
|
||||
export default app;
|
||||
Reference in New Issue
Block a user