feat: added the pirate bay parser

This commit is contained in:
ner committed 2023-08-18 11:26:27 +03:00
1 parent 74e35267f9
commit ada725072f
6 files changed
+74 -4755

No files matched your search

+63
View File
@@ -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;
-99
View File
@@ -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;