37 lines
987 B
TypeScript
37 lines
987 B
TypeScript
import { aniList } from "~/consumet";
|
|
import { sortByProperty } from "~/libs/sortByProperty";
|
|
import type { FetchUrlResponse } from "~/types/episode/fetch-url-response";
|
|
|
|
import { qualityPriority, subtitlesPriority } from "./priorities";
|
|
|
|
export async function getSourcesFromConsumet(
|
|
watchId: string,
|
|
): Promise<FetchUrlResponse | null> {
|
|
try {
|
|
const { sources, subtitles, intro, outro } =
|
|
await aniList.fetchEpisodeSources(watchId);
|
|
|
|
if (sources.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
const source = sources.sort(sortByProperty(qualityPriority, "quality"))[0]
|
|
?.url;
|
|
subtitles?.sort(sortByProperty(subtitlesPriority, "lang"));
|
|
|
|
return {
|
|
source,
|
|
subtitles: subtitles ?? [],
|
|
audio: [],
|
|
intro: intro ? [intro.start, intro.end] : undefined,
|
|
outro: outro ? [outro.start, outro.end] : undefined,
|
|
};
|
|
} catch (error) {
|
|
if (error.message === "Episode not found.") {
|
|
return null;
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
}
|