mirror of
https://github.com/sussy-code/smov.git
synced 2026-04-19 07:32:05 +00:00
53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import { list } from "subsrt-ts";
|
|
|
|
import { proxiedFetch } from "@/backend/helpers/fetch";
|
|
import { convertSubtitlesToSrt } from "@/components/player/utils/captions";
|
|
import { CaptionListItem } from "@/stores/player/slices/source";
|
|
import { SimpleCache } from "@/utils/cache";
|
|
|
|
import {
|
|
isExtensionActiveCached,
|
|
sendExtensionRequest,
|
|
} from "../extension/messaging";
|
|
|
|
export const subtitleTypeList = list().map((type) => `.${type}`);
|
|
const downloadCache = new SimpleCache<string, string>();
|
|
downloadCache.setCompare((a, b) => a === b);
|
|
const expirySeconds = 24 * 60 * 60;
|
|
|
|
/**
|
|
* Always returns SRT
|
|
*/
|
|
export async function downloadCaption(
|
|
caption: CaptionListItem,
|
|
): Promise<string> {
|
|
const cached = downloadCache.get(caption.url);
|
|
if (cached) return cached;
|
|
|
|
let data: string | undefined;
|
|
if (caption.needsProxy) {
|
|
if (isExtensionActiveCached()) {
|
|
const extensionResponse = await sendExtensionRequest({
|
|
url: caption.url,
|
|
method: "GET",
|
|
});
|
|
if (
|
|
!extensionResponse?.success ||
|
|
typeof extensionResponse.response.body !== "string"
|
|
) {
|
|
throw new Error("failed to get caption data from extension");
|
|
}
|
|
|
|
data = extensionResponse.response.body;
|
|
} else {
|
|
data = await proxiedFetch<string>(caption.url, { responseType: "text" });
|
|
}
|
|
} else {
|
|
data = await fetch(caption.url).then((v) => v.text());
|
|
}
|
|
if (!data) throw new Error("failed to get caption data");
|
|
|
|
const output = convertSubtitlesToSrt(data);
|
|
downloadCache.set(caption.url, output, expirySeconds);
|
|
return output;
|
|
}
|