feat(player): external subtitles via OpenSubtitles

Add an OpenSubtitles client, /api/subtitles routes and useSubtitles to
fetch subtitles by show/episode/filename, wire them as player tracks
alongside embedded ones, and strip hls.js's auto-added tracks. New
OPENSUBTITLES_* / SUBTITLE_LANGUAGES env vars (documented in env.example).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
nerandClaude Opus 4.8 committed 2026-08-08 13:17:12 +03:00
1 parent c695697c4f
commit ecc57ff135
24 files changed
+372 -67

No files matched your search

-5
View File
@@ -3,11 +3,6 @@ import { NextRequest, NextResponse } from 'next/server';
import tlk from '@/data/parsers/tlk';
import { STREAM_URL } from '@/env';
// Public proxy the TV calls to re-pull a Toloka thread's CURRENT magnet.
// Given a Toloka topic/thread id (the `id` in download.php?id=<id>), it
// re-downloads the thread's .torrent server-side (auth handled by the Toloka
// client) and returns a fresh magnet + the derived stream m3u. The TV holds
// only the numeric id — never Toloka's authenticated URL.
export async function GET({ nextUrl: { searchParams } }: NextRequest) {
const tid = searchParams.get('tid');
+39
View File
@@ -0,0 +1,39 @@
import { NextResponse } from 'next/server';
import { downloadSubtitle } from '@/data/clients/openSubtitles';
import redis from '@/data/clients/redis';
type SubtitleParams = {
params: {
fileId: string;
};
};
const CACHE_TTL = 60 * 60 * 24 * 30;
export async function GET(_: Request, { params: { fileId } }: SubtitleParams) {
const id = fileId.replace(/\.vtt$/i, '');
if (!/^\d+$/.test(id)) {
return NextResponse.json({ error: 'Invalid subtitle file id' }, { status: 400 });
}
const key = `subtitles:${id}`;
try {
const cached = await redis?.get<string>(key).catch(() => null);
const content = cached ?? (await downloadSubtitle(Number(id)));
if (!cached) await redis?.set(key, content, { ex: CACHE_TTL }).catch(() => null);
return new NextResponse(content, {
headers: {
'Content-Type': 'text/vtt; charset=utf-8',
'Cache-Control': 'public, max-age=31536000, immutable'
}
});
} catch (error) {
console.error('[subtitles] download', error);
return NextResponse.json({ error: 'Failed to download subtitle' }, { status: 502 });
}
}
+34
View File
@@ -0,0 +1,34 @@
import { NextRequest, NextResponse } from 'next/server';
import { searchSubtitles } from '@/data/clients/openSubtitles';
import { SUBTITLE_LANGUAGES } from '@/env';
export async function GET({ nextUrl: { searchParams } }: NextRequest) {
const imdbId = searchParams.get('imdb_id') || undefined;
const tmdbId = searchParams.get('tmdb_id') || undefined;
if (!imdbId && !tmdbId) {
return NextResponse.json({ error: 'imdb_id or tmdb_id is required' }, { status: 400 });
}
const season = searchParams.get('season');
const episode = searchParams.get('episode');
try {
const subtitles = await searchSubtitles({
type: searchParams.get('type') === 'tv' ? 'tv' : 'movie',
imdbId,
tmdbId,
season: season ? Number(season) : undefined,
episode: episode ? Number(episode) : undefined,
languages: searchParams.get('languages') || SUBTITLE_LANGUAGES
});
return NextResponse.json(subtitles, {
headers: { 'Cache-Control': 'public, s-maxage=86400, stale-while-revalidate=86400' }
});
} catch (error) {
console.error('[subtitles] search', error);
return NextResponse.json({ error: 'Failed to search subtitles' }, { status: 502 });
}
}
-2
View File
@@ -9,7 +9,6 @@ const LIST_PAGES = 25;
const PERSON_PAGES = 10;
const BATCH_SIZE = 20;
// Mirrors the discover filters used by the /popular and /top pages (data/queries/popular.ts)
const DISCOVER_FILTERS = {
'vote_count.gte': '300',
'vote_average.gte': '5',
@@ -36,7 +35,6 @@ const fetchEntries = async (path: string, params: Record<string, string>): Promi
return results.map(({ id, title, name }) => ({ id, title: title || name || '' }));
};
// TMDB allows ~50 requests per second, so the task list is drained in small batches
const collectEntries = async (tasks: (() => Promise<Entry[]>)[]) => {
const entries = new Map<number, string>();