import { sortByProperty } from "~/libs/sortByProperty"; import type { FetchUrlResponse } from "~/types/episode/fetch-url-response"; import { type SkipTime, convertSkipTime } from "./convertSkipTime"; import { audioPriority, qualityPriority, subtitlesPriority, } from "./priorities"; export async function getSourcesFromAnify( provider: string, watchId: string, aniListId: number, ): Promise { const response = await fetch("https://anify.eltik.cc/sources", { body: JSON.stringify({ watchId, providerId: provider, episodeNumber: "1", id: aniListId.toString(), subType: "sub", }), method: "POST", }).then((res) => res.json() as Promise); 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: convertSkipTime(intro), outro: convertSkipTime(outro), headers: Object.keys(headers ?? {}).length > 0 ? headers : undefined, }; } interface AnifySourcesResponse { sources: VideoSource[]; subtitles: LanguageSource[]; audio: LanguageSource[]; intro: SkipTime; outro: SkipTime; headers?: Record; } interface VideoSource { url: string; quality: string; } interface LanguageSource { url: string; lang: string; }