feat(torrents): series parsing, S:E/Year columns, magnet cache

- parse Toloka Ukrainian season/episode naming (Сезони 1-10,
  серій 7 з 8, homoglyphs) into a clean title + episodes field
- add series-only sortable S:E column, desktop-only Year column;
  strip year/source meta from the title cell
- filter out torrents with fewer than 5 seeders in the API route
- cache Toloka magnets in Redis so each .torrent is downloaded once
  across serverless instances; append public trackers to magnets
  (never the passkey-bearing announce URLs from the .torrent)
- fix play button hover state while magnet is pending

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
nerandClaude Fable 5 committed 2026-08-06 22:14:58 +03:00
1 parent 131beeb07f
commit 2e81908b22
9 files changed
+163 -34

No files matched your search

+34 -8
View File
@@ -2,15 +2,17 @@ import axios, { AxiosInstance } from 'axios';
import { wrapper } from 'axios-cookiejar-support';
import { load } from 'cheerio';
import parseTorrent, { Instance } from 'parse-torrent';
import torrentTitle from 'parse-torrent-title';
import { Cookie, CookieJar, SerializedCookieJar } from 'tough-cookie';
import redis from '@/data/clients/redis';
import { TOLOKA_HOST, TOLOKA_PASSWORD, TOLOKA_USERNAME } from '@/env';
import { Sort } from '../index';
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) =>
@@ -113,9 +115,7 @@ export class Toloka {
const seeders = $(row).find('td:nth-child(10)').text();
const download = $(row).find('td:nth-child(6) a').attr('href');
const { year, source, codec, container, title, season, episode, resolution } = torrentTitle.parse(
originalTitle || ''
);
const { year, source, codec, container, title, resolution, episodes } = parseTolokaTitle(originalTitle || '');
return {
year: year?.toString(),
@@ -123,7 +123,8 @@ export class Toloka {
codec,
container,
originalTitle,
title: `${title}${season && episode ? ` [S${season}:E${episode}]` : ''}`,
title,
episodes,
id: originalTitle || '',
size,
seeders: parseInt(seeders || '0'),
@@ -151,6 +152,20 @@ export class Toloka {
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'
@@ -169,11 +184,22 @@ export class Toloka {
torrent = (await fetchTorrent()) as Instance;
}
const magnet = `magnet:?xt=urn:btih:${torrent.infoHash}&dn=${encodeURIComponent(torrent.name?.toString() || '')}`;
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;
return this.magnets[url];
try {
await redis?.set(`${MAGNET_KEY_PREFIX}${url}`, magnet);
} catch (error) {
redisWarn('Failed to persist Toloka magnet to Redis:', error);
}
return magnet;
}
}