diff --git a/src/providers/captions.ts b/src/providers/captions.ts index 2ee775c..d64dcc2 100644 --- a/src/providers/captions.ts +++ b/src/providers/captions.ts @@ -10,7 +10,6 @@ export type Caption = { type: CaptionType; id: string; // only unique per stream opensubtitles?: boolean; - wyziesubs?: boolean; url: string; hasCorsRestrictions: boolean; language: string; diff --git a/src/utils/opensubtitles.ts b/src/utils/opensubtitles.ts index 116f7ee..6bfcd52 100644 --- a/src/utils/opensubtitles.ts +++ b/src/utils/opensubtitles.ts @@ -1,6 +1,6 @@ /* eslint-disable no-console */ import { ScrapeMedia } from '@/entrypoint/utils/media'; -import { Caption, labelToLanguageCode, removeDuplicatedLanguages } from '@/providers/captions'; +import { Caption, labelToLanguageCode } from '@/providers/captions'; import { IndividualEmbedRunnerOptions } from '@/runners/individualRunner'; import { ProviderRunnerOptions } from '@/runners/runner'; @@ -29,36 +29,33 @@ export async function addOpenSubtitlesCaptions( .map((x, i) => (i === 0 ? x : Number(x) || null)); if (!imdbId) return captions; - // Try Wyzie subs first. 2 second timeout - try { - const wyziePromise = addWyzieCaptions( - [], - ops.media?.tmdbId?.toString() || '', - imdbId.toString(), - typeof season === 'number' ? season : undefined, - typeof episode === 'number' ? episode : undefined, - ); + const allCaptions = [...captions]; - const wyzieCaptions = await Promise.race([wyziePromise, timeout(2000, 'Wyzie')]); - - // If we found Wyzie subs, return them as OpenSubtitles captions - if (wyzieCaptions && wyzieCaptions.length > 0) { - return [ - ...captions, - ...wyzieCaptions.map((caption) => ({ + // Fetch Wyzie captions with 2 second timeout + const wyziePromise = addWyzieCaptions( + [], + ops.media?.tmdbId?.toString() || '', + imdbId.toString(), + typeof season === 'number' ? season : undefined, + typeof episode === 'number' ? episode : undefined, + ) + .then((wyzieCaptions) => { + if (wyzieCaptions && wyzieCaptions.length > 0) { + return wyzieCaptions.map((caption) => ({ ...caption, opensubtitles: true, - })), - ]; - } - } catch (error) { - // Wyzie failed for a reason other than timeout - console.error('Wyzie subtitles fetch failed:', error); - } + })); + } + return []; + }) + .catch((error) => { + console.error('Wyzie subtitles fetch failed:', error); + return []; + }); - // Fall back to OpenSubtitles with a 5 second timeout - try { - const openSubsPromise = ops.proxiedFetcher( + // Fetch OpenSubtitles captions with 5 second timeout + const openSubsPromise = ops + .proxiedFetcher( `https://rest.opensubtitles.org/search/${ season && episode ? `episode-${episode}/` : '' }imdbid-${(imdbId as string).slice(2)}${season && episode ? `/season-${season}` : ''}`, @@ -67,37 +64,41 @@ export async function addOpenSubtitlesCaptions( 'X-User-Agent': 'VLSub 0.10.2', }, }, - ); + ) + .then((Res) => { + 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 openSubtilesCaptions; + }) + .catch((error) => { + console.error('OpenSubtitles fetch failed:', error); + return []; + }); - const Res: { - LanguageName: string; - SubDownloadLink: string; - SubFormat: 'srt' | 'vtt'; - }[] = await Promise.race([openSubsPromise, timeout(5000, 'OpenSubtitles')]); + // Wait for both promises with their respective timeouts + const [wyzieCaptions, openSubsCaptions] = await Promise.all([ + Promise.race([wyziePromise, timeout(2000, 'Wyzie')]), + Promise.race([openSubsPromise, timeout(5000, 'OpenSubtitles')]), + ]); - if (!Res) return captions; // Timeout occurred + // Add any successful captions to our result + if (wyzieCaptions) allCaptions.push(...wyzieCaptions); + if (openSubsCaptions) allCaptions.push(...openSubsCaptions); - 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 (error) { - // OpenSubtitles failed for a reason other than timeout - console.error('OpenSubtitles fetch failed:', error); - return captions; - } + return allCaptions; } catch (error) { console.error('Error in addOpenSubtitlesCaptions:', error); return captions;