fix: toloka magnet links

This commit is contained in:
ner committed 2024-10-26 23:24:09 +03:00
1 parent ee782f3845
commit 4f4e6b1307
10 files changed
+35 -734

No files matched your search

-2
View File
@@ -234,5 +234,3 @@ export const credits = async ({
});
});
};
export const fetchTLKMagnet = async (url: string) => tlk.magnet(url);
+3 -22
View File
@@ -1,5 +1,4 @@
import axios, { AxiosInstance } from 'axios';
import parseTorrent from 'parse-torrent';
import torrentTitle from 'parse-torrent-title';
import { load } from 'cheerio';
@@ -16,8 +15,6 @@ export class Toloka {
[Sort.seeds]: 10
};
private magnets: Record<string, string> = {};
cookieJar = new CookieJar();
constructor() {
@@ -64,6 +61,8 @@ 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 || ''
);
@@ -80,7 +79,7 @@ export class Toloka {
seeders: parseInt(seeders || '0'),
quality: resolution,
magnet: '',
download
download: sid ? `${this.host}/${download}&sid=${sid}` : ''
};
})
.get();
@@ -96,24 +95,6 @@ export class Toloka {
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();
@@ -1,10 +1,12 @@
import { useQuery } from '@tanstack/react-query';
import { STREAM_URL as BASE_STREAM_URL } from '../../constants';
export type Source = { name: string; src: string; type: string };
type Sources = { subtitles: Source[]; videos: Source[] };
const useFiles = (magnet: string) => {
const STREAM_URL = `${process.env.NEXT_PUBLIC_TORRENT_PROXY}/stream?link=${encodeURIComponent(magnet)}`;
const STREAM_URL = `${BASE_STREAM_URL}${encodeURIComponent(magnet)}`;
const query = useQuery<Stream>({
queryKey: ['files', magnet],
@@ -10,8 +10,6 @@ import { providers } from '../../../../constants';
import { useCastMagnet, usePrefix } from '../../../../hooks/useMagnetHosts';
import showPlayer from '../../../Player';
import useMagnet from './useMagnet';
interface Props {
torrent: Torrent;
backdrop: string;
@@ -22,7 +20,6 @@ interface Props {
const Actions: FC<Props> = ({ torrent, backdrop, title, provider }) => {
const prefix = usePrefix();
const cast = useCastMagnet();
const { mutateAsync, isPending } = useMagnet();
const supportedForStream = Boolean(
(provider === providers.yts.key &&
@@ -33,55 +30,37 @@ const Actions: FC<Props> = ({ torrent, backdrop, title, provider }) => {
const supportedForCast = prefix && !prefix.includes('{host}');
const prefetchMagnet = (handler: (magnet: string) => void) => () => {
if (!torrent.download) return handler(torrent.magnet);
return mutateAsync(torrent.download).then((magnet) => {
console.log('magnet', magnet);
return handler(magnet);
});
};
return (
<ButtonsGroup className='ml-auto w-fit grow-0'>
{supportedForStream && (
<Button
disabled={isPending}
variant='destructive'
className={cn('flex-grow-0 px-3 bg-red-600')}
onClick={prefetchMagnet((magnet) => showPlayer({ magnet, backdrop, title }))}
onClick={() => showPlayer({ magnet: torrent.download || torrent.magnet, backdrop, title })}
title='Play'
>
<Play size={15} />
</Button>
)}
<Button
disabled={isPending}
variant='outline'
className='relative grow-0 px-3'
title='Play using your native video player'
onClick={prefetchMagnet((magnet) => {
window.open(`${process.env.NEXT_PUBLIC_TORRENT_PROXY}/stream?m3u&link=${encodeURIComponent(magnet)}`);
})}
>
<Button variant='outline' className='relative grow-0 px-3' title='Play using your native video player'>
<a
className='absolute left-0 top-0 size-full'
href={`${process.env.NEXT_PUBLIC_TORRENT_PROXY}/stream?m3u&link=${encodeURIComponent(
torrent.download || torrent.magnet
)}`}
/>
<ListVideo size={20} />
</Button>
<Button
disabled={isPending}
className='relative grow-0 px-3'
variant='outline'
onClick={prefetchMagnet((magnet) => window.open(magnet))}
title='Download magnet'
title={`Download ${torrent.download ? 'torrent' : 'magnet'}`}
>
<a className='absolute left-0 top-0 size-full' href={torrent.download || torrent.magnet} />
<Magnet size={20} />
</Button>
{supportedForCast && (
<Button
disabled={isPending}
className='grow-0 px-3'
variant='outline'
onClick={prefetchMagnet((magnet) => cast(magnet))}
>
<Button className='grow-0 px-3' variant='outline' onClick={() => cast(torrent.download || torrent.magnet)}>
<Cast size={20} />
</Button>
)}
@@ -1,9 +0,0 @@
import { useMutation } from '@tanstack/react-query';
import { fetchTLKMagnet } from '@/api';
const useMagnet = () => {
return useMutation({ mutationFn: fetchTLKMagnet });
};
export default useMagnet;
@@ -1,5 +1,7 @@
import { Sort } from '@/api/parsers';
export const STREAM_URL = `${process.env.NEXT_PUBLIC_TORRENT_PROXY}/stream?link=`;
type Options = { query: string; sort: Sort; imdbID: string };
const queryFn = ({ query, sort, imdbID, key }: Options & { key: keyof typeof providers }) =>
@@ -4,7 +4,9 @@ import { atomWithStorage } from 'jotai/utils';
import showHostManagerModal from '@/components/Preview/components/Torrents/components/HostManager';
const magnetPrefixAtom = atomWithStorage<string>('magnet-prefix', 'http://{host}:65220/playuri?uri=', undefined, {
import { STREAM_URL } from '../constants';
const magnetPrefixAtom = atomWithStorage<string>('magnet-prefix', 'http://{host}:9090/magnet?magnet=', undefined, {
unstable_getOnInit: true
});
@@ -16,7 +18,15 @@ export const useCastMagnet = () => {
const prefix = usePrefix();
return useCallback(
(magnet: string) => {
async (link: string) => {
let magnet = link;
if (link && !link.startsWith('magnet')) {
magnet = await fetch(`${STREAM_URL}${encodeURIComponent(link)}&stat=true`)
.then((response) => response.json())
.then((torrent: Stream) => `magnet:?xt=urn:btih:${torrent.hash}&dn=${encodeURIComponent(torrent.name)}`);
}
const open = (prefix: string) => {
const link = window.open(`${prefix}${magnet}`, '_blank');
setTimeout(() => link?.close(), 1000);
+3 -663
View File
File diff suppressed because it is too large. Load diff
-2
View File
@@ -10,7 +10,6 @@
"prepare": "husky"
},
"dependencies": {
"@next/bundle-analyzer": "^15.0.1",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-dropdown-menu": "^2.1.2",
"@radix-ui/react-slot": "^1.0.2",
@@ -30,7 +29,6 @@
"media-icons": "^1.1.5",
"next": "^14.2.7",
"next-pwa": "^5.6.0",
"parse-torrent": "^11.0.17",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-modal-promise": "^1.0.2",
+1 -1
View File
@@ -1 +1 @@
if(!self.define){let e,s={};const n=(n,i)=>(n=new URL(n+".js",i).href,s[n]||new Promise((s=>{if("document"in self){const e=document.createElement("script");e.src=n,e.onload=s,document.head.appendChild(e)}else e=n,importScripts(n),s()})).then((()=>{let e=s[n];if(!e)throw new Error(`Module ${n} didn’t register its module`);return e})));self.define=(i,t)=>{const a=e||("document"in self?document.currentScript.src:"")||location.href;if(s[a])return;let c={};const r=e=>n(e,a),o={module:{uri:a},exports:c,require:r};s[a]=Promise.all(i.map((e=>o[e]||r(e)))).then((e=>(t(...e),c)))}}define(["./workbox-4754cb34"],(function(e){"use strict";importScripts(),self.skipWaiting(),e.clientsClaim(),e.precacheAndRoute([{url:"/_next/app-build-manifest.json",revision:"9ca4cf4934614ee2884f54c0a4ae74f9"},{url:"/_next/static/chunks/106-e4df334a8b6054b2.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/152.e619d8b05424829e.js",revision:"e619d8b05424829e"},{url:"/_next/static/chunks/23-e95fe3862375276d.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/26.286553a5ff2812fc.js",revision:"286553a5ff2812fc"},{url:"/_next/static/chunks/276.c3c1130f2adade69.js",revision:"c3c1130f2adade69"},{url:"/_next/static/chunks/311-d98f807f8da8ea75.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/344.05938a041227e582.js",revision:"05938a041227e582"},{url:"/_next/static/chunks/37-f6e2aef0dac1a9f4.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/383.78d47f7582340f53.js",revision:"78d47f7582340f53"},{url:"/_next/static/chunks/460.ac20021c7496f1d6.js",revision:"ac20021c7496f1d6"},{url:"/_next/static/chunks/497.a16beaad36f500fe.js",revision:"a16beaad36f500fe"},{url:"/_next/static/chunks/61.e269d4a699fba0b6.js",revision:"e269d4a699fba0b6"},{url:"/_next/static/chunks/621-fa4d2ff1cf645813.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/673.362c29137dd3e968.js",revision:"362c29137dd3e968"},{url:"/_next/static/chunks/695.156d21364de6e419.js",revision:"156d21364de6e419"},{url:"/_next/static/chunks/755.5cd959eac5682527.js",revision:"5cd959eac5682527"},{url:"/_next/static/chunks/792.b51012549074b93f.js",revision:"b51012549074b93f"},{url:"/_next/static/chunks/810.6105e84414768b0d.js",revision:"6105e84414768b0d"},{url:"/_next/static/chunks/922.9ce4bf0553cfb724.js",revision:"9ce4bf0553cfb724"},{url:"/_next/static/chunks/957-9f7dee59febd35c3.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/app/(site)/favorites/page-2ad2720a689c7bf7.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/app/(site)/loading-96645736797edde7.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/app/(site)/page-550186c61d98ee57.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/app/(site)/popular/page-0c29524eddadf350.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/app/(site)/similar/page-1126cc975a9ba802.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/app/(site)/starring/page-0fbb653750c18be1.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/app/(site)/top/page-75f0b7e736133ad6.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/app/(site)/watch-list/page-38ccba9f9dff3e6c.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/app/_not-found/page-aa0c8e90320336c2.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/app/layout-74481e5fe3b4935f.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/c8bcbd21.63daf74955ffea8b.js",revision:"63daf74955ffea8b"},{url:"/_next/static/chunks/fd9d1056-bdf7d185613ab264.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/framework-f66176bb897dc684.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/main-65cc2be174f92f9a.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/main-app-51cdaff1a81e6b49.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/pages/_app-6a626577ffa902a4.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/pages/_error-1be831200e60c5c0.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/chunks/polyfills-42372ed130431b0a.js",revision:"846118c33b2c0e922d7b3a7676f81f6f"},{url:"/_next/static/chunks/webpack-522f7df141f474e5.js",revision:"vcsU5PL_nCIbZtw1TiXgA"},{url:"/_next/static/css/0d5f6452b2682b9b.css",revision:"0d5f6452b2682b9b"},{url:"/_next/static/css/af880b2e4df9cb52.css",revision:"af880b2e4df9cb52"},{url:"/_next/static/media/46c21389e888bf13-s.woff2",revision:"272930c09ba14c81bb294be1fe18b049"},{url:"/_next/static/media/8888a3826f4a3af4-s.p.woff2",revision:"792477d09826b11d1e5a611162c9797a"},{url:"/_next/static/media/eafabf029ad39a43-s.p.woff2",revision:"43751174b6b810eb169101a20d8c26f8"},{url:"/_next/static/media/f5767adec246cdc1-s.woff2",revision:"7a1c6501aa2b3327c1cf556362a851cb"},{url:"/_next/static/vcsU5PL_nCIbZtw1TiXgA/_buildManifest.js",revision:"2ec694eb52ae4f523f265a46bae4d768"},{url:"/_next/static/vcsU5PL_nCIbZtw1TiXgA/_ssgManifestLine truncated
if(!self.define){let e,s={};const c=(c,i)=>(c=new URL(c+".js",i).href,s[c]||new Promise((s=>{if("document"in self){const e=document.createElement("script");e.src=c,e.onload=s,document.head.appendChild(e)}else e=c,importScripts(c),s()})).then((()=>{let e=s[c];if(!e)throw new Error(`Module ${c} didn’t register its module`);return e})));self.define=(i,n)=>{const a=e||("document"in self?document.currentScript.src:"")||location.href;if(s[a])return;let t={};const r=e=>c(e,a),o={module:{uri:a},exports:t,require:r};s[a]=Promise.all(i.map((e=>o[e]||r(e)))).then((e=>(n(...e),t)))}}define(["./workbox-4754cb34"],(function(e){"use strict";importScripts(),self.skipWaiting(),e.clientsClaim(),e.precacheAndRoute([{url:"/_next/app-build-manifest.json",revision:"6715767eaaf023e68da769f355c964dc"},{url:"/_next/static/Xb1HyVXc3SiXrE1cLlDYW/_buildManifest.js",revision:"2ec694eb52ae4f523f265a46bae4d768"},{url:"/_next/static/Xb1HyVXc3SiXrE1cLlDYW/_ssgManifest.js",revision:"b6652df95db52feb4daf4eca35380933"},{url:"/_next/static/chunks/106-e4df334a8b6054b2.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/152.e619d8b05424829e.js",revision:"e619d8b05424829e"},{url:"/_next/static/chunks/23-e95fe3862375276d.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/26.286553a5ff2812fc.js",revision:"286553a5ff2812fc"},{url:"/_next/static/chunks/276.c3c1130f2adade69.js",revision:"c3c1130f2adade69"},{url:"/_next/static/chunks/344.05938a041227e582.js",revision:"05938a041227e582"},{url:"/_next/static/chunks/37-f6e2aef0dac1a9f4.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/383.78d47f7582340f53.js",revision:"78d47f7582340f53"},{url:"/_next/static/chunks/460.ac20021c7496f1d6.js",revision:"ac20021c7496f1d6"},{url:"/_next/static/chunks/497.a16beaad36f500fe.js",revision:"a16beaad36f500fe"},{url:"/_next/static/chunks/61.e269d4a699fba0b6.js",revision:"e269d4a699fba0b6"},{url:"/_next/static/chunks/621-fa4d2ff1cf645813.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/673.362c29137dd3e968.js",revision:"362c29137dd3e968"},{url:"/_next/static/chunks/695.156d21364de6e419.js",revision:"156d21364de6e419"},{url:"/_next/static/chunks/755.5cd959eac5682527.js",revision:"5cd959eac5682527"},{url:"/_next/static/chunks/792.b51012549074b93f.js",revision:"b51012549074b93f"},{url:"/_next/static/chunks/810.6105e84414768b0d.js",revision:"6105e84414768b0d"},{url:"/_next/static/chunks/922.9ce4bf0553cfb724.js",revision:"9ce4bf0553cfb724"},{url:"/_next/static/chunks/957-9b7477753e8c8e13.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/967-989563256ba892f5.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/app/(site)/favorites/page-5c70e54f244fc439.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/app/(site)/loading-96645736797edde7.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/app/(site)/page-931b2031c553cbce.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/app/(site)/popular/page-2952450eb72040b4.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/app/(site)/similar/page-7643a7bf835ea1bb.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/app/(site)/starring/page-95dba534befcb881.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/app/(site)/top/page-ed6905f3b1275cda.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/app/(site)/watch-list/page-507593c119ab8810.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/app/_not-found/page-aa0c8e90320336c2.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/app/layout-459e579410c99427.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/c8bcbd21.63daf74955ffea8b.js",revision:"63daf74955ffea8b"},{url:"/_next/static/chunks/fd9d1056-bdf7d185613ab264.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/framework-f66176bb897dc684.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/main-65cc2be174f92f9a.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/main-app-51cdaff1a81e6b49.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/pages/_app-6a626577ffa902a4.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/pages/_error-1be831200e60c5c0.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/chunks/polyfills-42372ed130431b0a.js",revision:"846118c33b2c0e922d7b3a7676f81f6f"},{url:"/_next/static/chunks/webpack-38c502f26170c560.js",revision:"Xb1HyVXc3SiXrE1cLlDYW"},{url:"/_next/static/css/0d5f6452b2682b9b.css",revision:"0d5f6452b2682b9b"},{url:"/_next/static/css/af880b2e4df9cb52.css",revision:"af880b2e4df9cb52"},{url:"/_next/static/media/46c21389e888bf13-s.woff2",revision:"272930c09ba14c81bb294be1fe18b049"},{url:"/_next/static/media/8888a3826f4a3af4-s.p.woff2",revision:"792477d09826b11d1e5a611162c9797a"},{url:"/_next/static/media/eafabf029ad39a43-s.p.woff2",revision:"43751174b6b810eb169101a20d8c26f8"},{url:"/_next/static/media/f5767adec246cdc1-s.woLine truncated