diff --git a/api/parsers/tlk/index.ts b/api/parsers/tlk/index.ts index 21254fe..cb5ee29 100644 --- a/api/parsers/tlk/index.ts +++ b/api/parsers/tlk/index.ts @@ -2,6 +2,8 @@ import axios, { AxiosInstance } from 'axios'; import torrentTitle from 'parse-torrent-title'; import { load } from 'cheerio'; +import parseTorrent from 'parse-torrent'; + import { CookieJar } from 'tough-cookie'; import { wrapper } from 'axios-cookiejar-support'; @@ -15,7 +17,9 @@ export class Toloka { [Sort.seeds]: 10 }; - cookieJar = new CookieJar(); + private cookieJar = new CookieJar(); + + private magnets: Record = {}; constructor() { this.host = process.env.TOLOKA_HOST || 'https://toloka.to'; @@ -29,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); @@ -79,7 +83,7 @@ export class Toloka { seeders: parseInt(seeders || '0'), quality: resolution, magnet: '', - download: sid ? `${this.host}/${download}&sid=${sid}` : '' + download }; }) .get(); @@ -95,6 +99,22 @@ export class Toloka { return parseTorrents(page.data); } + + async magnet(url: string) { + if (this.magnets[url]) return this.magnets[url]; + + const { data: buffer } = await this.client.get(`/${url}`, { + responseType: 'arraybuffer' + }); + + const torrent = await parseTorrent(buffer); + + const magnet = `magnet:?xt=urn:btih:${torrent.infoHash}&dn=${encodeURIComponent(torrent.name?.toString() || '')}`; + + this.magnets[url] = magnet; + + return this.magnets[url]; + } } const parser = new Toloka(); diff --git a/app/api/magnet/route.ts b/app/api/magnet/route.ts new file mode 100644 index 0000000..ac5d16d --- /dev/null +++ b/app/api/magnet/route.ts @@ -0,0 +1,14 @@ +import { NextRequest, NextResponse } from 'next/server'; + +import tlk from '@/api/parsers/tlk'; + +export async function GET({ nextUrl: { searchParams } }: NextRequest) { + const url = searchParams.get('url') as string; + + try { + return NextResponse.json(await tlk.magnet(url)); + } catch (_) { + console.log('error', _); + return NextResponse.json([]); + } +} diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx index 2567f1a..2ae3c47 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx @@ -4,12 +4,15 @@ import { Cast, ListVideo, Magnet, Play } from 'lucide-react'; import { Quality } from '@/api/parsers/yts/models'; import { Button } from '@/components/ui/button'; import ButtonsGroup from '@/components/ui/buttons-group'; +import { toast, useToast } from '@/hooks/useToast'; import { cn } from '@/lib/utils'; import { providers } from '../../../../constants'; import { useCastMagnet, usePrefix } from '../../../../hooks/useMagnetHosts'; import showPlayer from '../../../Player'; +import useMagnet from './useMagnet'; + interface Props { torrent: Torrent; backdrop: string; @@ -21,6 +24,8 @@ const Actions: FC = ({ torrent, backdrop, title, provider }) => { const prefix = usePrefix(); const cast = useCastMagnet(); + const { toast } = useToast(); + const supportedForStream = Boolean( (provider === providers.yts.key && torrent.seeders && @@ -28,6 +33,8 @@ const Actions: FC = ({ torrent, backdrop, title, provider }) => { torrent.container?.includes('mp4') ); + const magnet = useMagnet(torrent); + const supportedForCast = prefix && !prefix.includes('{host}'); return ( @@ -42,6 +49,14 @@ const Actions: FC = ({ torrent, backdrop, title, provider }) => { )} +