feat: YTS torrents provider
This commit is contained in:
1 parent
c256b7fb7c
commit
4b262bc1d6
9 files changed
+308
-59
No files matched your search
@@ -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<Torrent[]>;
|
||||
}) => {
|
||||
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 })
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -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 (
|
||||
<>
|
||||
<ToggleTrailer type={type} movieId={movie?.id}>
|
||||
@@ -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
|
||||
</Button>
|
||||
<Button
|
||||
className='h-8 flex-grow w-[50%]'
|
||||
onClick={() => {
|
||||
router.push(
|
||||
`/similar?movieId=${movie.id}&title=${encodeURIComponent(movie?.name || movie.title)}&type=${type}`
|
||||
);
|
||||
onClose?.();
|
||||
}}
|
||||
<Link
|
||||
className='flex w-[50%]'
|
||||
href={`/similar?movieId=${movie.id}&title=${encodeURIComponent(movie?.name || movie.title)}&type=${type}`}
|
||||
onClick={onClose}
|
||||
>
|
||||
Similar
|
||||
</Button>
|
||||
<Button className='h-8 flex-grow'>Similar</Button>
|
||||
</Link>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -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<void> {
|
||||
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<SetStateAction<Sorting>>;
|
||||
}) => {
|
||||
sort: Sort;
|
||||
value: Sort;
|
||||
sortable: boolean;
|
||||
onChange: Dispatch<SetStateAction<Sort>>;
|
||||
}> = ({ title, sort, value, onChange, sortable }) => {
|
||||
return (
|
||||
<TableHead
|
||||
onClick={() =>
|
||||
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')}
|
||||
>
|
||||
<div className='flex gap-1 items-center'>
|
||||
<span>{title}</span>
|
||||
{value.by === by ? <ArrowDownIcon className='ml-2 h-4 w-4' /> : <CaretSortIcon className='ml-2 h-4 w-4' />}
|
||||
{sortable &&
|
||||
(sort === value ? <ArrowDownIcon className='ml-2 h-4 w-4' /> : <CaretSortIcon className='ml-2 h-4 w-4' />)}
|
||||
</div>
|
||||
</TableHead>
|
||||
);
|
||||
};
|
||||
|
||||
const Torrents: FC<Props> = ({ 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<Props> = ({ title, year, type, id, onResolve }) => {
|
||||
const isMobile = matchMedia('(max-width: 600px)').matches;
|
||||
const prefix = usePrefix();
|
||||
const [sorting, setSorting] = useState<Sorting>({ by: ORDER.seeders });
|
||||
const [sorting, setSorting] = useState<Sort>(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<Props> = ({ title, year, onResolve }) => {
|
||||
<Modal className='p-0' onClose={onResolve}>
|
||||
<Table className='rounded-xl overflow-hidden'>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableRow className='!border-b-0'>
|
||||
<TableHead className='px-2' colSpan={4}>
|
||||
<div className='grid grid-cols-[1fr_auto] items-center gap-3'>
|
||||
<span className='overflow-ellipsis overflow-hidden text-base whitespace-nowrap'>{title}</span>
|
||||
@@ -82,10 +89,35 @@ const Torrents: FC<Props> = ({ title, year, onResolve }) => {
|
||||
</div>
|
||||
</TableHead>
|
||||
</TableRow>
|
||||
<TableRow className='!border-b-0'>
|
||||
<th colSpan={5}>
|
||||
<Tabs defaultValue={key} value={key} className='w-full rounded-none'>
|
||||
<TabsList className='grid w-full grid-cols-2'>
|
||||
{providers.map((provider) => (
|
||||
<TabsTrigger key={provider.key} value={provider.key} onClick={() => setProvider(provider)}>
|
||||
{provider.label}
|
||||
</TabsTrigger>
|
||||
))}
|
||||
</TabsList>
|
||||
</Tabs>
|
||||
</th>
|
||||
</TableRow>
|
||||
<TableRow>
|
||||
{!isMobile && <TableHead className='px-2'>Title</TableHead>}
|
||||
<TableHeadSortable title='Size' by={ORDER.size} value={sorting} onChange={setSorting} />
|
||||
<TableHeadSortable title='Seeders' by={ORDER.seeders} value={sorting} onChange={setSorting} />
|
||||
<TableHeadSortable
|
||||
sortable={sortable}
|
||||
title='Size'
|
||||
sort={Sort.size}
|
||||
value={sorting}
|
||||
onChange={setSorting}
|
||||
/>
|
||||
<TableHeadSortable
|
||||
sortable={sortable}
|
||||
title='Seeders'
|
||||
sort={Sort.seeds}
|
||||
value={sorting}
|
||||
onChange={setSorting}
|
||||
/>
|
||||
<TableHead className='px-2 cursor-pointer select-none' onClick={() => showHostManagerModal()} />
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
@@ -138,7 +170,7 @@ const Torrents: FC<Props> = ({ title, year, onResolve }) => {
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
{isFetched && !torrents?.length && (
|
||||
{!isLoading && isFetched && !torrents?.length && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={4}>
|
||||
<div className='h-40 w-full flex items-center justify-center text-xl text-muted-foreground'>
|
||||
|
||||
+32
-2
@@ -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<IDName[]> => {
|
||||
return server.get<{ genres: IDName[] }>('/genre/movie/list').then((data) => data.genres);
|
||||
};
|
||||
|
||||
export const externalIDs = async ({ id, type = 'movie' }: { id: number; type: ShowType }): Promise<string> => {
|
||||
return server.get<ExternalIDS>(`/${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) {
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export enum Sort {
|
||||
size = 'size',
|
||||
seeds = 'seeds'
|
||||
}
|
||||
@@ -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<Torrent[]> {
|
||||
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[];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Torrent[]> {
|
||||
return this.client
|
||||
.get<YTSResponse>('', {
|
||||
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;
|
||||
@@ -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'
|
||||
}
|
||||
Vendored
+9
@@ -156,3 +156,12 @@ interface Filters {
|
||||
year: Array<string>;
|
||||
score: Array<string>;
|
||||
}
|
||||
|
||||
interface ExternalIDS {
|
||||
id: number;
|
||||
imdb_id: string;
|
||||
wikidata_id: string;
|
||||
facebook_id: string;
|
||||
instagram_id: string;
|
||||
twitter_id: string;
|
||||
}
|
||||
Reference in new issue
Block a user