diff --git a/app/api/torrents/route.ts b/app/api/torrents/route.ts index 369fd77..90e7dbd 100644 --- a/app/api/torrents/route.ts +++ b/app/api/torrents/route.ts @@ -13,11 +13,12 @@ export async function GET({ nextUrl: { searchParams } }: NextRequest) { const imdbID = searchParams.get('imdbID') as string; const query = searchParams.get('query') as string; const sort = searchParams.get('sort') as Sort; + const type = searchParams.get('type') as 'movie' | 'tv' | null; const providers = { yts, tpb, tlk }; 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)); } catch (_) { diff --git a/components/Preview/components/Torrents/constants.ts b/components/Preview/components/Torrents/constants.ts index 0fed254..f91472a 100644 --- a/components/Preview/components/Torrents/constants.ts +++ b/components/Preview/components/Torrents/constants.ts @@ -1,9 +1,11 @@ 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 }) => - fetch(`/api/torrents?key=${key}&query=${query}&sort=${sort}&imdbID=${imdbID}`).then((response) => response.json()); +const queryFn = ({ query, sort, imdbID, type, key }: Options & { key: keyof typeof providers }) => + fetch(`/api/torrents?key=${key}&query=${query}&sort=${sort}&imdbID=${imdbID}&type=${type ?? ''}`).then((response) => + response.json() + ); export const providers = { yts: { diff --git a/components/Preview/components/Torrents/index.tsx b/components/Preview/components/Torrents/index.tsx index 5b8137b..00abfa7 100644 --- a/components/Preview/components/Torrents/index.tsx +++ b/components/Preview/components/Torrents/index.tsx @@ -52,9 +52,10 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => { const isSeries = show.type === 'tv'; - // Toloka's search is a plain "contains" word-AND match with no IMDB filter, so - // 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; + // 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); + // 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 }; @@ -64,6 +65,7 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => { sort, key: providers.yts.key, imdbID: show.imdb_id!, + type: show.type, show: tracked }); const tpb = useTorrents({ @@ -72,6 +74,7 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => { sort, key: providers.tpb.key, imdbID: show.imdb_id!, + type: show.type, show: tracked }); const tlk = useTorrents({ @@ -80,6 +83,7 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => { sort, key: providers.tlk.key, imdbID: show.imdb_id!, + type: show.type, show: tracked }); diff --git a/components/Preview/components/Torrents/useTorrents.ts b/components/Preview/components/Torrents/useTorrents.ts index 21e64b0..846114c 100644 --- a/components/Preview/components/Torrents/useTorrents.ts +++ b/components/Preview/components/Torrents/useTorrents.ts @@ -8,6 +8,7 @@ const useTorrents = ({ query, sort, imdbID, + type, key, queryFn, show @@ -16,13 +17,14 @@ const useTorrents = ({ query: string; sort: Sort; imdbID: string; + type?: Show['type']; show: { id: Show['id']; type: Show['type']; title: Show['title'] }; - queryFn: (params: { query: string; sort: Sort; imdbID: string }) => Promise; + queryFn: (params: { query: string; sort: Sort; imdbID: string; type?: Show['type'] }) => Promise; }) => { const result = useQuery({ enabled: Boolean(query), - queryKey: [key, query, sort, imdbID], - queryFn: () => queryFn({ query, sort, imdbID }) + queryKey: [key, query, sort, imdbID, type], + queryFn: () => queryFn({ query, sort, imdbID, type }) }); useTrackOnce(analytics.torrentsSearched, result.data ? `${key}:${query}` : undefined, () => ({ diff --git a/data/parsers/tlk/index.ts b/data/parsers/tlk/index.ts index 136161b..4525c45 100644 --- a/data/parsers/tlk/index.ts +++ b/data/parsers/tlk/index.ts @@ -13,6 +13,13 @@ import { parseTolokaTitle } from './title'; 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) => console.warn(message, error instanceof Error ? error.message.split(', command was:')[0] : error); @@ -95,10 +102,19 @@ export class Toloka { await this.persist(); } - async search({ query, sort = Sort.size }: { query: string; sort?: Sort }): Promise { - await this.hydrate(); + private buildSearchUrl(query: string, sections: number[], sort: Sort) { + 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 { + const fetchPage = () => this.client.get(url); const parseTorrents = (data: string) => { const $ = load(data); @@ -143,6 +159,24 @@ export class Toloka { return parseTorrents(page.data); } + async search({ query, sort = Sort.size, type }: { query: string; sort?: Sort; type?: ShowType }): Promise { + 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) { await this.hydrate();