mirror of
https://github.com/sussy-code/providers.git
synced 2026-04-18 15:22:01 +00:00
49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import { Caption, labelToLanguageCode, removeDuplicatedLanguages } from '@/providers/captions';
|
|
import { IndividualEmbedRunnerOptions } from '@/runners/individualRunner';
|
|
import { ProviderRunnerOptions } from '@/runners/runner';
|
|
|
|
export async function addOpenSubtitlesCaptions(
|
|
captions: Caption[],
|
|
ops: ProviderRunnerOptions | IndividualEmbedRunnerOptions,
|
|
media: string,
|
|
): Promise<Caption[]> {
|
|
try {
|
|
const [imdbId, season, episode] = atob(media)
|
|
.split('.')
|
|
.map((x, i) => (i === 0 ? x : Number(x) || null));
|
|
if (!imdbId) return captions;
|
|
const Res: {
|
|
LanguageName: string;
|
|
SubDownloadLink: string;
|
|
SubFormat: 'srt' | 'vtt';
|
|
}[] = await ops.proxiedFetcher(
|
|
`https://rest.opensubtitles.org/search/${
|
|
season && episode ? `episode-${episode}/` : ''
|
|
}imdbid-${(imdbId as string).slice(2)}${season && episode ? `/season-${season}` : ''}`,
|
|
{
|
|
headers: {
|
|
'X-User-Agent': 'VLSub 0.10.2',
|
|
},
|
|
},
|
|
);
|
|
|
|
const openSubtilesCaptions: Caption[] = [];
|
|
for (const caption of Res) {
|
|
const url = caption.SubDownloadLink.replace('.gz', '').replace('download/', 'download/subencoding-utf8/');
|
|
const language = labelToLanguageCode(caption.LanguageName);
|
|
if (!url || !language) continue;
|
|
else
|
|
openSubtilesCaptions.push({
|
|
id: url,
|
|
opensubtitles: true,
|
|
url,
|
|
type: caption.SubFormat || 'srt',
|
|
hasCorsRestrictions: false,
|
|
language,
|
|
});
|
|
}
|
|
return [...captions, ...removeDuplicatedLanguages(openSubtilesCaptions)];
|
|
} catch {
|
|
return captions;
|
|
}
|
|
}
|