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

+1 -33
View File
@@ -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<void>;
private magnets: Record<string, string> = {};
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<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 { 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}`;
}
}