fix: toloka auth

This commit is contained in:
ner committed 2024-10-26 15:11:23 +03:00
1 parent 335e26aa76
commit 1111712d2c
13 files changed
+613 -230

No files matched your search

+2 -2
View File
@@ -5,7 +5,7 @@ import { cookies } from 'next/headers';
import { createUniqueRandomGenerator } from '@/lib/utils';
import mapMoviesSeriesResponseToShows, { mapMovieSeriesToShow } from './dto/Show';
import toloka from './parsers/toloka';
import tlk from './parsers/tlk';
import http from './Http';
const session = () => cookies().get('session')?.value;
@@ -235,4 +235,4 @@ export const credits = async ({
});
};
export const fetchTLKMagnet = async (url: string) => toloka.magnet(url);
export const fetchTLKMagnet = async (url: string) => tlk.magnet(url);
+121
View File
@@ -0,0 +1,121 @@
import axios, { AxiosInstance } from 'axios';
import parseTorrent from 'parse-torrent';
import torrentTitle from 'parse-torrent-title';
import { load } from 'cheerio';
import { CookieJar } from 'tough-cookie';
import { wrapper } from 'axios-cookiejar-support';
import { Sort } from '../index';
export class Toloka {
private client: AxiosInstance;
private host: string;
private sort = {
[Sort.size]: 7,
[Sort.seeds]: 10
};
private magnets: Record<string, string> = {};
cookieJar = new CookieJar();
constructor() {
this.host = process.env.TOLOKA_HOST || 'https://toloka.to';
this.client = wrapper(
axios.create({
jar: this.cookieJar,
baseURL: this.host,
withCredentials: true
})
);
}
async isAuthorized(data: string) {
return !load(data)('[href="/login.php"]').length;
}
async auth() {
const data = new FormData();
data.append('username', process.env.TOLOKA_USERNAME as string);
data.append('password', process.env.TOLOKA_PASSWORD as string);
data.append('autologin', 'on');
data.append('ssl', 'on');
data.append('login', 'Вхід');
await this.client.post('/login.php', data, {
headers: { 'Content-Type': 'multipart/form-data' }
});
}
async search({ query, sort = Sort.size }: { query: string; sort?: Sort }): Promise<Torrent[]> {
const fetchPage = () => this.client.get(`/tracker.php?o=${this.sort[sort]}&nm=${query}`);
const parseTorrents = (data: string) => {
const $ = load(data);
const rows = $('.prow1, .prow2');
return rows
.map((_, row) => {
const originalTitle = $(row).find('td:nth-child(3)').text();
const size = $(row).find('td:nth-child(7)').text();
const seeders = $(row).find('td:nth-child(10)').text();
const download = $(row).find('td:nth-child(6) a').attr('href');
const { year, source, codec, container, title, season, episode, resolution } = torrentTitle.parse(
originalTitle || ''
);
return {
year: year?.toString(),
source,
codec,
container,
originalTitle,
title: `${title}${season && episode ? ` [S${season}:E${episode}]` : ''}`,
id: originalTitle || '',
size,
seeders: parseInt(seeders || '0'),
quality: resolution,
magnet: '',
download
};
})
.get();
};
let page = await fetchPage();
if (await this.isAuthorized(page.data)) return parseTorrents(page.data);
await this.auth();
page = await fetchPage();
return parseTorrents(page.data);
}
async magnet(url: string): Promise<string> {
if (this.magnets[url]) return this.magnets[url];
const { data } = await this.client.get(`/${url}`, {
responseType: 'arraybuffer'
});
const torrent = await parseTorrent(data);
const magnet = `magnet:?xt=urn:btih:${torrent.infoHash}&dn=${encodeURIComponent(
torrent.name as string
)}&tr=${torrent.announce?.join('&tr=')}`;
this.magnets[url] = magnet;
return magnet;
}
}
const parser = new Toloka();
export default parser;
-82
View File
@@ -1,82 +0,0 @@
import axios, { AxiosInstance } from 'axios';
import { parse } from 'node-html-parser';
import parseTorrent from 'parse-torrent';
import torrentTitle from 'parse-torrent-title';
import { Sort } from './index';
export class Toloka {
private client: AxiosInstance;
private sort = {
[Sort.size]: 7,
[Sort.seeds]: 10
};
private magnets: Record<string, string> = {};
constructor(private host: string = process.env.TOLOKA_HOST || 'https://toloka.to') {
this.host = host;
this.client = axios.create({
baseURL: this.host,
headers: { Cookie: 'toloka_sid=e9693224d00c5b60af50c2596d1b880d; toloka_ssl=1' }
});
}
async search({ query, sort = Sort.size }: { query: string; sort?: Sort }): Promise<Torrent[]> {
return this.client.get(`/tracker.php?o=${this.sort[sort]}&nm=${query}`).then(({ data }) => {
const html = parse(data);
const rows = html?.querySelectorAll('.prow1, .prow2');
console.log(data);
return rows?.length
? (rows?.map((row) => {
const originalTitle = row.querySelector('td:nth-child(3)')?.innerText;
const size = row.querySelector('td:nth-child(7)')?.innerText;
const seeders = row.querySelector('td:nth-child(10)')?.innerText;
const download = row.querySelector('td:nth-child(6) a')?.getAttribute('href');
const { year, source, codec, container, title, season, episode, resolution } = torrentTitle.parse(
originalTitle || ''
);
return {
year,
source,
codec,
container,
originalTitle,
title: `${title}${season && episode ? ` [S${season}:E${episode}]` : ''}`,
id: originalTitle,
size,
seeders: parseInt(seeders || '0'),
quality: resolution,
magnet: '',
download
};
}) as Torrent[])
: data;
});
}
async magnet(url: string): Promise<string> {
if (this.magnets[url]) return this.magnets[url];
const { data } = await this.client.get(`/${url}`, {
responseType: 'arraybuffer'
});
const torrent = await parseTorrent(data);
const magnet = `magnet:?xt=urn:btih:${torrent.infoHash}&dn=${encodeURIComponent(
torrent.name as string
)}&tr=${torrent.announce?.join('&tr=')}`;
this.magnets[url] = magnet;
return magnet;
}
}
const parser = new Toloka();
export default parser;
@@ -1,18 +1,20 @@
import axios, { AxiosInstance } from 'axios';
import { parse } from 'node-html-parser';
import { load } from 'cheerio';
import torrentTitle from 'parse-torrent-title';
import { Sort } from './index';
import { Sort } from '../index';
export class PirateBay {
private client: AxiosInstance;
private host: string;
private sort = {
[Sort.size]: 5,
[Sort.seeds]: 7
};
constructor(private host: string = process.env.PIRATE_BAY_HOST || 'https://thepiratebay10.org') {
this.host = host;
constructor() {
this.host = process.env.PIRATE_BAY_HOST || 'https://thepiratebay10.info';
this.client = axios.create({
baseURL: this.host
});
@@ -30,23 +32,28 @@ export class PirateBay {
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)');
const $ = load(data);
const table = $('#searchResult');
const rows = table.find('tr:not(.header)');
const titles = rows?.map((row) => row.querySelector('td:nth-child(2) a')?.innerText).filter(Boolean);
const seeders = rows?.map((row) => row.querySelector('td:nth-child(6)')?.innerText);
const titles = rows
.map((_, row) => $(row).find('td:nth-child(2) a').text())
.get()
.filter(Boolean);
const seeders = rows.map((_, row) => $(row).find('td:nth-child(6)').text()).get();
const sizes = rows?.reduce((sizes, row) => {
const size = row.querySelector('td:nth-child(5)')?.innerText;
const sizes = rows
.map((_, row) => {
const size = $(row).find('td:nth-child(5)').text();
return size ? size.replace('&nbsp;', ' ').replace('Size ', '') : null;
})
.get()
.filter(Boolean);
if (!size) return sizes;
sizes.push(size.replace('&nbsp;', ' ').replace('Size ', ''));
return sizes;
}, [] as string[]);
const magnets = table?.querySelectorAll('a[href^="magnet:"]').map((link) => link.getAttribute('href'));
const magnets = table
.find('a[href^="magnet:"]')
.map((_, link) => $(link).attr('href'))
.get();
return titles.map((originalTitle, index) => {
const { title, resolution, source, codec, container, season, episode, year } = torrentTitle.parse(
@@ -54,7 +61,7 @@ export class PirateBay {
);
return {
year,
year: year?.toString(),
source,
codec,
container,
@@ -62,11 +69,11 @@ export class PirateBay {
title: `${title} ${season && episode ? `[S${season}:E${episode}]` : ''}`,
id: title,
size: sizes[index],
seeders: parseInt(seeders[index]!),
seeders: parseInt(seeders[index] || '0', 10),
quality: resolution,
magnet: magnets[index]!
magnet: magnets[index] || ''
};
}) as Torrent[];
});
});
}
}
+4 -1
View File
@@ -6,8 +6,11 @@ import { YTSResponse } from './models';
export class YTS {
private client: AxiosInstance;
private host: string;
constructor() {
this.host = process.env.YTS_HOST || 'https://yts.mx/api/v2/list_movies.json';
constructor(private host: string = process.env.YTS_HOST || 'https://yts.mx/api/v2/list_movies.json') {
this.client = axios.create({
baseURL: this.host
});