[addon] removes moch url resolving from addon

This commit is contained in:
TheBeastLT
2020-03-19 12:29:52 +01:00
parent 084744b35b
commit d5a82fc72d
6 changed files with 44 additions and 116 deletions

View File

@@ -1,17 +1,7 @@
const { encode } = require('magnet-uri');
const RealDebridClient = require('real-debrid-api');
const namedQueue = require('named-queue');
const isVideo = require('../lib/video');
const { cacheWrapUnrestricted } = require('../lib/cache');
const ADDON_HOST = process.env.ADDON_HOST || 'http://localhost:7050';
const PROXY_HOST = process.env.PROXY_HOST;
const PROXY_USERNAME = process.env.PROXY_USERNAME;
const PROXY_PASSWORD = process.env.PROXY_PASSWORD;
const unrestrictQueue = new namedQueue((task, callback) => task.method()
.then(result => callback(false, result))
.catch((error => callback(error))));
const RESOLVER_HOST = process.env.RESOLVER_HOST || 'http://localhost:7000';
async function applyMoch(streams, apiKey) {
const RD = new RealDebridClient(apiKey);
@@ -20,10 +10,10 @@ async function applyMoch(streams, apiKey) {
if (available) {
streams.forEach(stream => {
const cachedEntry = available[stream.infoHash];
const cachedIds = getCachedFileIds(stream.fileIdx, cachedEntry);
if (cachedIds && cachedIds.length) {
const cachedIds = getCachedFileIds(stream.fileIdx, cachedEntry).join(',');
if (cachedIds.length) {
stream.name = `[RD Cached]\n${stream.name}`;
stream.url = `${ADDON_HOST}/realdebrid/${apiKey}/${stream.infoHash}/${cachedIds.join(',')}/${stream.fileIdx}`;
stream.url = `${RESOLVER_HOST}/realdebrid/${apiKey}/${stream.infoHash}/${cachedIds}/${stream.fileIdx}`;
delete stream.infoHash;
delete stream.fileIndex;
}
@@ -33,47 +23,6 @@ async function applyMoch(streams, apiKey) {
return streams;
}
async function unrestrict(apiKey, infoHash, cachedFileIds, fileIndex) {
if (!apiKey || !infoHash || !cachedFileIds || !cachedFileIds.length) {
return Promise.reject("No valid parameters passed");
}
const id = `${apiKey}_${infoHash}_${fileIndex}`;
const method = () => cacheWrapUnrestricted(id, () => _unrestrict(apiKey, infoHash, cachedFileIds, fileIndex));
return new Promise(((resolve, reject) => {
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
}));
}
async function _unrestrict(apiKey, infoHash, cachedFileIds, fileIndex) {
const RD = new RealDebridClient(apiKey);
const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedFileIds);
if (torrentId) {
const info = await RD.torrents.info(torrentId);
const targetFile = info.files.find(file => file.id === fileIndex + 1)
|| info.files.filter(file => file.selected).sort((a, b) => b.bytes - a.bytes)[0];
const selectedFiles = info.files.filter(file => file.selected);
const fileLink = info.links.length === 1
? info.links[0]
: info.links[selectedFiles.indexOf(targetFile)];
return _unrestrictLink(RD, fileLink);
}
return Promise.reject("Failed adding torrent");
}
async function _createOrFindTorrentId(RD, infoHash, cachedFileIds) {
return RD.torrents.get(0, 1)
.then(torrents => torrents.find(torrent => torrent.hash.toLowerCase() === infoHash))
.then(torrent => torrent && torrent.id || Promise.reject('No recent torrent found'))
.catch((error) => RD.torrents.addMagnet(encode({ infoHash }))
.then(response => RD.torrents.selectFiles(response.id, cachedFileIds)
.then((() => response.id))))
.catch(error => {
console.warn('Failed RealDebrid torrent retrieval', error);
return undefined;
});
}
async function _instantAvailability(RD, hashes) {
return RD._get(`torrents/instantAvailability/${hashes.join('/')}`)
.catch(error => {
@@ -82,21 +31,6 @@ async function _instantAvailability(RD, hashes) {
});
}
async function _unrestrictLink(RD, link) {
if (!link || !link.length) {
return Promise.reject("No available links found");
}
return RD._post('unrestrict/link', { form: { link }, proxy: getProxy() })
.then(unrestrictedLink => unrestrictedLink.download);
// .then(unrestrictedLink => RD.streaming.transcode(unrestrictedLink.id))
// .then(transcodedLink => {
// const url = transcodedLink.apple && transcodedLink.apple.full
// || transcodedLink[Object.keys(transcodedLink)[0]].full;
// console.log(`Unrestricted ${link} to ${url}`);
// return url;
// });
}
function getCachedFileIds(fileIndex, hosterResults) {
if (!hosterResults || Array.isArray(hosterResults)) {
return [];
@@ -111,11 +45,4 @@ function getCachedFileIds(fileIndex, hosterResults) {
return cachedTorrents.length && cachedTorrents[0] || [];
}
function getProxy() {
if (PROXY_HOST && PROXY_USERNAME && PROXY_PASSWORD) {
return `http://${PROXY_USERNAME}:${PROXY_PASSWORD}@${PROXY_HOST}`;
}
return undefined;
}
module.exports = { applyMoch, unrestrict };
module.exports = { applyMoch };