feat: new version for getEpisodeUrl by just passing the episode number
This commit is contained in:
@@ -2,17 +2,21 @@ import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|||||||
|
|
||||||
import { readEnvVariable } from "~/libs/readEnvVariable";
|
import { readEnvVariable } from "~/libs/readEnvVariable";
|
||||||
import type { Env } from "~/types/env";
|
import type { Env } from "~/types/env";
|
||||||
import {
|
import type { Episode } from "~/types/episode";
|
||||||
FetchUrlResponse,
|
import { FetchUrlResponse } from "~/types/episode/fetch-url-response";
|
||||||
FetchUrlResponseSchema,
|
|
||||||
} from "~/types/episode/fetch-url-response";
|
|
||||||
import {
|
import {
|
||||||
AniListIdQuerySchema,
|
AniListIdQuerySchema,
|
||||||
|
EpisodeNumberSchema,
|
||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
ErrorResponseSchema,
|
ErrorResponseSchema,
|
||||||
} from "~/types/schema";
|
} from "~/types/schema";
|
||||||
|
|
||||||
const FetchUrlRequest = z.object({ id: z.string(), provider: z.string() });
|
import { fetchEpisodesFromAllProviders } from "../getByAniListId";
|
||||||
|
|
||||||
|
const FetchUrlRequest = z.union([
|
||||||
|
z.object({ id: z.string(), provider: z.string() }),
|
||||||
|
z.object({ episodeNumber: EpisodeNumberSchema }),
|
||||||
|
]);
|
||||||
|
|
||||||
const route = createRoute({
|
const route = createRoute({
|
||||||
tags: ["aniplay", "episodes"],
|
tags: ["aniplay", "episodes"],
|
||||||
@@ -68,12 +72,54 @@ const route = createRoute({
|
|||||||
|
|
||||||
const app = new OpenAPIHono<Env>();
|
const app = new OpenAPIHono<Env>();
|
||||||
|
|
||||||
|
export async function fetchEpisodeUrlFromAllProviders(
|
||||||
|
aniListId: number,
|
||||||
|
episodeNumber: number,
|
||||||
|
isAnifyEnabled: boolean,
|
||||||
|
) {
|
||||||
|
const results = await fetchEpisodesFromAllProviders(
|
||||||
|
aniListId,
|
||||||
|
isAnifyEnabled,
|
||||||
|
);
|
||||||
|
if (results.length === 0) {
|
||||||
|
return { episodes: null, fetchUrlResult: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
let episodes: Episode[] | null = 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,
|
||||||
|
isAnifyEnabled,
|
||||||
|
);
|
||||||
|
if (!urlResult) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchUrlResult = urlResult;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { episodes, fetchUrlResult };
|
||||||
|
}
|
||||||
|
|
||||||
export async function fetchEpisodeUrl(
|
export async function fetchEpisodeUrl(
|
||||||
provider: string,
|
provider: string,
|
||||||
id: string,
|
id: string,
|
||||||
aniListId: number,
|
aniListId: number,
|
||||||
isAnifyEnabled: boolean,
|
isAnifyEnabled: boolean,
|
||||||
) {
|
): Promise<FetchUrlResponse | null> {
|
||||||
if (provider === "consumet" || !isAnifyEnabled) {
|
if (provider === "consumet" || !isAnifyEnabled) {
|
||||||
try {
|
try {
|
||||||
const result = await import("./consumet").then(
|
const result = await import("./consumet").then(
|
||||||
@@ -126,16 +172,29 @@ 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 } = await c.req.json<typeof FetchUrlRequest._type>();
|
const { provider, id, episodeNumber } =
|
||||||
|
await c.req.json<typeof FetchUrlRequest._type>();
|
||||||
|
if (!provider && episodeNumber == undefined) {
|
||||||
|
return c.json(ErrorResponse, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const isAnifyEnabled = readEnvVariable<boolean>(c.env, "ENABLE_ANIFY");
|
const isAnifyEnabled = readEnvVariable<boolean>(c.env, "ENABLE_ANIFY");
|
||||||
const result = await fetchEpisodeUrl(
|
let result: FetchUrlResponse | null;
|
||||||
provider,
|
if (provider) {
|
||||||
id,
|
result = await fetchEpisodeUrl(provider, id, aniListId, isAnifyEnabled);
|
||||||
|
} else {
|
||||||
|
const { fetchUrlResult } = await fetchEpisodeUrlFromAllProviders(
|
||||||
aniListId,
|
aniListId,
|
||||||
|
episodeNumber!,
|
||||||
isAnifyEnabled,
|
isAnifyEnabled,
|
||||||
);
|
);
|
||||||
|
if (!fetchUrlResult) {
|
||||||
|
return c.json(ErrorResponse, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
result = fetchUrlResult;
|
||||||
|
}
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
return c.json({ success: true, result });
|
return c.json({ success: true, result });
|
||||||
|
|||||||
Reference in New Issue
Block a user