feat(torrents): Toloka section filter by show type, year scope for movies only

This commit is contained in:
ner committed 2026-09-13 19:14:42 +03:00
1 parent c52d798221
commit f642dad64e
5 files changed
+56 -13

No files matched your search

+2 -1
View File
@@ -13,11 +13,12 @@ export async function GET({ nextUrl: { searchParams } }: NextRequest) {
const imdbID = searchParams.get('imdbID') as string; const imdbID = searchParams.get('imdbID') as string;
const query = searchParams.get('query') as string; const query = searchParams.get('query') as string;
const sort = searchParams.get('sort') as Sort; const sort = searchParams.get('sort') as Sort;
const type = searchParams.get('type') as 'movie' | 'tv' | null;
const providers = { yts, tpb, tlk }; const providers = { yts, tpb, tlk };
try { try {
const torrents = await providers[key].search({ imdbID, query, sort }); const torrents = await providers[key].search({ imdbID, query, sort, type: type ?? undefined });
return NextResponse.json(torrents.filter((torrent) => torrent.seeders >= MIN_SEEDERS)); return NextResponse.json(torrents.filter((torrent) => torrent.seeders >= MIN_SEEDERS));
} catch (_) { } catch (_) {
@@ -1,9 +1,11 @@
import { Sort } from '@/data/parsers'; import { Sort } from '@/data/parsers';
type Options = { query: string; sort: Sort; imdbID: string }; type Options = { query: string; sort: Sort; imdbID: string; type?: 'movie' | 'tv' };
const queryFn = ({ query, sort, imdbID, key }: Options & { key: keyof typeof providers }) => const queryFn = ({ query, sort, imdbID, type, key }: Options & { key: keyof typeof providers }) =>
fetch(`/api/torrents?key=${key}&query=${query}&sort=${sort}&imdbID=${imdbID}`).then((response) => response.json()); fetch(`/api/torrents?key=${key}&query=${query}&sort=${sort}&imdbID=${imdbID}&type=${type ?? ''}`).then((response) =>
response.json()
);
export const providers = { export const providers = {
yts: { yts: {
@@ -52,9 +52,10 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => {
const isSeries = show.type === 'tv'; const isSeries = show.type === 'tv';
// Toloka's search is a plain "contains" word-AND match with no IMDB filter, so // Toloka's search is a plain "contains" word-AND match with no IMDB filter. For movies,
// scope the query by the show's year to keep, e.g., "Dune" (2021) away from "Dune: Part Two" (2024). // scope the query by the show's year to keep, e.g., "Dune" (2021) away from "Dune: Part Two" (2024);
const tolokaQuery = query && Number.isFinite(year) ? `${query} ${year}` : query; // for series the TV sections do the disambiguation and a year suffix would hide other seasons.
const tolokaQuery = isSeries ? query : query && Number.isFinite(year) ? `${query} ${year}` : query;
const tracked = { id: show.id, type: show.type, title: show.title }; const tracked = { id: show.id, type: show.type, title: show.title };
@@ -64,6 +65,7 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => {
sort, sort,
key: providers.yts.key, key: providers.yts.key,
imdbID: show.imdb_id!, imdbID: show.imdb_id!,
type: show.type,
show: tracked show: tracked
}); });
const tpb = useTorrents({ const tpb = useTorrents({
@@ -72,6 +74,7 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => {
sort, sort,
key: providers.tpb.key, key: providers.tpb.key,
imdbID: show.imdb_id!, imdbID: show.imdb_id!,
type: show.type,
show: tracked show: tracked
}); });
const tlk = useTorrents({ const tlk = useTorrents({
@@ -80,6 +83,7 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => {
sort, sort,
key: providers.tlk.key, key: providers.tlk.key,
imdbID: show.imdb_id!, imdbID: show.imdb_id!,
type: show.type,
show: tracked show: tracked
}); });
@@ -8,6 +8,7 @@ const useTorrents = ({
query, query,
sort, sort,
imdbID, imdbID,
type,
key, key,
queryFn, queryFn,
show show
@@ -16,13 +17,14 @@ const useTorrents = ({
query: string; query: string;
sort: Sort; sort: Sort;
imdbID: string; imdbID: string;
type?: Show['type'];
show: { id: Show['id']; type: Show['type']; title: Show['title'] }; show: { id: Show['id']; type: Show['type']; title: Show['title'] };
queryFn: (params: { query: string; sort: Sort; imdbID: string }) => Promise<Torrent[]>; queryFn: (params: { query: string; sort: Sort; imdbID: string; type?: Show['type'] }) => Promise<Torrent[]>;
}) => { }) => {
const result = useQuery({ const result = useQuery({
enabled: Boolean(query), enabled: Boolean(query),
queryKey: [key, query, sort, imdbID], queryKey: [key, query, sort, imdbID, type],
queryFn: () => queryFn({ query, sort, imdbID }) queryFn: () => queryFn({ query, sort, imdbID, type })
}); });
useTrackOnce(analytics.torrentsSearched, result.data ? `${key}:${query}` : undefined, () => ({ useTrackOnce(analytics.torrentsSearched, result.data ? `${key}:${query}` : undefined, () => ({
+37 -3
View File
@@ -13,6 +13,13 @@ import { parseTolokaTitle } from './title';
const COOKIE_JAR_KEY = 'toloka:cookie-jar'; const COOKIE_JAR_KEY = 'toloka:cookie-jar';
type ShowType = 'movie' | 'tv';
// Section ids for the `f[]` search filter: the movie/TV sets keep releases of the
// other media type (and non-video sections) out of the results.
const MOVIE_SECTIONS = [16, 42, 55, 70, 96, 129];
const TV_SECTIONS = [32, 124, 173, 192];
const redisWarn = (message: string, error: unknown) => const redisWarn = (message: string, error: unknown) =>
console.warn(message, error instanceof Error ? error.message.split(', command was:')[0] : error); console.warn(message, error instanceof Error ? error.message.split(', command was:')[0] : error);
@@ -95,10 +102,19 @@ export class Toloka {
await this.persist(); await this.persist();
} }
async search({ query, sort = Sort.size }: { query: string; sort?: Sort }): Promise<Torrent[]> { private buildSearchUrl(query: string, sections: number[], sort: Sort) {
await this.hydrate(); const params = new URLSearchParams();
const fetchPage = () => this.client.get(`/tracker.php?o=${this.sort[sort]}&nm=${query}`); params.append('o', this.sort[sort].toString());
params.append('nm', query);
for (const section of sections) params.append('f[]', section.toString());
return `/tracker.php?${params.toString()}`;
}
private async runSearch(url: string): Promise<Torrent[]> {
const fetchPage = () => this.client.get(url);
const parseTorrents = (data: string) => { const parseTorrents = (data: string) => {
const $ = load(data); const $ = load(data);
@@ -143,6 +159,24 @@ export class Toloka {
return parseTorrents(page.data); return parseTorrents(page.data);
} }
async search({ query, sort = Sort.size, type }: { query: string; sort?: Sort; type?: ShowType }): Promise<Torrent[]> {
await this.hydrate();
const sections = type === 'tv' ? TV_SECTIONS : type === 'movie' ? MOVIE_SECTIONS : [];
const search = (searchQuery: string) => this.runSearch(this.buildSearchUrl(searchQuery, sections, sort));
const torrents = await search(query);
if (torrents.length) return torrents;
// The query is scoped by the show's year to disambiguate sequels, but the TMDB
// release year can differ from the year in release names, so fall back to the bare title.
const fallback = query.replace(/\s\d{4}$/, '');
return fallback === query ? torrents : search(fallback);
}
async magnet(url: string) { async magnet(url: string) {
await this.hydrate(); await this.hydrate();