From d5a82fc72d611735ca27b10dcdcf28967ffb0172 Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Thu, 19 Mar 2020 12:29:52 +0100 Subject: [PATCH] [addon] removes moch url resolving from addon --- addon/lib/cache.js | 16 +------- addon/moch/realdebrid.js | 83 +++------------------------------------- addon/package-lock.json | 37 ++++++++++++++++++ addon/package.json | 2 - addon/serverless.js | 17 -------- now.json | 5 +-- 6 files changed, 44 insertions(+), 116 deletions(-) diff --git a/addon/lib/cache.js b/addon/lib/cache.js index d6be8c7..5b158ab 100644 --- a/addon/lib/cache.js +++ b/addon/lib/cache.js @@ -3,9 +3,7 @@ const mangodbStore = require('cache-manager-mongodb'); const GLOBAL_KEY_PREFIX = 'torrentio-addon'; const STREAM_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|stream`; -const REALDEBRID_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|realdebrid`; -const REDIRECT_TTL = process.env.STREAM_EMPTY_TTL || 10; // 5 minutes const STREAM_TTL = process.env.STREAM_TTL || 4 * 60 * 60; // 4 hours const STREAM_EMPTY_TTL = process.env.STREAM_EMPTY_TTL || 30 * 60; // 30 minutes // When the streams are empty we want to cache it for less time in case of timeouts or failures @@ -14,7 +12,6 @@ const MONGO_URI = process.env.MONGODB_URI; const NO_CACHE = process.env.NO_CACHE || false; const remoteCache = initiateCache(); -const memoryCache = initiateMemoryCache(); function initiateCache() { if (NO_CACHE) { @@ -39,13 +36,6 @@ function initiateCache() { } } -function initiateMemoryCache() { - return cacheManager.caching({ - store: 'memory', - ttl: 60 - }); -} - function cacheWrap(cache, key, method, options) { if (NO_CACHE || !cache) { return method(); @@ -59,9 +49,5 @@ function cacheWrapStream(id, method) { }); } -function cacheWrapUnrestricted(id, method) { - return cacheWrap(memoryCache, `${REALDEBRID_KEY_PREFIX}:${id}`, method, { ttl: REDIRECT_TTL }); -} - -module.exports = { cacheWrapStream, cacheWrapUnrestricted }; +module.exports = { cacheWrapStream }; diff --git a/addon/moch/realdebrid.js b/addon/moch/realdebrid.js index 712fd42..88f195e 100644 --- a/addon/moch/realdebrid.js +++ b/addon/moch/realdebrid.js @@ -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 }; \ No newline at end of file +module.exports = { applyMoch }; \ No newline at end of file diff --git a/addon/package-lock.json b/addon/package-lock.json index 5b75ff5..1869cca 100644 --- a/addon/package-lock.json +++ b/addon/package-lock.json @@ -1435,6 +1435,31 @@ "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", "dev": true }, + "needle": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/needle/-/needle-2.3.3.tgz", + "integrity": "sha512-EkY0GeSq87rWp1hoq/sH/wnTWgFVhYlnIkbJ0YJFfRgEFlz2RraCjBpFQ+vrEgEdp0ThfyHADmkChEhcb7PKyw==", + "requires": { + "debug": "^3.2.6", + "iconv-lite": "^0.4.4", + "sax": "^1.2.4" + }, + "dependencies": { + "debug": { + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", + "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", + "requires": { + "ms": "^2.1.1" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "negotiator": { "version": "0.6.2", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", @@ -1527,6 +1552,13 @@ "callsites": "^3.0.0" } }, + "parse-torrent-title": { + "version": "git://github.com/TheBeastLT/parse-torrent-title.git#7259b01bfe6e1fbc3879ba68d9c58ebac84029e9", + "from": "git://github.com/TheBeastLT/parse-torrent-title.git#7259b01bfe6e1fbc3879ba68d9c58ebac84029e9", + "requires": { + "moment": "^2.24.0" + } + }, "parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -1895,6 +1927,11 @@ "sparse-bitfield": "^3.0.3" } }, + "sax": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", + "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" + }, "semver": { "version": "5.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", diff --git a/addon/package.json b/addon/package.json index fe4e410..1b6381d 100644 --- a/addon/package.json +++ b/addon/package.json @@ -11,9 +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", "pg": "^7.8.2", "pg-hstore": "^2.3.2", diff --git a/addon/serverless.js b/addon/serverless.js index 6dbe541..31fc885 100644 --- a/addon/serverless.js +++ b/addon/serverless.js @@ -4,7 +4,6 @@ const addonInterface = require('./addon'); const { manifest } = require('./lib/manifest'); const parseConfiguration = require('./lib/configuration'); const landingTemplate = require('./lib/landingTemplate'); -const realDebrid = require('./moch/realdebrid'); const router = getRouter(addonInterface); const limiter = rateLimit({ @@ -69,22 +68,6 @@ 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.log(`Unrestricting ${infoHash} [${fileIndex}]`); - realDebrid.unrestrict(apiKey, infoHash, cachedFileIds, isNaN(fileIndex) ? undefined : parseInt(fileIndex)) - .then(url => { - console.log(`Unrestricted ${infoHash} [${fileIndex}] to ${url}`); - res.writeHead(301, { Location: url }); - res.end(); - }) - .catch(error => { - console.log(error); - res.statusCode = 404; - res.end(); - }); -}); - module.exports = function (req, res) { router(req, res, function () { res.statusCode = 404; diff --git a/now.json b/now.json index d6a2e38..ef9dfed 100644 --- a/now.json +++ b/now.json @@ -22,10 +22,7 @@ "env": { "MONGODB_URI": "@mongodb-uri", "DATABASE_URI": "@database-uri", - "ADDON_HOST": "@torrentio-addon-host", - "PROXY_HOST": "@torrentio-proxy-host", - "PROXY_USERNAME": "@torrentio-proxy-username", - "PROXY_PASSWORD": "@torrentio-proxy-password" + "RESOLVER_HOST": "@torrentio-resolver-host" } }