diff --git a/app/api/refresh/route.ts b/app/api/refresh/route.ts new file mode 100644 index 0000000..6fc564d --- /dev/null +++ b/app/api/refresh/route.ts @@ -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=), 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(); + } +} diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx index da04261..5107bc9 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx @@ -90,6 +90,14 @@ const Actions: FC = ({ torrent, title, provider, show }) => { if (show.type === 'tv') { const season = seasonFromEpisodes(torrent.episodes) ?? show.airing?.season; if (season != null) params.set('season', season.toString()); + + // Toloka thread id (download.php?id=): the TV stores it and later + // asks movie-advisor's /api/refresh to re-pull the thread's current + // magnet, so a still-airing season picks up new episodes on its own. + if (provider === providers.tlk.key && torrent.download) { + const tid = new URLSearchParams(torrent.download.split('?')[1] || '').get('id'); + if (tid) params.set('tlk', tid); + } } if (isSafari) { diff --git a/data/parsers/tlk/index.ts b/data/parsers/tlk/index.ts index 2afb7b6..898d178 100644 --- a/data/parsers/tlk/index.ts +++ b/data/parsers/tlk/index.ts @@ -12,7 +12,6 @@ import { Sort, TRACKERS } from '../index'; import { parseTolokaTitle } from './title'; const COOKIE_JAR_KEY = 'toloka:cookie-jar'; -const MAGNET_KEY_PREFIX = 'toloka:magnet:'; // UpstashError messages embed the full command, including cookie values — never log them const redisWarn = (message: string, error: unknown) => @@ -30,8 +29,6 @@ export class Toloka { private hydration?: Promise; - private magnets: Record = {}; - constructor() { this.host = TOLOKA_HOST || 'https://toloka.to'; @@ -148,24 +145,8 @@ export class Toloka { } async magnet(url: string) { - if (this.magnets[url]) return this.magnets[url]; - await this.hydrate(); - // an infohash never changes for a given torrent, so a cached magnet is valid forever - // and saves a .torrent download from Toloka on every cold start - try { - const cached = await redis?.get(`${MAGNET_KEY_PREFIX}${url}`); - - if (cached) { - this.magnets[url] = cached; - - return cached; - } - } catch (error) { - redisWarn('Failed to read Toloka magnet from Redis:', error); - } - const fetchTorrent = async () => { const { data: buffer } = await this.client.get(`/${url}`, { responseType: 'arraybuffer' @@ -184,22 +165,9 @@ export class Toloka { torrent = (await fetchTorrent()) as Instance; } - console.info(`[tlk] downloaded .torrent for ${url}`); - - // the .torrent's own announce URLs embed the account passkey — never expose them in a magnet; - // public trackers help discover cross-seeded peers, the rest is covered by DHT const trackers = TRACKERS.map((tracker) => `&tr=${encodeURIComponent(tracker)}`).join(''); - const magnet = `magnet:?xt=urn:btih:${torrent.infoHash}&dn=${encodeURIComponent(torrent.name?.toString() || '')}${trackers}`; - this.magnets[url] = magnet; - - try { - await redis?.set(`${MAGNET_KEY_PREFIX}${url}`, magnet); - } catch (error) { - redisWarn('Failed to persist Toloka magnet to Redis:', error); - } - - return magnet; + return `magnet:?xt=urn:btih:${torrent.infoHash}&dn=${encodeURIComponent(torrent.name?.toString() || '')}${trackers}`; } }