diff --git a/app/(site)/useTorrents.ts b/app/(site)/useTorrents.ts index 15b4362..fdfb6dd 100644 --- a/app/(site)/useTorrents.ts +++ b/app/(site)/useTorrents.ts @@ -1,13 +1,26 @@ import { useQuery } from '@tanstack/react-query'; -import { torrents } from '@/lib/api'; -import { ORDER } from '@/lib/api/parsers/pirate-bay'; +import { Sort } from '@/lib/api/parsers'; -const useTorrents = ({ query, order }: { query: string; order: ORDER }) => { +const useTorrents = ({ + query, + sort, + id, + type, + key, + queryFn +}: { + type: ShowType; + key: string; + id: number; + query: string; + sort: Sort; + queryFn: (params: { query: string; sort: Sort; id: number; type: ShowType }) => Promise; +}) => { return useQuery({ enabled: Boolean(query), - queryKey: ['pirate-bay', query, order], - queryFn: () => torrents({ query, order }) + queryKey: [key, query, sort, id, type], + queryFn: () => queryFn({ query, sort, id, type }) }); }; diff --git a/components/Movie/Preview/components/Actions.tsx b/components/Movie/Preview/components/Actions.tsx index a271b17..820724e 100644 --- a/components/Movie/Preview/components/Actions.tsx +++ b/components/Movie/Preview/components/Actions.tsx @@ -1,6 +1,6 @@ 'use client'; -import { useRouter } from 'next/navigation'; +import Link from 'next/link'; import { Button } from '@/components/ui/button'; @@ -14,8 +14,6 @@ interface Props { } export default function Actions({ onClose, movie, type }: Props) { - const router = useRouter(); - return ( <> @@ -29,6 +27,8 @@ export default function Actions({ onClose, movie, type }: Props) { className='h-8 flex-grow w-[50%]' onClick={() => showTorrentsModal({ + id: movie.id, + type: type as ShowType, title: movie?.name || movie.title, year: new Date(movie.first_air_date || movie.release_date).getFullYear() }) @@ -36,17 +36,13 @@ export default function Actions({ onClose, movie, type }: Props) { > Torrents - + + ); } diff --git a/components/Movie/components/Torrents/index.tsx b/components/Movie/components/Torrents/index.tsx index fb7fafe..2b71396 100644 --- a/components/Movie/components/Torrents/index.tsx +++ b/components/Movie/components/Torrents/index.tsx @@ -1,65 +1,72 @@ import React, { Dispatch, FC, Fragment, SetStateAction, useState } from 'react'; import { create, InstanceProps } from 'react-modal-promise'; import { ArrowDownIcon, CaretSortIcon } from '@radix-ui/react-icons'; -import { Cast, Copy,Loader, Magnet } from 'lucide-react'; +import { Cast, Copy, Loader, Magnet } from 'lucide-react'; import useTorrents from '@/app/(site)/useTorrents'; import { Modal } from '@/components/ui/dialog'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'; +import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Toggle } from '@/components/ui/toggle'; import { useCastMagnet, usePrefix } from '@/hooks/useMagnetHosts'; -import { ORDER } from '@/lib/api/parsers/pirate-bay'; +import { TPBTorrents, YTSTorrents } from '@/lib/api'; +import { Sort } from '@/lib/api/parsers'; +import { cn } from '@/lib/utils'; import showHostManagerModal from './HostManager'; interface Props extends InstanceProps { + id: number; + type: ShowType; title: string; year: number; } -type Sorting = { - by: ORDER; -}; - -const TableHeadSortable = ({ - title, - by, - value, - onChange -}: { +const TableHeadSortable: FC<{ title: string; - by: Sorting['by']; - value: Sorting; - onChange: Dispatch>; -}) => { + sort: Sort; + value: Sort; + sortable: boolean; + onChange: Dispatch>; +}> = ({ title, sort, value, onChange, sortable }) => { return ( - onChange((sorting) => ({ - by: sorting.by === ORDER.seeders ? ORDER.size : ORDER.seeders - })) - } - className='cursor-pointer select-none px-2' + onClick={!sortable ? undefined : () => onChange((sort) => (sort === Sort.seeds ? Sort.size : Sort.seeds))} + className={cn('select-none px-2', sortable && 'cursor-pointer')} >
{title} - {value.by === by ? : } + {sortable && + (sort === value ? : )}
); }; -const Torrents: FC = ({ title, year, onResolve }) => { +const providers = [ + { label: 'YTS', key: 'yts', queryFn: YTSTorrents, sortable: false }, + { label: 'The Pirate Bay', key: 'tpb', queryFn: TPBTorrents, sortable: true } +]; + +const Torrents: FC = ({ title, year, type, id, onResolve }) => { const isMobile = matchMedia('(max-width: 600px)').matches; const prefix = usePrefix(); - const [sorting, setSorting] = useState({ by: ORDER.seeders }); + const [sorting, setSorting] = useState(Sort.seeds); const [withYear, setWithYear] = useState(true); + const [{ key, queryFn, sortable }, setProvider] = useState(providers[0]); const { data: torrents, isLoading, isFetched - } = useTorrents({ query: withYear ? `${title} ${year || ''}` : title, order: sorting.by }); + } = useTorrents({ + query: withYear ? `${title} ${year || ''}` : title, + sort: sorting, + id, + type, + key, + queryFn + }); const cast = useCastMagnet(); @@ -67,7 +74,7 @@ const Torrents: FC = ({ title, year, onResolve }) => { - +
{title} @@ -82,10 +89,35 @@ const Torrents: FC = ({ title, year, onResolve }) => {
+ +
+ {!isMobile && Title} - - + + showHostManagerModal()} /> @@ -138,7 +170,7 @@ const Torrents: FC = ({ title, year, onResolve }) => { )} - {isFetched && !torrents?.length && ( + {!isLoading && isFetched && !torrents?.length && (
diff --git a/lib/api/index.ts b/lib/api/index.ts index d76ba71..fa1e3ae 100644 --- a/lib/api/index.ts +++ b/lib/api/index.ts @@ -6,6 +6,8 @@ import filterUnknownMovies from '../filterUnknownMovies'; import { createUniqueRandomGenerator } from '../utils'; import pirateBay from './parsers/pirate-bay'; +import yts from './parsers/yts'; +import { Sort } from './parsers'; export const search = async ({ query, @@ -102,6 +104,10 @@ export const genres = async (): Promise => { return server.get<{ genres: IDName[] }>('/genre/movie/list').then((data) => data.genres); }; +export const externalIDs = async ({ id, type = 'movie' }: { id: number; type: ShowType }): Promise => { + return server.get(`/${type}/${id}/external_ids`).then(({ imdb_id }) => imdb_id); +}; + export const credits = async ({ movieId, type = 'movie' @@ -123,11 +129,35 @@ export const credits = async ({ }); }; -export const torrents = async ({ query, order, seeders }: { query: string; seeders?: number; order: number }) => { +export const YTSTorrents = async ({ + query, + sort, + type, + id +}: { + query: string; + sort: Sort; + id: number; + type: ShowType; +}) => { + try { + const imdbID = await externalIDs({ id, type }); + const torrents = await yts.search({ + imdbID, + query, + sort + }); + return torrents; + } catch (error) { + return []; + } +}; + +export const TPBTorrents = async ({ query, sort }: { query: string; sort: Sort; id: number; type: ShowType }) => { try { const torrents = await pirateBay.search({ query, - order: order || seeders + sort }); return torrents; } catch (error) { diff --git a/lib/api/parsers/index.ts b/lib/api/parsers/index.ts new file mode 100644 index 0000000..7faab1f --- /dev/null +++ b/lib/api/parsers/index.ts @@ -0,0 +1,4 @@ +export enum Sort { + size = 'size', + seeds = 'seeds' +} diff --git a/lib/api/parsers/pirate-bay.ts b/lib/api/parsers/pirate-bay.ts index 4a0d866..03d34a8 100644 --- a/lib/api/parsers/pirate-bay.ts +++ b/lib/api/parsers/pirate-bay.ts @@ -1,13 +1,14 @@ import axios, { AxiosInstance } from 'axios'; import { parse } from 'node-html-parser'; -export enum ORDER { - size = 5, - seeders = 7 -} +import { Sort } from './index'; export class PirateBay { private client: AxiosInstance; + private sort = { + [Sort.size]: 5, + [Sort.seeds]: 7 + }; constructor(private host: string = process.env.PIRATE_BAY_HOST || 'https://thepiratebay10.org') { this.host = host; @@ -20,14 +21,14 @@ export class PirateBay { query, page = 1, category = 200, - order = ORDER.size + sort = Sort.size }: { query: string; page?: number; category?: number; - order?: ORDER; - }) { - return this.client.post(`/search/${query}/${page}/${order}/${category}`, null).then(({ data }) => { + sort?: Sort; + }): Promise { + return this.client.post(`/search/${query}/${page}/${this.sort[sort]}/${category}`, null).then(({ data }) => { const html = parse(data); const table = html?.getElementById('searchResult'); const rows = table?.querySelectorAll('tr:not(.header)'); @@ -54,7 +55,7 @@ export class PirateBay { seeders: parseInt(seeders[index]!), magnet: magnets[index]! }; - }); + }) as Torrent[]; }); } } diff --git a/lib/api/parsers/yts/index.ts b/lib/api/parsers/yts/index.ts new file mode 100644 index 0000000..94590f8 --- /dev/null +++ b/lib/api/parsers/yts/index.ts @@ -0,0 +1,66 @@ +import axios, { AxiosInstance } from 'axios'; + +import { Sort } from '../index'; + +import { YTSResponse } from './models'; + +export class YTS { + private client: AxiosInstance; + + constructor(private host: string = process.env.YTS_HOST || 'https://yts.mx/api/v2/list_movies.json') { + this.client = axios.create({ + baseURL: this.host + }); + } + + async search({ + query, + page = 1, + sort = Sort.size, + imdbID + }: { + query: string; + imdbID: string; + page?: number; + sort?: Sort; + }): Promise { + return this.client + .get('', { + params: { + page, + query_term: query, + sort_by: sort + } + }) + .then(({ data }) => { + const movie = data?.data?.movies?.find((movie) => movie.imdb_code === imdbID); + + return ( + movie?.torrents.map((torrent) => { + const trackers = [ + 'udp://open.demonii.com:1337/announce', + 'udp://tracker.openbittorrent.com:80', + 'udp://tracker.coppersurfer.tk:6969', + 'udp://glotorrents.pw:6969/announce', + 'udp://tracker.opentrackr.org:1337/announce', + 'udp://torrent.gresille.org:80/announce', + 'udp://p4p.arenabg.com:1337', + 'udp://tracker.leechers-paradise.org:6969' + ]; + + return { + id: movie.id?.toString(), + title: `${movie.title_english} ${torrent.quality ? `[${torrent.quality}]` : ''}`, + seeders: torrent.seeds, + size: torrent.size, + magnet: `magnet:?xt=urn:btih:${torrent.hash}&dn=${encodeURIComponent(query)}&tr=${trackers.join('&tr=')}` + }; + }) || [] + ); + }); + } +} + +const yts = new YTS(); + +export default yts; diff --git a/lib/api/parsers/yts/models.ts b/lib/api/parsers/yts/models.ts new file mode 100644 index 0000000..91df34c --- /dev/null +++ b/lib/api/parsers/yts/models.ts @@ -0,0 +1,98 @@ +export interface YTSResponse { + status: Stat; + status_message: string; + data: Data; + '@meta': Meta; +} + +export interface Meta { + server_time: number; + server_timezone: string; + api_version: number; + execution_time: string; +} + +export interface Data { + movie_count: number; + limit: number; + page_number: number; + movies: Movie[]; +} + +export interface Movie { + id: number; + url: string; + imdb_code: string; + title: string; + title_english: string; + title_long: string; + slug: string; + year: number; + rating: number; + runtime: number; + genres: string[]; + summary: string; + description_full: string; + synopsis: string; + yt_trailer_code: string; + language: Language; + mpa_rating: MpaRating; + background_image: string; + background_image_original: string; + small_cover_image: string; + medium_cover_image: string; + large_cover_image: string; + state: Stat; + torrents: Torrent[]; + date_uploaded: Date; + date_uploaded_unix: number; +} + +export enum Language { + En = 'en', + Ja = 'ja' +} + +export enum MpaRating { + Empty = '', + PG13 = 'PG-13', + R = 'R' +} + +export enum Stat { + Ok = 'ok' +} + +export interface Torrent { + url: string; + hash: string; + quality: Quality; + type: Type; + is_repack: string; + video_codec: VideoCodec; + bit_depth: string; + audio_channels: string; + seeds: number; + peers: number; + size: string; + size_bytes: number; + date_uploaded: Date; + date_uploaded_unix: number; +} + +export enum Quality { + The1080P = '1080p', + The2160P = '2160p', + The3D = '3D', + The720P = '720p' +} + +export enum Type { + Bluray = 'bluray', + Web = 'web' +} + +export enum VideoCodec { + X264 = 'x264', + X265 = 'x265' +} diff --git a/types.d.ts b/types.d.ts index 4327c63..20a5944 100644 --- a/types.d.ts +++ b/types.d.ts @@ -156,3 +156,12 @@ interface Filters { year: Array; score: Array; } + +interface ExternalIDS { + id: number; + imdb_id: string; + wikidata_id: string; + facebook_id: string; + instagram_id: string; + twitter_id: string; +}
+ + + {providers.map((provider) => ( + setProvider(provider)}> + {provider.label} + + ))} + + +