fix(aniplay): Migrates to Aniwatch only
Removes Consumet and consolidates episode fetching to use Aniwatch as the sole provider. This simplifies the codebase and ensures a consistent data source for episodes and URLs. Also updates `wrangler` and sets `ENABLE_ANIFY` to false.
This commit is contained in:
@@ -49,7 +49,7 @@
|
|||||||
"prettier-plugin-toml": "^2.0.4",
|
"prettier-plugin-toml": "^2.0.4",
|
||||||
"ts-morph": "^22.0.0",
|
"ts-morph": "^22.0.0",
|
||||||
"typescript": "^5.8.3",
|
"typescript": "^5.8.3",
|
||||||
"wrangler": "^4.13.0",
|
"wrangler": "^4.28.1",
|
||||||
"zx": "8.1.5"
|
"zx": "8.1.5"
|
||||||
},
|
},
|
||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
|
|||||||
@@ -4,11 +4,10 @@ import { streamSSE } from "hono/streaming";
|
|||||||
|
|
||||||
import { fetchEpisodes } from "~/controllers/episodes/getByAniListId";
|
import { fetchEpisodes } from "~/controllers/episodes/getByAniListId";
|
||||||
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
|
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
|
||||||
import { sleep } from "~/libs/sleep";
|
|
||||||
import { associateDeviceIdWithUsername } from "~/models/token";
|
import { associateDeviceIdWithUsername } from "~/models/token";
|
||||||
import { setWatchStatus } from "~/models/watchStatus";
|
import { setWatchStatus } from "~/models/watchStatus";
|
||||||
import type { Env } from "~/types/env";
|
import type { Env } from "~/types/env";
|
||||||
import { Episode, EpisodesResponseSchema } from "~/types/episode";
|
import { EpisodesResponseSchema } from "~/types/episode";
|
||||||
import { ErrorResponse, ErrorResponseSchema } from "~/types/schema";
|
import { ErrorResponse, ErrorResponseSchema } from "~/types/schema";
|
||||||
import { Title } from "~/types/title";
|
import { Title } from "~/types/title";
|
||||||
|
|
||||||
@@ -186,13 +185,8 @@ app.openapi(route, async (c) => {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
await fetchEpisodes(
|
await fetchEpisodes(media.id, true).then(({ episodes }) => {
|
||||||
media.id,
|
if (episodes.length === 0) {
|
||||||
{ ...env(c, "workerd"), ENABLE_ANIFY: "false" },
|
|
||||||
true,
|
|
||||||
).then(({ result: episodesResult }) => {
|
|
||||||
const episodes = episodesResult?.episodes;
|
|
||||||
if (!episodes) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -97,12 +97,12 @@ async function fetchEpisodes(
|
|||||||
.then(
|
.then(
|
||||||
(res) =>
|
(res) =>
|
||||||
res.json() as Promise<{
|
res.json() as Promise<{
|
||||||
success: boolean;
|
status: number;
|
||||||
data: AniwatchEpisodesResponse;
|
data: AniwatchEpisodesResponse;
|
||||||
}>,
|
}>,
|
||||||
)
|
)
|
||||||
.then(({ success, data }) => {
|
.then(({ status, data }) => {
|
||||||
if (!success || data.totalEpisodes === 0) {
|
if (status >= 300 || data.totalEpisodes === 0) {
|
||||||
console.error(
|
console.error(
|
||||||
`Error trying to load episodes from aniwatch; aniListId: ${aniListId}, totalEpisodes: ${data.totalEpisodes}`,
|
`Error trying to load episodes from aniwatch; aniListId: ${aniListId}, totalEpisodes: ${data.totalEpisodes}`,
|
||||||
);
|
);
|
||||||
@@ -164,12 +164,12 @@ function getAniwatchId(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const json = (await res.value.json()) as {
|
const json = (await res.value.json()) as {
|
||||||
success: boolean;
|
status: number;
|
||||||
data: AniwatchSearchResponse;
|
data: AniwatchSearchResponse;
|
||||||
};
|
};
|
||||||
const currentValue = await current;
|
const currentValue = await current;
|
||||||
return {
|
return {
|
||||||
success: currentValue.success || json.success,
|
success: currentValue.success || json.status === 200,
|
||||||
data: {
|
data: {
|
||||||
...currentValue.data,
|
...currentValue.data,
|
||||||
animes: [
|
animes: [
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
import { aniList } from "~/consumet";
|
|
||||||
|
|
||||||
import { Episode, EpisodesResponse } from "./episode";
|
|
||||||
|
|
||||||
export async function getEpisodesFromConsumet(
|
|
||||||
aniListId: number,
|
|
||||||
): Promise<EpisodesResponse | null> {
|
|
||||||
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(
|
|
||||||
`Error trying to load episodes from consumet; aniListId: ${aniListId}`,
|
|
||||||
);
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@@ -1,17 +1,13 @@
|
|||||||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||||
import { env } from "hono/adapter";
|
|
||||||
|
|
||||||
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
|
|
||||||
import type { Env } from "~/types/env";
|
import type { Env } from "~/types/env";
|
||||||
import { EpisodesResponse, EpisodesResponseSchema } from "~/types/episode";
|
import { EpisodesResponseSchema } from "~/types/episode";
|
||||||
import {
|
import {
|
||||||
AniListIdQuerySchema,
|
AniListIdQuerySchema,
|
||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
ErrorResponseSchema,
|
ErrorResponseSchema,
|
||||||
} from "~/types/schema";
|
} from "~/types/schema";
|
||||||
|
|
||||||
import { getEpisodesFromAnify } from "./anify";
|
|
||||||
|
|
||||||
const route = createRoute({
|
const route = createRoute({
|
||||||
tags: ["aniplay", "episodes"],
|
tags: ["aniplay", "episodes"],
|
||||||
summary: "Fetch episodes for a title",
|
summary: "Fetch episodes for a title",
|
||||||
@@ -43,61 +39,25 @@ const route = createRoute({
|
|||||||
|
|
||||||
const app = new OpenAPIHono<Env>();
|
const app = new OpenAPIHono<Env>();
|
||||||
|
|
||||||
export function fetchEpisodesFromAllProviders(
|
export function fetchEpisodes(aniListId: number, shouldRetry: boolean = false) {
|
||||||
aniListId: number,
|
return import("./aniwatch")
|
||||||
env: Env,
|
.then(({ getEpisodesFromAniwatch }) =>
|
||||||
): Promise<EpisodesResponse[]> {
|
getEpisodesFromAniwatch(aniListId, shouldRetry),
|
||||||
return Promise.allSettled([
|
)
|
||||||
import("./aniwatch").then(({ getEpisodesFromAniwatch }) =>
|
.then((episodeResults) => episodeResults?.episodes ?? []);
|
||||||
getEpisodesFromAniwatch(aniListId),
|
|
||||||
),
|
|
||||||
getEpisodesFromAnify(env, aniListId),
|
|
||||||
]).then((episodeResults) =>
|
|
||||||
episodeResults
|
|
||||||
.filter((result) => result.status === "fulfilled")
|
|
||||||
.map((result) => result.value)
|
|
||||||
.filter((episodes) => !!episodes),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function fetchEpisodes(
|
|
||||||
aniListId: number,
|
|
||||||
env: Env,
|
|
||||||
shouldRetry: boolean = false,
|
|
||||||
) {
|
|
||||||
return fetchFromMultipleSources([
|
|
||||||
() =>
|
|
||||||
import("./aniwatch").then(({ getEpisodesFromAniwatch }) =>
|
|
||||||
getEpisodesFromAniwatch(aniListId, shouldRetry),
|
|
||||||
),
|
|
||||||
() => getEpisodesFromAnify(env, aniListId),
|
|
||||||
// () =>
|
|
||||||
// import("./consumet").then(({ getEpisodesFromConsumet }) =>
|
|
||||||
// getEpisodesFromConsumet(aniListId),
|
|
||||||
// ),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.openapi(route, async (c) => {
|
app.openapi(route, async (c) => {
|
||||||
const aniListId = Number(c.req.param("aniListId"));
|
const aniListId = Number(c.req.param("aniListId"));
|
||||||
|
|
||||||
const { result, errorOccurred } = await fetchEpisodes(
|
const episodes = await fetchEpisodes(aniListId);
|
||||||
aniListId,
|
if (episodes.length === 0) {
|
||||||
env(c, "workerd"),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (errorOccurred || !result) {
|
|
||||||
return c.json(ErrorResponse, { status: 500 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const { episodes, providerId } = result;
|
|
||||||
if (!episodes || episodes.length === 0) {
|
|
||||||
return c.json(ErrorResponse, { status: 404 });
|
return c.json(ErrorResponse, { status: 404 });
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
success: true,
|
success: true,
|
||||||
result: { providerId, episodes },
|
result: { providerId: "aniwatch", episodes },
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -36,12 +36,12 @@ async function getEpisodeUrl(watchId: string, server?: string) {
|
|||||||
.then(
|
.then(
|
||||||
(res) =>
|
(res) =>
|
||||||
res.json() as Promise<{
|
res.json() as Promise<{
|
||||||
success: boolean;
|
status: number;
|
||||||
data: AniwatchEpisodeUrlResponse;
|
data: AniwatchEpisodeUrlResponse;
|
||||||
}>,
|
}>,
|
||||||
)
|
)
|
||||||
.then(({ success, data }) => {
|
.then(({ status, data }) => {
|
||||||
if (!success || !data.sources || data.sources.length === 0) {
|
if (status >= 300 || !data.sources || data.sources.length === 0) {
|
||||||
return { source: null };
|
return { source: null };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,9 +50,10 @@ async function getEpisodeUrl(watchId: string, server?: string) {
|
|||||||
intro: convertSkipTime(intro),
|
intro: convertSkipTime(intro),
|
||||||
outro: convertSkipTime(outro),
|
outro: convertSkipTime(outro),
|
||||||
source: sources[0].url,
|
source: sources[0].url,
|
||||||
subtitles: tracks
|
subtitles: tracks.map(({ url, lang }) => ({
|
||||||
.filter(({ kind }) => kind === "captions")
|
url,
|
||||||
.map(({ file, label }) => ({ url: file, lang: label ?? "" })),
|
lang,
|
||||||
|
})),
|
||||||
headers,
|
headers,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -79,8 +80,8 @@ async function getEpisodeServers(watchId: string) {
|
|||||||
)
|
)
|
||||||
.then((res) => res.json() as Promise<AniwatchEpisodeServersResponse>)
|
.then((res) => res.json() as Promise<AniwatchEpisodeServersResponse>)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (!res.success) {
|
if (res.status >= 300 || !res.data) {
|
||||||
throw new Error(res.message);
|
throw new Error("Failed to fetch episode servers");
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
@@ -105,21 +106,16 @@ interface Source {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Track {
|
interface Track {
|
||||||
file: string;
|
url: string;
|
||||||
label?: string;
|
lang?: string;
|
||||||
kind: string;
|
kind: string;
|
||||||
default?: boolean;
|
default?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
type AniwatchEpisodeServersResponse =
|
interface AniwatchEpisodeServersResponse {
|
||||||
| {
|
status: number;
|
||||||
success: true;
|
data: AniwatchEpisodeServers;
|
||||||
data: AniwatchEpisodeServers;
|
}
|
||||||
}
|
|
||||||
| {
|
|
||||||
success: false;
|
|
||||||
message: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
interface AniwatchEpisodeServers {
|
interface AniwatchEpisodeServers {
|
||||||
sub: AniwatchEpisodeServer[];
|
sub: AniwatchEpisodeServer[];
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
import { aniList } from "~/consumet";
|
|
||||||
import { sortByProperty } from "~/libs/sortByProperty";
|
|
||||||
import type { FetchUrlResponse } from "~/types/episode/fetch-url-response";
|
|
||||||
|
|
||||||
import { qualityPriority, subtitlesPriority } from "./priorities";
|
|
||||||
|
|
||||||
export async function getSourcesFromConsumet(
|
|
||||||
watchId: string,
|
|
||||||
): Promise<FetchUrlResponse | null> {
|
|
||||||
try {
|
|
||||||
const { sources, subtitles, intro, outro } =
|
|
||||||
await aniList.fetchEpisodeSources(watchId);
|
|
||||||
|
|
||||||
if (sources.length === 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const source = sources.sort(sortByProperty(qualityPriority, "quality"))[0]
|
|
||||||
?.url;
|
|
||||||
subtitles?.sort(sortByProperty(subtitlesPriority, "lang"));
|
|
||||||
|
|
||||||
return {
|
|
||||||
source,
|
|
||||||
subtitles: subtitles ?? [],
|
|
||||||
audio: [],
|
|
||||||
intro: intro ? [intro.start, intro.end] : undefined,
|
|
||||||
outro: outro ? [outro.start, outro.end] : undefined,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
if (error.message === "Episode not found.") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,14 +6,13 @@ import { server } from "~/mocks";
|
|||||||
server.listen();
|
server.listen();
|
||||||
|
|
||||||
describe('requests the "/episodes/:id/url" route', () => {
|
describe('requests the "/episodes/:id/url" route', () => {
|
||||||
it("with sources from Anify", async () => {
|
it("with sources from Aniwatch", async () => {
|
||||||
const response = await app.request(
|
const response = await app.request(
|
||||||
"/episodes/1/url",
|
"/episodes/4/url",
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
provider: "anify",
|
episodeNumber: 1,
|
||||||
id: "/ore-dake-level-up-na-ken-episode-2",
|
|
||||||
}),
|
}),
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
},
|
||||||
@@ -26,19 +25,18 @@ describe('requests the "/episodes/:id/url" route', () => {
|
|||||||
success: true,
|
success: true,
|
||||||
result: {
|
result: {
|
||||||
source:
|
source:
|
||||||
"https://proxy.anify.tv/video/8CLGIJg8G3k%252BH%252BYV9xyOYVGZ8al8uZqqtbXk44wKRco%252BGATkCrqlkgdRiam3owmOU4f2MAB89GOblOuZbxifwbGsjvp32uxhRC4kZVYrWnZmP%252FrLxtqwi0n6zY%252BvrffUh6dbg6DADSLCWhd2bNUUIg%253D%253D/%7B%7D/.m3u8",
|
"https://www032.vipanicdn.net/streamhls/aa804a2400535d84dd59454b28d329fb/ep.1.1712504065.m3u8",
|
||||||
subtitles: [],
|
subtitles: [],
|
||||||
audio: [],
|
audio: [],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("with no URL from Anify source", async () => {
|
it("with no URL from Aniwatch source", async () => {
|
||||||
const response = await app.request("/episodes/-1/url", {
|
const response = await app.request("/episodes/-1/url", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
provider: "anify",
|
episodeNumber: -1,
|
||||||
id: "/ore-dake-level-up-na-ken-episode-2",
|
|
||||||
}),
|
}),
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
@@ -46,85 +44,4 @@ describe('requests the "/episodes/:id/url" route', () => {
|
|||||||
expect(response.json()).resolves.toEqual({ success: false });
|
expect(response.json()).resolves.toEqual({ success: false });
|
||||||
expect(response.status).toBe(404);
|
expect(response.status).toBe(404);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("with sources from Consumet", async () => {
|
|
||||||
const response = await app.request(
|
|
||||||
"/episodes/1/url",
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
body: JSON.stringify({
|
|
||||||
provider: "consumet",
|
|
||||||
id: "/ore-dake-level-up-na-ken-episode-2",
|
|
||||||
}),
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ENABLE_ANIFY: "true",
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(response.json()).resolves.toEqual({
|
|
||||||
success: true,
|
|
||||||
result: {
|
|
||||||
source: "https://consumet.com",
|
|
||||||
subtitles: [],
|
|
||||||
audio: [],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("with no URL from Consumet source", async () => {
|
|
||||||
const response = await app.request("/episodes/-1/url", {
|
|
||||||
method: "POST",
|
|
||||||
body: JSON.stringify({
|
|
||||||
provider: "consumet",
|
|
||||||
id: "unknown",
|
|
||||||
}),
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(response.json()).resolves.toEqual({ success: false });
|
|
||||||
expect(response.status).toBe(404);
|
|
||||||
});
|
|
||||||
|
|
||||||
// it("with sources from Aniwatch", async () => {
|
|
||||||
// const response = await app.request(
|
|
||||||
// "/episodes/1/url",
|
|
||||||
// {
|
|
||||||
// method: "POST",
|
|
||||||
// body: JSON.stringify({
|
|
||||||
// provider: "aniwatch",
|
|
||||||
// id: "ore-dake-level-up-na-ken-episode-2",
|
|
||||||
// }),
|
|
||||||
// headers: { "Content-Type": "application/json" },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// ENABLE_ANIFY: "true",
|
|
||||||
// },
|
|
||||||
// );
|
|
||||||
|
|
||||||
// expect(response.json()).resolves.toEqual({
|
|
||||||
// success: true,
|
|
||||||
// result: {
|
|
||||||
// source:
|
|
||||||
// "https://www032.vipanicdn.net/streamhls/aa804a2400535d84dd59454b28d329fb/ep.1.1712504065.m3u8",
|
|
||||||
// subtitles: [],
|
|
||||||
// audio: [],
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
|
|
||||||
// it("with no URL from Aniwatch source", async () => {
|
|
||||||
// const response = await app.request("/episodes/-1/url", {
|
|
||||||
// method: "POST",
|
|
||||||
// body: JSON.stringify({
|
|
||||||
// provider: "aniwatch",
|
|
||||||
// id: "unknown",
|
|
||||||
// }),
|
|
||||||
// headers: { "Content-Type": "application/json" },
|
|
||||||
// });
|
|
||||||
|
|
||||||
// expect(response.json()).resolves.toEqual({ success: false });
|
|
||||||
// expect(response.status).toBe(404);
|
|
||||||
// });
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||||
import { env } from "hono/adapter";
|
|
||||||
|
|
||||||
import { readEnvVariable } from "~/libs/readEnvVariable";
|
|
||||||
import type { Env } from "~/types/env";
|
import type { Env } from "~/types/env";
|
||||||
import type { Episode } from "~/types/episode";
|
|
||||||
import { FetchUrlResponse } from "~/types/episode/fetch-url-response";
|
import { FetchUrlResponse } from "~/types/episode/fetch-url-response";
|
||||||
import {
|
import {
|
||||||
AniListIdQuerySchema,
|
AniListIdQuerySchema,
|
||||||
@@ -12,12 +9,9 @@ import {
|
|||||||
ErrorResponseSchema,
|
ErrorResponseSchema,
|
||||||
} from "~/types/schema";
|
} from "~/types/schema";
|
||||||
|
|
||||||
import { fetchEpisodesFromAllProviders } from "../getByAniListId";
|
import { fetchEpisodes } from "../getByAniListId";
|
||||||
|
|
||||||
const FetchUrlRequest = z.union([
|
const FetchUrlRequest = z.object({ episodeNumber: EpisodeNumberSchema });
|
||||||
z.object({ id: z.string(), provider: z.string() }),
|
|
||||||
z.object({ episodeNumber: EpisodeNumberSchema }),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const route = createRoute({
|
const route = createRoute({
|
||||||
tags: ["aniplay", "episodes"],
|
tags: ["aniplay", "episodes"],
|
||||||
@@ -73,89 +67,40 @@ const route = createRoute({
|
|||||||
|
|
||||||
const app = new OpenAPIHono<Env>();
|
const app = new OpenAPIHono<Env>();
|
||||||
|
|
||||||
export async function fetchEpisodeUrlFromAllProviders(
|
export async function fetchEpisodeUrl({
|
||||||
aniListId: number,
|
id,
|
||||||
episodeNumber: number,
|
aniListId,
|
||||||
env: Env,
|
episodeNumber,
|
||||||
) {
|
}:
|
||||||
const results = await fetchEpisodesFromAllProviders(aniListId, env);
|
| { id: string; aniListId?: number; episodeNumber?: number }
|
||||||
if (results.length === 0) {
|
| {
|
||||||
return { episodes: null, fetchUrlResult: null };
|
id?: string;
|
||||||
}
|
aniListId: number;
|
||||||
|
episodeNumber: number;
|
||||||
let episodes: Episode[] | null = null;
|
}): Promise<FetchUrlResponse | null> {
|
||||||
let fetchUrlResult: FetchUrlResponse | null = null;
|
|
||||||
|
|
||||||
for (const { episodes: episodesResult, providerId } of results) {
|
|
||||||
const episode = episodesResult.find(
|
|
||||||
(episode) => episode.number === episodeNumber,
|
|
||||||
);
|
|
||||||
if (!episode) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
episodes = episodesResult;
|
|
||||||
|
|
||||||
const urlResult = await fetchEpisodeUrl(
|
|
||||||
providerId,
|
|
||||||
episode.id,
|
|
||||||
aniListId,
|
|
||||||
readEnvVariable(env, "ENABLE_ANIFY"),
|
|
||||||
);
|
|
||||||
if (!urlResult) {
|
|
||||||
episodes = null;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
fetchUrlResult = urlResult;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return { episodes, fetchUrlResult };
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function fetchEpisodeUrl(
|
|
||||||
provider: string,
|
|
||||||
id: string,
|
|
||||||
aniListId: number,
|
|
||||||
isAnifyEnabled: boolean,
|
|
||||||
): Promise<FetchUrlResponse | null> {
|
|
||||||
if (provider === "consumet" || !isAnifyEnabled) {
|
|
||||||
try {
|
|
||||||
const result = await import("./consumet").then(
|
|
||||||
({ getSourcesFromConsumet }) => getSourcesFromConsumet(id),
|
|
||||||
);
|
|
||||||
if (!result) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
} catch (e) {
|
|
||||||
console.error("Failed to fetch download URL from Consumet", e);
|
|
||||||
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (provider === "aniwatch") {
|
|
||||||
try {
|
|
||||||
const result = await import("./aniwatch").then(
|
|
||||||
({ getSourcesFromAniwatch }) => getSourcesFromAniwatch(id),
|
|
||||||
);
|
|
||||||
if (!result) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
} catch (e) {
|
|
||||||
console.error("Failed to fetch download URL from Aniwatch", e);
|
|
||||||
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await import("./anify").then(({ getSourcesFromAnify }) =>
|
let episodeId = id;
|
||||||
getSourcesFromAnify(provider, id, aniListId),
|
if (!id) {
|
||||||
|
const episodes = await fetchEpisodes(aniListId!);
|
||||||
|
if (episodes.length === 0) {
|
||||||
|
console.error(`Failed to fetch episodes for title ${aniListId}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const episode = episodes.find(
|
||||||
|
(episode) => episode.number === episodeNumber,
|
||||||
|
);
|
||||||
|
if (!episode) {
|
||||||
|
console.error(
|
||||||
|
`Episode ${episodeNumber} not found for title ${aniListId}`,
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
episodeId = episode.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await import("./aniwatch").then(
|
||||||
|
({ getSourcesFromAniwatch }) => getSourcesFromAniwatch(episodeId!),
|
||||||
);
|
);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return null;
|
return null;
|
||||||
@@ -163,7 +108,7 @@ export async function fetchEpisodeUrl(
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Failed to fetch download URL from Anify", e);
|
console.error("Failed to fetch download URL from Aniwatch", e);
|
||||||
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
@@ -171,37 +116,21 @@ export async function fetchEpisodeUrl(
|
|||||||
|
|
||||||
app.openapi(route, async (c) => {
|
app.openapi(route, async (c) => {
|
||||||
const aniListId = Number(c.req.param("aniListId"));
|
const aniListId = Number(c.req.param("aniListId"));
|
||||||
const { provider, id, episodeNumber } =
|
const { episodeNumber } = await c.req.json<typeof FetchUrlRequest._type>();
|
||||||
await c.req.json<typeof FetchUrlRequest._type>();
|
if (episodeNumber == undefined) {
|
||||||
if (!provider && episodeNumber == undefined) {
|
|
||||||
return c.json(ErrorResponse, { status: 400 });
|
return c.json(ErrorResponse, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const isAnifyEnabled = readEnvVariable<boolean>(c.env, "ENABLE_ANIFY");
|
console.log(
|
||||||
let result: FetchUrlResponse | null;
|
`Fetching episode URL for aniListId: ${aniListId}, episodeNumber: ${episodeNumber}`,
|
||||||
if (provider) {
|
);
|
||||||
console.log(`Fetching sources from ${provider} for ${aniListId}`);
|
const fetchUrlResult = await fetchEpisodeUrl({ aniListId, episodeNumber });
|
||||||
result = await fetchEpisodeUrl(provider, id, aniListId, isAnifyEnabled);
|
if (!fetchUrlResult) {
|
||||||
} else {
|
|
||||||
console.log(`Fetching sources from all providers for ${aniListId}`);
|
|
||||||
const { fetchUrlResult } = await fetchEpisodeUrlFromAllProviders(
|
|
||||||
aniListId,
|
|
||||||
episodeNumber!,
|
|
||||||
env(c, "workerd"),
|
|
||||||
);
|
|
||||||
if (!fetchUrlResult) {
|
|
||||||
return c.json(ErrorResponse, { status: 404 });
|
|
||||||
}
|
|
||||||
|
|
||||||
result = fetchUrlResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result) {
|
|
||||||
return c.json({ success: true, result });
|
|
||||||
} else {
|
|
||||||
return c.json(ErrorResponse, { status: 404 });
|
return c.json(ErrorResponse, { status: 404 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return c.json({ success: true, result: fetchUrlResult });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return c.json(ErrorResponse, { status: 500 });
|
return c.json(ErrorResponse, { status: 500 });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Hono } from "hono";
|
|||||||
import { env } from "hono/adapter";
|
import { env } from "hono/adapter";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { fetchEpisodeUrlFromAllProviders } from "~/controllers/episodes/getEpisodeUrl";
|
import { fetchEpisodeUrl } from "~/controllers/episodes/getEpisodeUrl";
|
||||||
import { getAdminSdkCredentials } from "~/libs/gcloud/getAdminSdkCredentials";
|
import { getAdminSdkCredentials } from "~/libs/gcloud/getAdminSdkCredentials";
|
||||||
import { sendFcmMessage } from "~/libs/gcloud/sendFcmMessage";
|
import { sendFcmMessage } from "~/libs/gcloud/sendFcmMessage";
|
||||||
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
|
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
|
||||||
@@ -44,20 +44,7 @@ app.post(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { episodes, fetchUrlResult } = await fetchEpisodeUrlFromAllProviders(
|
const fetchUrlResult = await fetchEpisodeUrl({ aniListId, episodeNumber });
|
||||||
aniListId,
|
|
||||||
episodeNumber,
|
|
||||||
env<Env, typeof c>(c, "workerd"),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!episodes) {
|
|
||||||
console.error(`Failed to fetch episodes for title ${aniListId}`);
|
|
||||||
return c.json(
|
|
||||||
{ success: false, message: "Failed to fetch episodes" },
|
|
||||||
500,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fetchUrlResult) {
|
if (!fetchUrlResult) {
|
||||||
console.error(`Failed to fetch episode URL for episode`);
|
console.error(`Failed to fetch episode URL for episode`);
|
||||||
return c.json(
|
return c.json(
|
||||||
|
|||||||
@@ -66,3 +66,10 @@ app.doc("/openapi.json", {
|
|||||||
app.get("/docs", swaggerUI({ url: "/openapi.json" }));
|
app.get("/docs", swaggerUI({ url: "/openapi.json" }));
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|
||||||
|
export class AniwatchApiContainer /* extends Container */ {
|
||||||
|
// Port the container listens on (default: 8080)
|
||||||
|
defaultPort = 4444;
|
||||||
|
// Time before container sleeps due to inactivity (default: 30s)
|
||||||
|
sleepAfter = "2m";
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ compatibility_date = "2024-09-23"
|
|||||||
|
|
||||||
[vars]
|
[vars]
|
||||||
TURSO_URL = "libsql://aniplay-v2-silverandroid.turso.io"
|
TURSO_URL = "libsql://aniplay-v2-silverandroid.turso.io"
|
||||||
ENABLE_ANIFY = true
|
ENABLE_ANIFY = false
|
||||||
|
|
||||||
[observability]
|
[observability]
|
||||||
enabled = true
|
enabled = true
|
||||||
|
|||||||
Reference in New Issue
Block a user