From 8691c7d66d25b44aac0ed6e9ab4cdb21e4338491 Mon Sep 17 00:00:00 2001 From: ner Date: Sun, 27 Oct 2024 12:13:02 +0200 Subject: [PATCH] feat: fetch toloka's magnets --- api/parsers/tlk/index.ts | 24 +++++++---- app/api/magnet/route.ts | 5 +-- .../components/Actions/index.tsx | 42 ++++++++----------- .../components/Actions/useMagnet.ts | 14 ++++++- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/api/parsers/tlk/index.ts b/api/parsers/tlk/index.ts index cb5ee29..a673a06 100644 --- a/api/parsers/tlk/index.ts +++ b/api/parsers/tlk/index.ts @@ -2,7 +2,7 @@ import axios, { AxiosInstance } from 'axios'; import torrentTitle from 'parse-torrent-title'; import { load } from 'cheerio'; -import parseTorrent from 'parse-torrent'; +import parseTorrent, { Instance } from 'parse-torrent'; import { CookieJar } from 'tough-cookie'; import { wrapper } from 'axios-cookiejar-support'; @@ -65,8 +65,6 @@ export class Toloka { const seeders = $(row).find('td:nth-child(10)').text(); const download = $(row).find('td:nth-child(6) a').attr('href'); - const sid = this.cookieJar.toJSON()?.cookies.find(({ key }) => key === 'toloka_sid')?.value; - const { year, source, codec, container, title, season, episode, resolution } = torrentTitle.parse( originalTitle || '' ); @@ -103,11 +101,23 @@ export class Toloka { async magnet(url: string) { if (this.magnets[url]) return this.magnets[url]; - const { data: buffer } = await this.client.get(`/${url}`, { - responseType: 'arraybuffer' - }); + const fetchTorrent = async () => { + const { data: buffer } = await this.client.get(`/${url}`, { + responseType: 'arraybuffer' + }); - const torrent = await parseTorrent(buffer); + return parseTorrent(buffer); + }; + + 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() || '')}`; diff --git a/app/api/magnet/route.ts b/app/api/magnet/route.ts index ac5d16d..2b26927 100644 --- a/app/api/magnet/route.ts +++ b/app/api/magnet/route.ts @@ -7,8 +7,7 @@ export async function GET({ nextUrl: { searchParams } }: NextRequest) { try { return NextResponse.json(await tlk.magnet(url)); - } catch (_) { - console.log('error', _); - return NextResponse.json([]); + } catch (error) { + return NextResponse.error(); } } 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 2ae3c47..d54ae00 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx @@ -1,10 +1,9 @@ import { FC } from 'react'; -import { Cast, ListVideo, Magnet, Play } from 'lucide-react'; +import { Cast, ChevronLeft,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'; @@ -24,8 +23,6 @@ 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 && @@ -33,49 +30,46 @@ const Actions: FC = ({ torrent, backdrop, title, provider }) => { torrent.container?.includes('mp4') ); - const magnet = useMagnet(torrent); + const fetchMagnet = useMagnet(torrent); + + const magnet = fetchMagnet.data || torrent.magnet; const supportedForCast = prefix && !prefix.includes('{host}'); + if (!magnet) + return ( + + + + ); + return ( {supportedForStream && ( )} - - {supportedForCast && ( - )} diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/useMagnet.ts b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/useMagnet.ts index 13969da..cc6fb67 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/useMagnet.ts +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/useMagnet.ts @@ -2,8 +2,18 @@ import { useMutation } from '@tanstack/react-query'; const useMagnet = (torrent: Torrent) => { return useMutation({ - mutationFn: async () => - torrent.magnet ? torrent.magnet : fetch(`/api/magnet?url=${torrent.download}`).then((response) => response.json()) + retry: 4, + mutationFn: async () => { + try { + const response = await fetch(`/api/magnet?url=${torrent.download}`); + + if (!response.ok) return Promise.reject(); + + return response.json(); + } catch (error) { + return Promise.reject(error); + } + } }); };