feat(tlk): refresh endpoint to re-pull a thread's current magnet

Add /api/refresh?tid=<id> that re-downloads a Toloka thread's .torrent
server-side and returns a fresh magnet + m3u. Pass the thread id (tlk)
into the m3u params for series. Drop the per-magnet cache so refreshes
always reflect the thread's current state (new episodes while airing).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
nerandClaude Opus 4.8 committed 2026-08-07 20:24:44 +03:00
1 parent 284458f783
commit 3ddba5e6a2
3 files changed
+39 -33

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
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');
if (!tid || !/^\d+$/.test(tid)) {
return NextResponse.json({ error: 'tid required' }, { status: 400 });
}
try {
const magnet = await tlk.magnet(`download.php?id=${tid}`);
const [, infoHash] = magnet.match(/btih:([a-z0-9]+)/i) || [];
return NextResponse.json({
infoHash: infoHash?.toLowerCase() ?? null,
magnet,
m3u: `${STREAM_URL}/stream?m3u&link=${magnet}`
});
} catch (_) {
return NextResponse.error();
}
}