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:
1 parent
284458f783
commit
3ddba5e6a2
3 files changed
+39
-33
No files matched your search
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
@@ -90,6 +90,14 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
|
|||||||
if (show.type === 'tv') {
|
if (show.type === 'tv') {
|
||||||
const season = seasonFromEpisodes(torrent.episodes) ?? show.airing?.season;
|
const season = seasonFromEpisodes(torrent.episodes) ?? show.airing?.season;
|
||||||
if (season != null) params.set('season', season.toString());
|
if (season != null) params.set('season', season.toString());
|
||||||
|
|
||||||
|
// Toloka thread id (download.php?id=<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) {
|
if (isSafari) {
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import { Sort, TRACKERS } from '../index';
|
|||||||
import { parseTolokaTitle } from './title';
|
import { parseTolokaTitle } from './title';
|
||||||
|
|
||||||
const COOKIE_JAR_KEY = 'toloka:cookie-jar';
|
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
|
// UpstashError messages embed the full command, including cookie values — never log them
|
||||||
const redisWarn = (message: string, error: unknown) =>
|
const redisWarn = (message: string, error: unknown) =>
|
||||||
@@ -30,8 +29,6 @@ export class Toloka {
|
|||||||
|
|
||||||
private hydration?: Promise<void>;
|
private hydration?: Promise<void>;
|
||||||
|
|
||||||
private magnets: Record<string, string> = {};
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.host = TOLOKA_HOST || 'https://toloka.to';
|
this.host = TOLOKA_HOST || 'https://toloka.to';
|
||||||
|
|
||||||
@@ -148,24 +145,8 @@ export class Toloka {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async magnet(url: string) {
|
async magnet(url: string) {
|
||||||
if (this.magnets[url]) return this.magnets[url];
|
|
||||||
|
|
||||||
await this.hydrate();
|
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<string>(`${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 fetchTorrent = async () => {
|
||||||
const { data: buffer } = await this.client.get(`/${url}`, {
|
const { data: buffer } = await this.client.get(`/${url}`, {
|
||||||
responseType: 'arraybuffer'
|
responseType: 'arraybuffer'
|
||||||
@@ -184,22 +165,9 @@ export class Toloka {
|
|||||||
torrent = (await fetchTorrent()) as Instance;
|
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 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;
|
return `magnet:?xt=urn:btih:${torrent.infoHash}&dn=${encodeURIComponent(torrent.name?.toString() || '')}${trackers}`;
|
||||||
|
|
||||||
try {
|
|
||||||
await redis?.set(`${MAGNET_KEY_PREFIX}${url}`, magnet);
|
|
||||||
} catch (error) {
|
|
||||||
redisWarn('Failed to persist Toloka magnet to Redis:', error);
|
|
||||||
}
|
|
||||||
|
|
||||||
return magnet;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user