feat: create route to fetch stream URL from provider
Summary: Test Plan:
This commit is contained in:
85
src/controllers/episodes/getEpisodeUrl/index.ts
Normal file
85
src/controllers/episodes/getEpisodeUrl/index.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||
|
||||
import type { Env } from "~/types/env";
|
||||
import {
|
||||
ErrorResponse,
|
||||
ErrorResponseSchema,
|
||||
SuccessResponseSchema,
|
||||
} from "~/types/schema";
|
||||
|
||||
import { getSourcesFromAnify } from "./anify";
|
||||
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: {
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: FetchUrlRequest,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: FetchUrlResponse,
|
||||
},
|
||||
},
|
||||
description: "Returns a stream URL",
|
||||
},
|
||||
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>();
|
||||
|
||||
if (provider === "anify") {
|
||||
try {
|
||||
const result = await 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 });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export default app;
|
||||
Reference in New Issue
Block a user