Files
aniplay-api/src/controllers/episodes/getEpisodeUrl/index.ts

141 lines
3.4 KiB
TypeScript

import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { readEnvVariable } from "~/libs/readEnvVariable";
import type { Env } from "~/types/env";
import {
AniListIdQuerySchema,
ErrorResponse,
ErrorResponseSchema,
SuccessResponseSchema,
} from "~/types/schema";
import { FetchUrlResponse as FetchUrlResponseSchema } from "./responseType";
const FetchUrlRequest = z.object({ id: z.string(), provider: z.string() });
const FetchUrlResponse = SuccessResponseSchema(FetchUrlResponseSchema);
const route = createRoute({
tags: ["aniplay", "episodes"],
summary: "Fetch stream URL for an episode",
operationId: "fetchStreamUrl",
method: "post",
path: "/{aniListId}/url",
request: {
params: z.object({ aniListId: AniListIdQuerySchema }),
body: {
content: {
"application/json": {
schema: FetchUrlRequest,
},
},
},
},
responses: {
200: {
content: {
"application/json": {
schema: FetchUrlResponse,
},
},
description: "Returns a stream URL",
},
400: {
content: {
"application/json": {
schema: ErrorResponseSchema,
},
},
description: "Unknown provider",
},
404: {
content: {
"application/json": {
schema: ErrorResponseSchema,
},
},
description: "Provider did not return a source",
},
500: {
content: {
"application/json": {
schema: ErrorResponseSchema,
},
},
description: "Failed to fetch stream URL from provider",
},
},
});
const app = new OpenAPIHono<Env>();
app.openapi(route, async (c) => {
const aniListId = Number(c.req.param("aniListId"));
const { provider, id } = await c.req.json<typeof FetchUrlRequest._type>();
const isAnifyEnabled = readEnvVariable(c.env, "ENABLE_ANIFY");
if (provider === "consumet" || !isAnifyEnabled) {
try {
const result = await import("./consumet").then(
({ getSourcesFromConsumet }) => getSourcesFromConsumet(id),
);
if (!result) {
return c.json({ success: false }, { status: 404 });
}
return c.json({
success: true,
result,
});
} catch (e) {
console.error("Failed to fetch download URL from Consumet", e);
return c.json(ErrorResponse, { status: 500 });
}
}
if (provider === "anify") {
try {
const result = await import("./anify").then(({ getSourcesFromAnify }) =>
getSourcesFromAnify(provider, id, aniListId),
);
if (!result) {
return c.json({ success: false }, { status: 404 });
}
return c.json({
success: true,
result,
});
} catch (e) {
console.error("Failed to fetch download URL from Anify", e);
return c.json(ErrorResponse, { status: 500 });
}
}
if (provider === "amvstrm") {
try {
const result = await import("./amvstrm").then(
({ getSourcesFromAmvstrm }) => getSourcesFromAmvstrm(id),
);
if (!result) {
return c.json({ success: false }, { status: 404 });
}
return c.json({
success: true,
result,
});
} catch (e) {
console.error("Failed to fetch download URL from Amvstrm", e);
return c.json(ErrorResponse, { status: 500 });
}
}
return c.json(ErrorResponse, { status: 400 });
});
export default app;