mirror of
https://github.com/TheBeastLT/torrentio-scraper.git
synced 2026-01-11 22:40:22 +00:00
fix all debrid link resolving
This commit is contained in:
parent
2a71e689bc
commit
1a86603bdf
2 changed files with 44 additions and 2 deletions
|
|
@ -93,3 +93,31 @@ export function getCachedAvailabilityResults(infoHashes) {
|
||||||
return {};
|
return {};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function cacheMochAvailabilityResult(moch, infoHash, result = { cached: true }) {
|
||||||
|
const key = `${AVAILABILITY_KEY_PREFIX}:${moch}:${infoHash}`;
|
||||||
|
return remoteCache.set(key, result, AVAILABILITY_TTL);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeMochAvailabilityResult(moch, infoHash) {
|
||||||
|
const key = `${AVAILABILITY_KEY_PREFIX}:${moch}:${infoHash}`;
|
||||||
|
return remoteCache.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getMochCachedAvailabilityResults(moch, infoHashes) {
|
||||||
|
const keys = infoHashes.map(infoHash => `${AVAILABILITY_KEY_PREFIX}:${moch}:${infoHash}`)
|
||||||
|
return remoteCache.getMany(keys)
|
||||||
|
.then(result => {
|
||||||
|
const availabilityResults = {};
|
||||||
|
infoHashes.forEach((infoHash, index) => {
|
||||||
|
if (result[index]) {
|
||||||
|
availabilityResults[infoHash] = result[index];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return availabilityResults;
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.log('Failed retrieve availability cache', error)
|
||||||
|
return {};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,17 +4,24 @@ import { isVideo, isArchive } from '../lib/extension.js';
|
||||||
import StaticResponse from './static.js';
|
import StaticResponse from './static.js';
|
||||||
import { getMagnetLink } from '../lib/magnetHelper.js';
|
import { getMagnetLink } from '../lib/magnetHelper.js';
|
||||||
import { BadTokenError, AccessDeniedError, sameFilename, streamFilename, AccessBlockedError } from './mochHelper.js';
|
import { BadTokenError, AccessDeniedError, sameFilename, streamFilename, AccessBlockedError } from './mochHelper.js';
|
||||||
|
import {
|
||||||
|
cacheMochAvailabilityResult,
|
||||||
|
getMochCachedAvailabilityResults,
|
||||||
|
removeMochAvailabilityResult
|
||||||
|
} from "../lib/cache.js";
|
||||||
|
|
||||||
const KEY = 'alldebrid';
|
const KEY = 'alldebrid';
|
||||||
const AGENT = 'torrentio';
|
const AGENT = 'torrentio';
|
||||||
|
|
||||||
export async function getCachedStreams(streams, apiKey, ip) {
|
export async function getCachedStreams(streams, apiKey, ip) {
|
||||||
|
const hashes = streams.map(stream => stream.infoHash);
|
||||||
|
const available = await getMochCachedAvailabilityResults(KEY, hashes);
|
||||||
return streams
|
return streams
|
||||||
.reduce((mochStreams, stream) => {
|
.reduce((mochStreams, stream) => {
|
||||||
const filename = streamFilename(stream);
|
const filename = streamFilename(stream);
|
||||||
mochStreams[`${stream.infoHash}@${stream.fileIdx}`] = {
|
mochStreams[`${stream.infoHash}@${stream.fileIdx}`] = {
|
||||||
url: `${apiKey}/${stream.infoHash}/${filename}/${stream.fileIdx}`,
|
url: `${apiKey}/${stream.infoHash}/${filename}/${stream.fileIdx}`,
|
||||||
cached: false
|
cached: available[stream.infoHash]?.cached || false
|
||||||
}
|
}
|
||||||
return mochStreams;
|
return mochStreams;
|
||||||
}, {})
|
}, {})
|
||||||
|
|
@ -87,6 +94,7 @@ async function _resolve(AD, infoHash, cachedEntryInfo, fileIndex) {
|
||||||
return _unrestrictLink(AD, torrent, cachedEntryInfo, fileIndex);
|
return _unrestrictLink(AD, torrent, cachedEntryInfo, fileIndex);
|
||||||
} else if (statusDownloading(torrent?.statusCode)) {
|
} else if (statusDownloading(torrent?.statusCode)) {
|
||||||
console.log(`Downloading to AllDebrid ${infoHash} [${fileIndex}]...`);
|
console.log(`Downloading to AllDebrid ${infoHash} [${fileIndex}]...`);
|
||||||
|
removeMochAvailabilityResult(KEY, infoHash);
|
||||||
return StaticResponse.DOWNLOADING;
|
return StaticResponse.DOWNLOADING;
|
||||||
} else if (statusHandledError(torrent?.statusCode)) {
|
} else if (statusHandledError(torrent?.statusCode)) {
|
||||||
console.log(`Retrying downloading to AllDebrid ${infoHash} [${fileIndex}]...`);
|
console.log(`Retrying downloading to AllDebrid ${infoHash} [${fileIndex}]...`);
|
||||||
|
|
@ -130,12 +138,17 @@ async function _createTorrent(AD, infoHash) {
|
||||||
const magnetLink = await getMagnetLink(infoHash);
|
const magnetLink = await getMagnetLink(infoHash);
|
||||||
const uploadResponse = await AD.magnet.upload(magnetLink);
|
const uploadResponse = await AD.magnet.upload(magnetLink);
|
||||||
const torrentId = uploadResponse.data.magnets[0].id;
|
const torrentId = uploadResponse.data.magnets[0].id;
|
||||||
|
if (!torrentId) {
|
||||||
|
return Promise.reject(`No magnet added with response: ${JSON.stringify(uploadResponse)}`);
|
||||||
|
}
|
||||||
return AD.magnet.status(torrentId).then(statusResponse => statusResponse.data.magnets);
|
return AD.magnet.status(torrentId).then(statusResponse => statusResponse.data.magnets);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _unrestrictLink(AD, torrent, encodedFileName, fileIndex) {
|
async function _unrestrictLink(AD, torrent, encodedFileName, fileIndex) {
|
||||||
const targetFileName = decodeURIComponent(encodedFileName);
|
const targetFileName = decodeURIComponent(encodedFileName);
|
||||||
const files = getNestedFiles(await AD.magnet.files(torrent.id).then(response => response.data.magnets[0].files[0]));
|
let files = await AD.magnet.files(torrent.id)
|
||||||
|
.then(response => response.data.magnets[0].files)
|
||||||
|
.then(files => getNestedFiles({ e: files }))
|
||||||
const videos = files.filter(link => isVideo(link.n)).sort((a, b) => b.s - a.s);
|
const videos = files.filter(link => isVideo(link.n)).sort((a, b) => b.s - a.s);
|
||||||
const targetVideo = Number.isInteger(fileIndex)
|
const targetVideo = Number.isInteger(fileIndex)
|
||||||
&& videos.find(video => sameFilename(targetFileName, video.n))
|
&& videos.find(video => sameFilename(targetFileName, video.n))
|
||||||
|
|
@ -149,6 +162,7 @@ async function _unrestrictLink(AD, torrent, encodedFileName, fileIndex) {
|
||||||
return Promise.reject(`No AllDebrid links found for [${torrent.hash}] ${encodedFileName}`);
|
return Promise.reject(`No AllDebrid links found for [${torrent.hash}] ${encodedFileName}`);
|
||||||
}
|
}
|
||||||
const unrestrictedLink = await AD.link.unlock(targetVideo.l).then(response => response.data.link);
|
const unrestrictedLink = await AD.link.unlock(targetVideo.l).then(response => response.data.link);
|
||||||
|
cacheMochAvailabilityResult(KEY, torrent.hash.toLowerCase());
|
||||||
console.log(`Unrestricted AllDebrid ${torrent.hash} [${fileIndex}] to ${unrestrictedLink}`);
|
console.log(`Unrestricted AllDebrid ${torrent.hash} [${fileIndex}] to ${unrestrictedLink}`);
|
||||||
return unrestrictedLink;
|
return unrestrictedLink;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue