feat: added the pirate bay parser
This commit is contained in:
1 parent
74e35267f9
commit
ada725072f
6 files changed
+74
-4755
No files matched your search
@@ -0,0 +1,63 @@
|
||||
import axios, { AxiosInstance } from 'axios';
|
||||
import { parse } from 'node-html-parser';
|
||||
|
||||
enum ORDER {
|
||||
size = 5,
|
||||
seeders = 7
|
||||
}
|
||||
|
||||
export class PirateBay {
|
||||
private client: AxiosInstance;
|
||||
|
||||
constructor(private host: string = process.env.PIRATE_BAY_HOST || 'https://thepiratebay10.org') {
|
||||
this.host = host;
|
||||
this.client = axios.create({
|
||||
baseURL: this.host
|
||||
});
|
||||
}
|
||||
|
||||
async search({
|
||||
query,
|
||||
page = 1,
|
||||
category = 200,
|
||||
order = ORDER.size
|
||||
}: {
|
||||
query: string;
|
||||
page?: number;
|
||||
category?: number;
|
||||
order?: ORDER;
|
||||
}) {
|
||||
return this.client.post(`/search/${query}/${page}/${order}/${category}`, null).then(({ data }) => {
|
||||
const html = parse(data);
|
||||
const table = html?.getElementById('searchResult');
|
||||
|
||||
const titles = table?.querySelectorAll('.detLink').map((link) => link.innerText);
|
||||
const seeders = table
|
||||
?.querySelectorAll('td[align="right"]')
|
||||
.filter((_, index) => index % 2 === 0)
|
||||
.map((cell) => cell.innerText);
|
||||
const sizes = table?.querySelectorAll('.detDesc').reduce((sizes, description) => {
|
||||
const list = description.innerText?.split(', ');
|
||||
if (list.length === 1) return sizes;
|
||||
const [, size] = list;
|
||||
sizes.push(size.replace(' ', ' ').replace('Size ', ''));
|
||||
return sizes;
|
||||
}, [] as string[]);
|
||||
const magnets = table?.querySelectorAll('a[href^="magnet:"]').map((link) => link.getAttribute('href'));
|
||||
|
||||
return titles.map((title, index) => {
|
||||
return {
|
||||
title,
|
||||
id: title,
|
||||
size: sizes[index],
|
||||
seeders: parseInt(seeders[index]),
|
||||
magnet: magnets[index]
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const parser = new PirateBay();
|
||||
|
||||
export default parser;
|
||||
@@ -1,99 +0,0 @@
|
||||
import axios, { Axios, AxiosError, AxiosInstance } from 'axios';
|
||||
import iconv from 'iconv-lite';
|
||||
import { parse } from 'node-html-parser';
|
||||
|
||||
export class Tracker {
|
||||
private client: AxiosInstance;
|
||||
// private authorized: Promise<boolean>;
|
||||
|
||||
constructor(
|
||||
private credentials: { login: string; password: string },
|
||||
private host: string = process.env.TRACKER_HOST || 'https://rutracker.org/forum'
|
||||
) {
|
||||
this.host = host;
|
||||
this.credentials = credentials;
|
||||
this.client = axios.create({
|
||||
baseURL: this.host,
|
||||
responseType: 'arraybuffer',
|
||||
headers: {
|
||||
Cookie:
|
||||
'bb_guid=VRSW4TnBYXKS;bb_ssl=1;opt_js={%22only_new%22:0%2C%22h_flag%22:0%2C%22h_av%22:0%2C%22h_rnk_i%22:0%2C%22h_post_i%22:0%2C%22h_smile%22:0%2C%22h_sig%22:0%2C%22sp_op%22:0%2C%22tr_tm%22:0%2C%22h_cat%22:%22%22%2C%22h_tsp%22:0%2C%22h_ta%22:76};bb_t=a%3A5%3A%7Bi%3A6370796%3Bi%3A1685706087%3Bi%3A6358638%3Bi%3A1683106302%3Bi%3A6123599%3Bi%3A1664644059%3Bi%3A6169892%3Bi%3A1643607794%3Bi%3A6169913%3Bi%3A1643503061%3B%7D;bb_session=0-17771272-7EtecN88pRZEt82Uf9z7;cf_clearance=sR1CBz2JU3HK8IH8USbPQg681FvxoKMEDFcp.bOCiMw-1691858160-0-1-9ac26b22.c8ca8b5e.cdc334c2-0.2.1691858160'
|
||||
}
|
||||
});
|
||||
this.client.interceptors.response.use((response) => ({
|
||||
...response,
|
||||
data: iconv.decode(response.data, 'win1251')
|
||||
}));
|
||||
// this.authorized = this.login();
|
||||
}
|
||||
|
||||
async login() {
|
||||
const body = new URLSearchParams();
|
||||
|
||||
body.append('redirect', 'index.php');
|
||||
body.append('login_username', this.credentials.login);
|
||||
body.append('login_password', this.credentials.password);
|
||||
body.append('login', 'Вход');
|
||||
|
||||
return this.client
|
||||
.post('/login.php', body.toString(), {
|
||||
maxRedirects: 0,
|
||||
validateStatus(status) {
|
||||
return status === 302;
|
||||
}
|
||||
})
|
||||
.then(({ headers }) => {
|
||||
const [_, session] = headers['set-cookie'] || [];
|
||||
const [Cookie] = (session || '').split(';');
|
||||
|
||||
this.client.defaults.headers.common = {
|
||||
Cookie: `bb_ssl=1; ${Cookie}`
|
||||
};
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
async search({ query, page = 0, searchId }: { query: string; page?: number; searchId?: string }) {
|
||||
// try {
|
||||
// await this.authorized.catch((error) => {
|
||||
// return Promise.reject(iconv.decode(error.response?.data, 'win1251'));
|
||||
// });
|
||||
// } catch (error) {
|
||||
// return Promise.resolve(error);
|
||||
// }
|
||||
|
||||
return this.client
|
||||
.post('/tracker.php', null, {
|
||||
params: {
|
||||
nm: query,
|
||||
search_id: searchId,
|
||||
start: page * 50
|
||||
}
|
||||
})
|
||||
.then(({ data }) => {
|
||||
const html = parse(data);
|
||||
|
||||
const topics = html.querySelectorAll('tr[data-topic_id]').map((row) => {
|
||||
return {
|
||||
id: row.getAttribute('data-topic_id'),
|
||||
category: row.querySelector('.f-name .ts-text')?.text,
|
||||
size: row.querySelector('.tor-size .tr-dl')?.text?.replace(' ↓', ''),
|
||||
title: row.querySelector('.t-title .ts-text')?.text,
|
||||
seeders: parseInt(row.querySelector('.seedmed')?.text || '0'),
|
||||
searchId: new URLSearchParams(html.querySelector('.pg')?.getAttribute('href')?.split('?')?.[1]).get(
|
||||
'search_id'
|
||||
)
|
||||
};
|
||||
});
|
||||
|
||||
return topics;
|
||||
});
|
||||
}
|
||||
|
||||
fetchMagnet(torrentId: string) {}
|
||||
}
|
||||
|
||||
const tracker = new Tracker({ login: process.env.TRACKER_LOGIN!, password: process.env.TRACKER_PASSWORD! });
|
||||
|
||||
export default tracker;
|
||||
@@ -1,23 +1,13 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import PirateBay from 'thepiratebayfixed';
|
||||
|
||||
import pirateBay from '../parsers/pirate-bay';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const params = Object.fromEntries(new URL(request.url).searchParams.entries());
|
||||
|
||||
try {
|
||||
const results: PirateTorrent[] = await PirateBay.search(params.query, {
|
||||
category: 'video'
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
results?.map(({ seeders, name, id, magnetLink, size }) => ({
|
||||
id,
|
||||
title: name,
|
||||
magnetLink,
|
||||
size,
|
||||
seeders: parseInt(seeders)
|
||||
})) || []
|
||||
);
|
||||
const torrents = await pirateBay.search({ query: params.query });
|
||||
return NextResponse.json(torrents);
|
||||
} catch (error) {
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
// import Tracker from 'rutracker-api-with-proxy';
|
||||
import tracker from '../parsers/tracker';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const params = Object.fromEntries(new URL(request.url).searchParams.entries());
|
||||
|
||||
// try {
|
||||
// const tracker = new Tracker();
|
||||
// await tracker.login({ username: process.env.TRACKER_LOGIN, password: process.env.TRACKER_PASSWORD });
|
||||
|
||||
// const results: TrackerTorrent[] = await tracker.search({ query: params.query });
|
||||
|
||||
// const movies = results.filter((torrent) => torrent.category.includes('кино'));
|
||||
|
||||
// const links = ((await Promise.all(movies?.map(({ id }) => tracker.getMagnetLink(id)))) || []) as string[];
|
||||
|
||||
// const torrents =
|
||||
// movies?.map(
|
||||
// ({ seeds, title, id, size }, index) =>
|
||||
// ({
|
||||
// seeders: seeds,
|
||||
// title,
|
||||
// id,
|
||||
// magnetLink: links[index],
|
||||
// size: `${(size / (1024 * 1024 * 1024)).toFixed(2)} GiB`
|
||||
// }) as Torrent
|
||||
// ) || [];
|
||||
|
||||
// return NextResponse.json(torrents);
|
||||
// } catch (error) {
|
||||
// return NextResponse.json([{ title: JSON.stringify(error) }]);
|
||||
// }
|
||||
|
||||
const torrents = await tracker.search({ query: params.query, page: parseInt(params.page || '0') });
|
||||
|
||||
return NextResponse.json(torrents);
|
||||
}
|
||||
Reference in new issue
Block a user