refactor!: migrate away from bun
- migrate package management to pnpm - migrate test suite to vitest - also remove Anify integration
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,133 +0,0 @@
|
||||
import { DateTime } from "luxon";
|
||||
|
||||
import { PromiseTimedOutError, promiseTimeout } from "~/libs/promiseTimeout";
|
||||
import { readEnvVariable } from "~/libs/readEnvVariable";
|
||||
import { sortByProperty } from "~/libs/sortByProperty";
|
||||
import { getValue, setValue } from "~/models/kv";
|
||||
import type { EpisodesResponse } from "~/types/episode";
|
||||
|
||||
export async function getEpisodesFromAnify(
|
||||
aniListId: number,
|
||||
): Promise<EpisodesResponse | null> {
|
||||
if (await shouldSkipAnify(aniListId)) {
|
||||
console.log("Skipping Anify for title", aniListId);
|
||||
return null;
|
||||
}
|
||||
|
||||
let response: AnifyEpisodesResponse[] | null = null;
|
||||
const abortController = new AbortController();
|
||||
try {
|
||||
response = await promiseTimeout(
|
||||
fetch(`https://anify.eltik.cc/episodes/${aniListId}`, {
|
||||
signal: abortController.signal,
|
||||
}).then((res) => res.json() as Promise<AnifyEpisodesResponse[]>),
|
||||
30 * 1000,
|
||||
);
|
||||
if ("error" in response) {
|
||||
const error = response.error;
|
||||
if (error === "Too many requests") {
|
||||
console.log(
|
||||
"Sending too many requests to Anify, setting killswitch until",
|
||||
DateTime.now().plus({ minutes: 1 }).toISO(),
|
||||
);
|
||||
setValue(
|
||||
"anify_killswitch_till",
|
||||
DateTime.now().plus({ minutes: 1 }).toISO(),
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof PromiseTimedOutError) {
|
||||
abortController.abort("Loading episodes from Anify timed out");
|
||||
}
|
||||
console.error(
|
||||
`Error trying to load episodes from anify; aniListId: ${aniListId}`,
|
||||
);
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
if (!response || response.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const sourcePriority = {
|
||||
zoro: 1,
|
||||
gogoanime: 2,
|
||||
};
|
||||
const filteredEpisodesData = response
|
||||
.filter(({ providerId }) => {
|
||||
if (providerId === "9anime" || providerId === "animepahe") {
|
||||
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"));
|
||||
|
||||
if (filteredEpisodesData.length === 0) {
|
||||
return null;
|
||||
}
|
||||
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 async function shouldSkipAnify(aniListId: number): Promise<boolean> {
|
||||
if (!readEnvVariable("ENABLE_ANIFY")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Some mappings on Anify are incorrect so they return episodes from a similar title
|
||||
if (
|
||||
[
|
||||
153406, // Tower of God S2
|
||||
158927, // Spy x Family S2
|
||||
166873, // Mushoku Tensei: Jobless Reincarnation S2 part 2
|
||||
163134, // Re:ZERO -Starting Life in Another World- Season 3
|
||||
163146, // Blue Lock S2
|
||||
].includes(aniListId)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return await getValue("anify_killswitch_till").then((dateTime) => {
|
||||
if (!dateTime) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return DateTime.fromISO(dateTime).diffNow().as("minutes") > 0;
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}[];
|
||||
}
|
||||
@@ -1,12 +1,52 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import { env } from "cloudflare:test";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import app from "~/index";
|
||||
import { server } from "~/mocks";
|
||||
|
||||
server.listen();
|
||||
// Mock useMockData
|
||||
vi.mock("~/libs/useMockData", () => ({ useMockData: () => false }));
|
||||
|
||||
describe('requests the "/episodes/:id/url" route', () => {
|
||||
let app: typeof import("../../../src/index").app;
|
||||
let fetchEpisodes: any;
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.resetModules();
|
||||
|
||||
vi.doMock("../getByAniListId", async (importOriginal) => {
|
||||
const actual = await importOriginal<any>();
|
||||
return {
|
||||
...actual,
|
||||
fetchEpisodes: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
// Mock aniwatch initially as empty mock
|
||||
vi.doMock("./aniwatch", () => ({ getSourcesFromAniwatch: vi.fn() }));
|
||||
|
||||
app = (await import("~/index")).app;
|
||||
fetchEpisodes = (await import("../getByAniListId")).fetchEpisodes;
|
||||
});
|
||||
|
||||
it("with sources from Aniwatch", async () => {
|
||||
vi.mocked(fetchEpisodes).mockResolvedValue([{ id: "ep1", number: 1 }]);
|
||||
|
||||
const mockSource = {
|
||||
source:
|
||||
"https://www032.vipanicdn.net/streamhls/aa804a2400535d84dd59454b28d329fb/ep.1.1712504065.m3u8",
|
||||
subtitles: [],
|
||||
audio: [],
|
||||
};
|
||||
|
||||
// Since controller uses dynamic import, doMock SHOULD affect it if we set it up before the call
|
||||
// Wait, doMock inside test block might be tricky if we don't re-import the module using it?
|
||||
// BUT the controller uses `import("./aniwatch")`, causing a fresh import (if cache invalid?)
|
||||
// Or if `vi.doMock` updates the registry.
|
||||
// In Vitest, doMock updates the registry for NEXT imports.
|
||||
// So `import("./aniwatch")` should pick it up.
|
||||
|
||||
vi.doMock("./aniwatch", () => ({
|
||||
getSourcesFromAniwatch: vi.fn().mockResolvedValue(mockSource),
|
||||
}));
|
||||
|
||||
const response = await app.request(
|
||||
"/episodes/4/url",
|
||||
{
|
||||
@@ -16,32 +56,40 @@ describe('requests the "/episodes/:id/url" route', () => {
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
},
|
||||
{
|
||||
ENABLE_ANIFY: "true",
|
||||
},
|
||||
env,
|
||||
);
|
||||
|
||||
expect(response.json()).resolves.toEqual({
|
||||
const json = await response.json();
|
||||
expect(json).toEqual({
|
||||
success: true,
|
||||
result: {
|
||||
source:
|
||||
"https://www032.vipanicdn.net/streamhls/aa804a2400535d84dd59454b28d329fb/ep.1.1712504065.m3u8",
|
||||
subtitles: [],
|
||||
audio: [],
|
||||
},
|
||||
result: mockSource,
|
||||
});
|
||||
});
|
||||
|
||||
it("with no URL from Aniwatch source", async () => {
|
||||
const response = await app.request("/episodes/-1/url", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
episodeNumber: -1,
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
vi.mocked(fetchEpisodes).mockResolvedValue([{ id: "ep1", number: 1 }]);
|
||||
|
||||
expect(response.json()).resolves.toEqual({ success: false });
|
||||
// Make mock return null
|
||||
vi.doMock("./aniwatch", () => ({
|
||||
getSourcesFromAniwatch: vi.fn().mockResolvedValue(null),
|
||||
}));
|
||||
|
||||
const response = await app.request(
|
||||
"/episodes/4/url",
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
episodeNumber: 1, // Exists in episodes, but source returns null
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
},
|
||||
env,
|
||||
);
|
||||
|
||||
const json = await response.json();
|
||||
expect(json).toEqual({
|
||||
success: false,
|
||||
});
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user