feat: create route to fetch stream URL from provider

Summary:

Test Plan:
This commit is contained in:
2024-05-30 08:44:20 -04:00
parent 63cb1b26d9
commit 7aab9a19ec
10 changed files with 302 additions and 1 deletions

View File

@@ -0,0 +1,74 @@
import { sortByProperty } from "~/libs/sortByProperty";
import {
audioPriority,
qualityPriority,
subtitlesPriority,
} from "./priorities";
import type { FetchUrlResponse } from "./responseType";
export async function getSourcesFromAnify(
provider: string,
watchId: string,
aniListId: number,
): Promise<FetchUrlResponse | null> {
const response = await fetch("https://api.anify.tv/sources", {
body: JSON.stringify({
watchId,
providerId: provider,
episodeNumber: "1",
id: aniListId.toString(),
subType: "sub",
}),
method: "POST",
}).then((res) => res.json() as Promise<AnifySourcesResponse>);
const { sources, subtitles, audio, intro, outro, headers } = response;
if (!sources || sources.length === 0) {
return null;
}
const source = sources.sort(sortByProperty(qualityPriority, "quality"))[0]
?.url;
subtitles?.sort(sortByProperty(subtitlesPriority, "lang"));
audio?.sort(sortByProperty(audioPriority, "lang"));
return {
source,
audio,
subtitles,
intro:
typeof intro?.start === "number" && typeof intro?.end === "number"
? [intro.start, intro.end].map((seconds) => Math.floor(seconds))
: undefined,
outro:
typeof outro?.start === "number" && typeof outro?.end === "number"
? [outro.start, outro.end].map((seconds) => Math.floor(seconds))
: undefined,
headers: Object.keys(headers ?? {}).length > 0 ? headers : undefined,
};
}
interface AnifySourcesResponse {
sources: VideoSource[];
subtitles: LanguageSource[];
audio: LanguageSource[];
intro: SkipTime;
outro: SkipTime;
headers?: Record<string, string>;
}
interface SkipTime {
start: number;
end: number;
}
interface VideoSource {
url: string;
quality: string;
}
interface LanguageSource {
url: string;
lang: string;
}