feat: create route to fetch episodes for a title

Summary:

Test Plan:
This commit is contained in:
2024-05-24 17:07:06 -04:00
parent c5cce2543c
commit 75bb7615f5
11 changed files with 533 additions and 6 deletions

View File

@@ -0,0 +1,110 @@
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;
try {
const abortController = new AbortController();
response = await Promise.race([
fetch(`https://api.anify.tv/episodes/${aniListId}`, {
signal: abortController.signal,
}).then((res) => res.json() as Promise<AnifyEpisodesResponse[]>),
// set a limit of 30 seconds
new Promise((resolve) => setTimeout(resolve, 30 * 1000)).then(() => {
abortController.abort("Loading episodes from Anify timed out");
console.error(
`Loading episodes from Anify timed out; aniListId: ${aniListId}`,
);
return null;
}),
]);
} catch (e) {
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;
}[];
}

View 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),
});

View File

@@ -0,0 +1,168 @@
import { describe, expect, it } from "bun:test";
import app from "~/index";
import { server } from "~/mocks";
server.listen();
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/1",
{},
{
ENABLE_ANIFY: "false",
},
);
expect(response.json()).resolves.toEqual({ success: false });
expect(response.status).toBe(404);
});
it("with an unknown title from all sources", async () => {
const response = await app.request("/title?id=-1");
expect(response.json()).resolves.toEqual({ success: false });
expect(response.status).toBe(404);
});
});

View File

@@ -0,0 +1,63 @@
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
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",
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<HonoEnv>();
app.openapi(route, async (c) => {
const aniListId = Number(c.req.param("aniListId"));
const episodes = await fetchFromMultipleSources([
() => getEpisodesFromAnify(JSON.parse(c.env.ENABLE_ANIFY), aniListId),
]);
if (!episodes) {
return c.json(ErrorResponse, { status: 404 });
}
return c.json({
success: true,
result: episodes,
});
});
export default app;

View File

@@ -2,7 +2,6 @@ import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
import { PaginatedResponseSchema } from "~/types/schema";
import { Title } from "~/types/title";
import { fetchSearchResultsFromAmvstrm } from "./amvstrm";
import { fetchSearchResultsFromAnilist } from "./anilist";