[addon] add named queue to run single unrestrict

This commit is contained in:
TheBeastLT
2020-03-18 11:14:43 +01:00
parent b08d46efd4
commit 9c066a88f1
3 changed files with 15 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
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');
@@ -8,6 +9,10 @@ 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))));
async function applyMoch(streams, apiKey) {
const RD = new RealDebridClient(apiKey);
const hashes = streams.map(stream => stream.infoHash);
@@ -32,15 +37,17 @@ async function unrestrict(apiKey, infoHash, cachedFileIds, fileIndex) {
if (!apiKey || !infoHash || !cachedFileIds || !cachedFileIds.length) {
return Promise.reject("No valid parameters passed");
}
const key = `${apiKey}_${infoHash}_${fileIndex}`;
return cacheWrapUnrestricted(key, () => _unrestrict(apiKey, infoHash, cachedFileIds, fileIndex))
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) {
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
const RD = new RealDebridClient(apiKey);
const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedFileIds);
console.log(`Retrieved torrentId: ${torrentId}`);
if (torrentId) {
const info = await RD.torrents.info(torrentId);
const targetFile = info.files.find(file => file.id === fileIndex + 1)
@@ -80,10 +87,7 @@ async function _unrestrictLink(RD, link) {
return Promise.reject("No available links found");
}
return RD._post('unrestrict/link', { form: { link }, proxy: getProxy() })
.then(unrestrictedLink => {
console.log(`Unrestricted ${link} to ${unrestrictedLink.download}`);
return Promise.resolve(unrestrictedLink.download);
});
.then(unrestrictedLink => unrestrictedLink.download);
// .then(unrestrictedLink => RD.streaming.transcode(unrestrictedLink.id))
// .then(transcodedLink => {
// const url = transcodedLink.apple && transcodedLink.apple.full

View File

@@ -11,6 +11,7 @@
"cache-manager": "^2.9.0",
"cache-manager-mongodb": "^0.2.1",
"express-rate-limit": "^5.1.1",
"named-queue": "^2.2.1",
"needle": "^2.2.4",
"magnet-uri": "^5.1.7",
"parse-torrent-title": "git://github.com/TheBeastLT/parse-torrent-title.git#7259b01bfe6e1fbc3879ba68d9c58ebac84029e9",

View File

@@ -71,10 +71,10 @@ router.get('/:configuration/:resource/:type/:id.json', (req, res, next) => {
router.get('/realdebrid/:apiKey/:infoHash/:cachedFileIds/:fileIndex?', (req, res) => {
const { apiKey, infoHash, cachedFileIds, fileIndex } = req.params;
console.time(infoHash);
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
realDebrid.unrestrict(apiKey, infoHash, cachedFileIds, isNaN(fileIndex) ? undefined : parseInt(fileIndex))
.then(url => {
console.timeEnd(infoHash);
console.log(`Unrestricted ${infoHash} [${fileIndex}] to ${url}`);
res.writeHead(301, { Location: url });
res.end();
})