[addon] adds alldebrid download functionality

This commit is contained in:
TheBeastLT
2020-05-11 20:56:18 +02:00
parent c0224558d6
commit b4a4ebf529

View File

@@ -1,6 +1,7 @@
const AllDebridClient = require('all-debrid-api');
const namedQueue = require('named-queue');
const isVideo = require('../lib/video');
const StaticResponse = require('./static');
const { getRandomProxy, getRandomUserAgent } = require('../lib/request_helper');
const { cacheWrapResolvedUrl, cacheWrapProxy, cacheUserAgent } = require('../lib/cache');
@@ -37,7 +38,11 @@ async function resolve({ ip, apiKey, infoHash, cachedEntryInfo, fileIndex }) {
return Promise.reject("No valid parameters passed");
}
const id = `${apiKey}_${infoHash}_${fileIndex}`;
const method = () => cacheWrapResolvedUrl(id, () => _unrestrict(ip, apiKey, infoHash, cachedEntryInfo, fileIndex));
const method = () => cacheWrapResolvedUrl(id, () => _unrestrict(ip, apiKey, infoHash, cachedEntryInfo, fileIndex))
.catch(error => {
console.warn(error);
return StaticResponse.FAILED_UNEXPECTED;
});
return new Promise(((resolve, reject) => {
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
@@ -48,39 +53,63 @@ async function _unrestrict(ip, apiKey, infoHash, encodedFileName, fileIndex) {
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
const options = await getDefaultOptions(apiKey, ip);
const AD = new AllDebridClient(apiKey, options);
const cachedTorrent = await _createOrFindTorrent(AD, infoHash);
if (cachedTorrent && cachedTorrent.status === 'Ready') {
const targetFileName = decodeURIComponent(encodedFileName);
const videos = cachedTorrent.links.filter(link => isVideo(link.filename));
const targetVideo = Number.isInteger(fileIndex)
? videos.find(video => targetFileName.includes(video.filename))
: videos.sort((a, b) => b.size - a.size)[0];
const unrestrictedLink = await _unrestrictLink(AD, targetVideo.link);
console.log(`Unrestricted ${infoHash} [${fileIndex}] to ${unrestrictedLink}`);
return unrestrictedLink;
const torrent = await _createOrFindTorrent(AD, infoHash);
if (torrent && statusReady(torrent.statusCode)) {
return _unrestrictLink(AD, torrent, encodedFileName, fileIndex);
} else if (torrent && statusDownloading(torrent.statusCode)) {
return StaticResponse.DOWNLOADING;
} else if (torrent && statusHandledError(torrent.statusCode)) {
return _retryCreateTorrent(AD, infoHash, encodedFileName, fileIndex);
} else if (torrent && errorExpiredSubscriptionError(torrent)) {
return StaticResponse.FAILED_ACCESS;
}
return Promise.reject("Failed AllDebrid adding torrent");
}
async function _createOrFindTorrent(AD, infoHash) {
return AD.magnet.status()
.then(response => response.data.magnets)
.then(torrents => torrents.find(torrent => torrent.hash === infoHash))
.then(torrent => torrent || Promise.reject('No recent torrent found'))
.catch(() => AD.magnet.upload(infoHash)
.then(response => AD.magnet.status(response.data.magnets[0].id)
.then(statusResponse => statusResponse.data.magnets)))
return _findTorrent(AD, infoHash)
.catch(() => _createTorrent(AD, infoHash))
.catch(error => {
console.warn('Failed AllDebrid torrent retrieval', error);
return undefined;
return error;
});
}
async function _unrestrictLink(AD, link) {
if (!link || !link.length) {
async function _retryCreateTorrent(AD, infoHash, encodedFileName, fileIndex) {
const newTorrentId = await _createTorrent(AD, infoHash);
const newTorrent = await AD.magnet.status(newTorrentId);
return newTorrent && statusReady(newTorrent.statusCode)
? _unrestrictLink(AD, newTorrent, encodedFileName, fileIndex)
: StaticResponse.FAILED_DOWNLOAD;
}
async function _findTorrent(AD, infoHash) {
const torrents = await AD.magnet.status().then(response => response.data.magnets);
const foundTorrents = torrents.filter(torrent => torrent.hash.toLowerCase() === infoHash);
const nonFailedTorrent = foundTorrents.find(torrent => !statusError(torrent.statusCode));
const foundTorrent = nonFailedTorrent || foundTorrents[0];
return foundTorrent || Promise.reject('No recent torrent found');
}
async function _createTorrent(AD, infoHash) {
const uploadResponse = await AD.magnet.upload(infoHash);
const torrentId = uploadResponse.data.magnets[0].id;
return AD.magnet.status(torrentId).then(statusResponse => statusResponse.data.magnets);
}
async function _unrestrictLink(AD, torrent, encodedFileName, fileIndex) {
const targetFileName = decodeURIComponent(encodedFileName);
const videos = torrent.links.filter(link => isVideo(link.filename));
const targetVideo = Number.isInteger(fileIndex)
? videos.find(video => targetFileName.includes(video.filename))
: videos.sort((a, b) => b.size - a.size)[0];
if (!targetVideo.link || !targetVideo.link.length) {
return Promise.reject("No available links found");
}
return AD.link.unlock(link).then(response => response.data.link);
const unrestrictedLink = await AD.link.unlock(targetVideo.link).then(response => response.data.link);
console.log(`Unrestricted ${torrent.hash} [${fileIndex}] to ${unrestrictedLink}`);
return unrestrictedLink;
}
async function getDefaultOptions(id, ip) {
@@ -90,4 +119,25 @@ async function getDefaultOptions(id, ip) {
return { proxy: proxy, headers: { 'User-Agent': userAgent } };
}
function statusError(statusCode) {
return [5, 6, 7, 8, 9, 10, 11].includes(statusCode)
}
function statusHandledError(statusCode) {
return [5, 7, 9, 10].includes(statusCode)
}
function statusDownloading(statusCode) {
return [0, 1, 2, 3].includes(statusCode)
}
function statusReady(statusCode) {
return statusCode === 4;
}
function errorExpiredSubscriptionError(error) {
return ['You must be premium to use this feature', 'You must be premium to process this link']
.includes(error.message);
}
module.exports = { getCachedStreams, resolve };