log: migration

This commit is contained in:
ner committed 2024-10-27 16:49:12 +02:00
commit 1fe912f4bd
16 files changed
+199 -372

No files matched your search

-3
View File
@@ -3,7 +3,6 @@
import { cookies } from 'next/headers';
import mapMoviesSeriesResponseToShows from './dto/Show';
import tlk from './parsers/tlk';
import http from './clients/theMovieDb';
export const session = () => cookies().get('session')?.value;
@@ -74,5 +73,3 @@ export const usersShows = async ({
})
.then((response) => mapMoviesSeriesResponseToShows(response, showType));
};
export const fetchTLKMagnet = async (url: string) => tlk.magnet(url);
+25 -14
View File
@@ -1,8 +1,9 @@
import axios, { AxiosInstance } from 'axios';
import parseTorrent from 'parse-torrent';
import torrentTitle from 'parse-torrent-title';
import { load } from 'cheerio';
import parseTorrent, { Instance } from 'parse-torrent';
import { CookieJar } from 'tough-cookie';
import { wrapper } from 'axios-cookiejar-support';
@@ -16,9 +17,9 @@ export class Toloka {
[Sort.seeds]: 10
};
private magnets: Record<string, string> = {};
private cookieJar = new CookieJar();
cookieJar = new CookieJar();
private magnets: Record<string, string> = {};
constructor() {
this.host = process.env.TOLOKA_HOST || 'https://toloka.to';
@@ -32,11 +33,11 @@ export class Toloka {
);
}
async isAuthorized(data: string) {
private async isAuthorized(data: string) {
return !load(data)('[href="/login.php"]').length;
}
async auth() {
private async auth() {
const data = new FormData();
data.append('username', process.env.TOLOKA_USERNAME as string);
@@ -97,22 +98,32 @@ export class Toloka {
return parseTorrents(page.data);
}
async magnet(url: string): Promise<string> {
async magnet(url: string) {
if (this.magnets[url]) return this.magnets[url];
const { data } = await this.client.get(`/${url}`, {
responseType: 'arraybuffer'
});
const fetchTorrent = async () => {
const { data: buffer } = await this.client.get(`/${url}`, {
responseType: 'arraybuffer'
});
const torrent = await parseTorrent(data);
return parseTorrent(buffer);
};
const magnet = `magnet:?xt=urn:btih:${torrent.infoHash}&dn=${encodeURIComponent(
torrent.name as string
)}&tr=${torrent.announce?.join('&tr=')}`;
let torrent: Instance;
try {
torrent = (await fetchTorrent()) as Instance;
} catch (_) {
await this.auth();
torrent = (await fetchTorrent()) as Instance;
}
const magnet = `magnet:?xt=urn:btih:${torrent.infoHash}&dn=${encodeURIComponent(torrent.name?.toString() || '')}`;
this.magnets[url] = magnet;
return magnet;
return this.magnets[url];
}
}