feat: YTS torrents provider

This commit is contained in:
ner committed 2024-09-02 15:00:50 +03:00
1 parent c256b7fb7c
commit 4b262bc1d6
9 files changed
+308 -59

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
export enum Sort {
size = 'size',
seeds = 'seeds'
}
+10 -9
View File
@@ -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[];
});
}
}
+66
View File
@@ -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;
+98
View File
@@ -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'
}