Files
nerandClaude Fable 5 2e81908b22 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>
2026-08-06 22:14:58 +03:00

88 lines
2.2 KiB
TypeScript

import axios, { AxiosInstance } from 'axios';
import { load } from 'cheerio';
import torrentTitle from 'parse-torrent-title';
import { PIRATE_BAY_HOST } from '@/env';
import { Sort } from '../index';
export class PirateBay {
private client: AxiosInstance;
private host: string;
private sort = {
[Sort.size]: 5,
[Sort.seeds]: 7
};
constructor() {
this.host = PIRATE_BAY_HOST || 'https://thepiratebay10.info';
this.client = axios.create({
baseURL: this.host
});
}
async search({
query,
page = 1,
category = 200,
sort = Sort.size
}: {
query: string;
page?: number;
category?: number;
sort?: Sort;
}): Promise<Torrent[]> {
return this.client.post(`/search/${query}/${page}/${this.sort[sort]}/${category}`, null).then(({ data }) => {
const $ = load(data);
const table = $('#searchResult');
const rows = table.find('tr:not(.header)');
const titles = rows
.map((_, row) => $(row).find('td:nth-child(2) a').text())
.get()
.filter(Boolean);
const seeders = rows.map((_, row) => $(row).find('td:nth-child(6)').text()).get();
const sizes = rows
.map((_, row) => {
const size = $(row).find('td:nth-child(5)').text();
return size ? size.replace('&nbsp;', ' ').replace('Size ', '') : null;
})
.get()
.filter(Boolean);
const magnets = table
.find('a[href^="magnet:"]')
.map((_, link) => $(link).attr('href'))
.get();
return titles.map((originalTitle, index) => {
const { title, resolution, source, codec, container, season, episode, year } = torrentTitle.parse(
originalTitle || ''
);
return {
year: year?.toString(),
source,
codec,
container,
originalTitle,
title,
episodes: season && episode ? `S${season}:E${episode}` : undefined,
id: title,
size: sizes[index],
seeders: parseInt(seeders[index] || '0', 10),
quality: resolution,
magnet: magnets[index] || '',
hash: ''
};
});
});
}
}
const parser = new PirateBay();
export default parser;