From 9c066a88f1e531573a44c2e4fe0f5a96da9470f0 Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Wed, 18 Mar 2020 11:14:43 +0100 Subject: [PATCH] [addon] add named queue to run single unrestrict --- addon/moch/realdebrid.js | 20 ++++++++++++-------- addon/package.json | 1 + addon/serverless.js | 4 ++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/addon/moch/realdebrid.js b/addon/moch/realdebrid.js index 9a0dabf..712fd42 100644 --- a/addon/moch/realdebrid.js +++ b/addon/moch/realdebrid.js @@ -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 diff --git a/addon/package.json b/addon/package.json index ada4665..fe4e410 100644 --- a/addon/package.json +++ b/addon/package.json @@ -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", diff --git a/addon/serverless.js b/addon/serverless.js index e773d9a..6dbe541 100644 --- a/addon/serverless.js +++ b/addon/serverless.js @@ -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(); })