From 79f41937924ffadf5503aaed1400e829cceb1395 Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Tue, 9 Mar 2021 12:27:52 +0100 Subject: [PATCH] [addon] add debridlink support, closes #19 --- addon/lib/landingTemplate.js | 11 +++ addon/lib/manifest.js | 2 +- addon/moch/debridlink.js | 176 +++++++++++++++++++++++++++++++++++ addon/moch/moch.js | 8 ++ addon/package-lock.json | 10 +- addon/package.json | 3 +- 6 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 addon/moch/debridlink.js diff --git a/addon/lib/landingTemplate.js b/addon/lib/landingTemplate.js index a4f8cab..beaa7aa 100644 --- a/addon/lib/landingTemplate.js +++ b/addon/lib/landingTemplate.js @@ -198,6 +198,7 @@ function landingTemplate(manifest, config = {}) { const realDebridApiKey = config[MochOptions.realdebrid.key] || ''; const premiumizeApiKey = config[MochOptions.premiumize.key] || ''; const allDebridApiKey = config[MochOptions.alldebrid.key] || ''; + const debridLinkApiKey = config[MochOptions.debridlink.key] || ''; const putioKey = config[MochOptions.putio.key] || ''; const putioClientId = putioKey.replace(/@.*/, ''); const putioToken = putioKey.replace(/.*@/, ''); @@ -302,6 +303,11 @@ function landingTemplate(manifest, config = {}) { + +
@@ -343,6 +349,7 @@ function landingTemplate(manifest, config = {}) { $('#iRealDebrid').val("${realDebridApiKey}"); $('#iPremiumize').val("${premiumizeApiKey}"); $('#iAllDebrid').val("${allDebridApiKey}"); + $('#iDebridLink').val("${debridLinkApiKey}"); $('#iPutioClientId').val("${putioClientId}"); $('#iPutioToken').val("${putioToken}"); $('#iSort').val("${sort}"); @@ -366,6 +373,7 @@ function landingTemplate(manifest, config = {}) { $('#dRealDebrid').toggle(provider === '${MochOptions.realdebrid.key}'); $('#dPremiumize').toggle(provider === '${MochOptions.premiumize.key}'); $('#dAllDebrid').toggle(provider === '${MochOptions.alldebrid.key}'); + $('#dDebridLink').toggle(provider === '${MochOptions.debridlink.key}'); $('#dPutio').toggle(provider === '${MochOptions.putio.key}'); } @@ -378,6 +386,7 @@ function landingTemplate(manifest, config = {}) { const debridOptionsValue = $('#iDebridOptions').val().join(',') || ''; const realDebridValue = $('#iRealDebrid').val() || ''; const allDebridValue = $('#iAllDebrid').val() || ''; + const debridLinkValue = $('#iDebridLink').val() || '' const premiumizeValue = $('#iPremiumize').val() || ''; const putioClientIdValue = $('#iPutioClientId').val() || ''; const putioTokenValue = $('#iPutioToken').val() || ''; @@ -392,6 +401,7 @@ function landingTemplate(manifest, config = {}) { const realDebrid = realDebridValue.length && realDebridValue.trim(); const premiumize = premiumizeValue.length && premiumizeValue.trim(); const allDebrid = allDebridValue.length && allDebridValue.trim(); + const debridLink = debridLinkValue.length && debridLinkValue.trim(); const putio = putioClientIdValue.length && putioTokenValue.length && putioClientIdValue.trim() + '@' + putioTokenValue.trim(); let configurationValue = [ @@ -403,6 +413,7 @@ function landingTemplate(manifest, config = {}) { ['${MochOptions.realdebrid.key}', realDebrid], ['${MochOptions.premiumize.key}', premiumize], ['${MochOptions.alldebrid.key}', allDebrid], + ['${MochOptions.debridlink.key}', debridLink], ['${MochOptions.putio.key}', putio] ].filter(([_, value]) => value.length).map(([key, value]) => key + '=' + value).join('|'); configurationValue = '${LiteConfigValue}' === configurationValue ? 'lite' : configurationValue; diff --git a/addon/lib/manifest.js b/addon/lib/manifest.js index 492ab52..ecff326 100644 --- a/addon/lib/manifest.js +++ b/addon/lib/manifest.js @@ -9,7 +9,7 @@ const CatalogMochs = Object.values(MochOptions).filter(moch => moch.catalog); function manifest(config = {}) { return { id: `com.stremio.torrentio${config.lite ? '.lite' : ''}.addon`, - version: '0.0.10', + version: '0.0.11', name: `Torrentio${config.lite ? ' Lite' : ''}`, description: getDescription(config), catalogs: getCatalogs(config), diff --git a/addon/moch/debridlink.js b/addon/moch/debridlink.js new file mode 100644 index 0000000..2bb5b67 --- /dev/null +++ b/addon/moch/debridlink.js @@ -0,0 +1,176 @@ +const DebridLinkClient = require('debrid-link-api'); +const { Type } = require('../lib/types'); +const { isVideo, isArchive } = require('../lib/extension'); +const StaticResponse = require('./static'); +const { getMagnetLink } = require('../lib/magnetHelper'); +const { chunkArray } = require('./mochHelper'); +const delay = require('./delay'); + +const KEY = 'debridlink'; + +async function getCachedStreams(streams, apiKey) { + const options = await getDefaultOptions(); + const DL = new DebridLinkClient(apiKey, options); + const hashBatches = chunkArray(streams.map(stream => stream.infoHash), 50) + .map(batch => batch.join(',')); + const available = await Promise.all(hashBatches.map(hashes => DL.seedbox.cached(hashes))) + .then(results => results.map(result => result.value)) + .then(results => results.reduce((all, result) => Object.assign(all, result), {})) + .catch(error => { + console.warn('Failed DebridLink cached torrent availability request: ', error); + return undefined; + }); + return available && streams + .reduce((mochStreams, stream) => { + const cachedEntry = available[stream.infoHash]; + mochStreams[stream.infoHash] = { + url: `${apiKey}/${stream.infoHash}/null/${stream.fileIdx}`, + cached: !!cachedEntry + }; + return mochStreams; + }, {}) +} + +async function getCatalog(apiKey, offset = 0) { + if (offset > 0) { + return []; + } + const options = await getDefaultOptions(); + const DL = new DebridLinkClient(apiKey, options); + return DL.seedbox.list() + .then(response => response.value) + .then(torrents => (torrents || []) + .filter(torrent => torrent && statusReady(torrent.status)) + .map(torrent => ({ + id: `${KEY}:${torrent.id}`, + type: Type.OTHER, + name: torrent.name + }))); +} + +async function getItemMeta(itemId, apiKey) { + const options = await getDefaultOptions(); + const DL = new DebridLinkClient(apiKey, options); + return DL.seedbox.list(itemId) + .then(response => response.value[0]) + .then(torrent => ({ + id: `${KEY}:${torrent.id}`, + type: Type.OTHER, + name: torrent.name, + videos: torrent.files + .filter(file => isVideo(file.name)) + .map((file, index) => ({ + id: `${KEY}:${torrent.id}:${index}`, + title: file.name, + released: new Date(torrent.created * 1000 - index).toISOString(), + stream: { url: file.downloadUrl } + })) + })) +} + +async function resolve({ ip, apiKey, infoHash, fileIndex }) { + console.log(`Unrestricting DebridLink ${infoHash} [${fileIndex}]`); + const options = await getDefaultOptions(ip); + const DL = new DebridLinkClient(apiKey, options); + + return _resolve(DL, infoHash, fileIndex) + .catch(error => { + if (errorExpiredSubscriptionError(error)) { + console.log(`Access denied to DebridLink ${infoHash} [${fileIndex}]`); + return StaticResponse.FAILED_ACCESS; + } + return Promise.reject(`Failed DebridLink adding torrent ${JSON.stringify(error)}`); + }); +} + +async function _resolve(DL, infoHash, fileIndex) { + const torrent = await _createOrFindTorrent(DL, infoHash); + if (torrent && statusReady(torrent.status)) { + return _unrestrictLink(DL, torrent, fileIndex); + } else if (torrent && statusDownloading(torrent.status)) { + console.log(`Downloading to DebridLink ${infoHash} [${fileIndex}]...`); + return StaticResponse.DOWNLOADING; + } else if (torrent && statusOpening(torrent.status)) { + console.log(`Trying to open torrent on DebridLink ${infoHash} [${fileIndex}]...`); + return _openTorrent(DL, torrent.id) + .then(() => { + console.log(`Downloading to DebridLink ${infoHash} [${fileIndex}]...`); + return StaticResponse.DOWNLOADING + }) + .catch(error => { + console.log(`Failed DebridLink opening torrent ${infoHash} [${fileIndex}]:`, error); + return StaticResponse.FAILED_OPENING; + }); + } + + return Promise.reject(`Failed DebridLink adding torrent ${JSON.stringify(torrent)}`); +} + +async function _createOrFindTorrent(DL, infoHash) { + return _findTorrent(DL, infoHash) + .catch(() => _createTorrent(DL, infoHash)); +} + +async function _findTorrent(DL, infoHash) { + const torrents = await DL.seedbox.list().then(response => response.value); + const foundTorrents = torrents.filter(torrent => torrent.hashString.toLowerCase() === infoHash); + const nonFailedTorrent = foundTorrents.find(torrent => !statusError(torrent.status)); + const foundTorrent = nonFailedTorrent || foundTorrents[0]; + return foundTorrent || Promise.reject('No recent torrent found'); +} + +async function _createTorrent(DL, infoHash) { + const magnetLink = await getMagnetLink(infoHash); + const uploadResponse = await DL.seedbox.add(magnetLink, null, true); + return uploadResponse.value; +} + +async function _openTorrent(DL, torrentId, pollCounter = 0, pollRate = 2000, maxPollNumber = 15) { + return DL.seedbox.list(torrentId) + .then(response => response.value[0]) + .then(torrent => torrent && statusOpening(torrent.status) && pollCounter < maxPollNumber + ? delay(pollRate).then(() => _openTorrent(DL, torrentId, pollCounter + 1)) + : torrent); +} + +async function _unrestrictLink(DL, torrent, fileIndex) { + const targetFile = Number.isInteger(fileIndex) + ? torrent.files[fileIndex] + : torrent.files.filter(file => file.downloadPercent === 100).sort((a, b) => b.size - a.size)[0]; + + if (!targetFile && isArchive(targetFile.downloadUrl)) { + console.log(`Only DebridLink archive is available for [${torrent.hash}] ${fileIndex}`) + return StaticResponse.FAILED_RAR; + } + if (!targetFile || !targetFile.downloadUrl) { + return Promise.reject(`No DebridLink links found for [${torrent.hash}] ${fileIndex}`); + } + console.log(`Unrestricted DebridLink ${torrent.hash} [${fileIndex}] to ${targetFile.downloadUrl}`); + return targetFile.downloadUrl; +} + +async function getDefaultOptions(ip) { + return { timeout: 30000 }; +} + +function statusError(status) { + return [].includes(status); +} + +function statusOpening(status) { + return [2].includes(status); +} + +function statusDownloading(status) { + return [4].includes(status); +} + +function statusReady(status) { + return [6, 100].includes(status); +} + +function errorExpiredSubscriptionError(error) { + return ['freeServerOverload', 'maxTorrent', 'maxLink', 'maxLinkHost', 'maxData', 'maxDataHost'].includes(error); +} + +module.exports = { getCachedStreams, resolve, getCatalog, getItemMeta }; \ No newline at end of file diff --git a/addon/moch/moch.js b/addon/moch/moch.js index 6bae156..60d4325 100644 --- a/addon/moch/moch.js +++ b/addon/moch/moch.js @@ -3,6 +3,7 @@ const options = require('./options'); const realdebrid = require('./realdebrid'); const premiumize = require('./premiumize'); const alldebrid = require('./alldebrid'); +const debridlink = require('./debridlink'); const putio = require('./putio'); const StaticResponse = require('./static'); const { cacheWrapResolvedUrl } = require('../lib/cache'); @@ -31,6 +32,13 @@ const MOCHS = { shortName: 'AD', catalog: true }, + debridlink: { + key: 'debridlink', + instance: debridlink, + name: 'DebridLink', + shortName: 'DL', + catalog: true + }, putio: { key: 'putio', instance: putio, diff --git a/addon/package-lock.json b/addon/package-lock.json index 919fb64..b97973b 100644 --- a/addon/package-lock.json +++ b/addon/package-lock.json @@ -1,6 +1,6 @@ { "name": "stremio-torrentio", - "version": "1.0.10", + "version": "1.0.11", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -456,6 +456,14 @@ "assert-plus": "^1.0.0" } }, + "debrid-link-api": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/debrid-link-api/-/debrid-link-api-1.0.0.tgz", + "integrity": "sha512-FkewWunFaG8Ssqb8bUkE06ogkcBDbvUG6l0TRnvNQcDzDIKaxpef20yFcZR7jAjkeBGv6tkRCoaRYADBtmfbog==", + "requires": { + "request": "^2.83.0" + } + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/addon/package.json b/addon/package.json index 565ace3..13f8098 100644 --- a/addon/package.json +++ b/addon/package.json @@ -1,6 +1,6 @@ { "name": "stremio-torrentio", - "version": "1.0.10", + "version": "1.0.11", "main": "addon.js", "scripts": { "start": "node index.js" @@ -13,6 +13,7 @@ "bottleneck": "^2.19.5", "cache-manager": "^2.11.1", "cache-manager-mongodb": "^0.2.2", + "debrid-link-api": "^1.0.0", "express-rate-limit": "^5.1.1", "https-proxy-agent": "^5.0.0", "magnet-uri": "^5.1.7",