diff --git a/addon/moch/alldebrid.js b/addon/moch/alldebrid.js index 1ca3716..ca26952 100644 --- a/addon/moch/alldebrid.js +++ b/addon/moch/alldebrid.js @@ -17,18 +17,18 @@ async function getCachedStreams(streams, apiKey) { console.warn('Failed AllDebrid cached torrent availability request: ', error); return undefined; }); - return available && available.data && available.data.magnets - .filter(magnet => magnet.instant) - .reduce((cachedStreams, magnet) => { - const stream = streams.find(stream => stream.infoHash === magnet.hash.toLowerCase()); - if (stream) { - const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); - const fileName = streamTitleParts[streamTitleParts.length - 1]; - const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null; - const encodedFileName = encodeURIComponent(fileName); - cachedStreams[stream.infoHash] = `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`; + return available && available.data && streams + .reduce((mochStreams, stream) => { + const cachedEntry = available.data.magnets.find(magnet => stream.infoHash === magnet.hash.toLowerCase()); + const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); + const fileName = streamTitleParts[streamTitleParts.length - 1]; + const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null; + const encodedFileName = encodeURIComponent(fileName); + mochStreams[stream.infoHash] = { + url: `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`, + cached: cachedEntry && cachedEntry.instant } - return cachedStreams; + return mochStreams; }, {}) } diff --git a/addon/moch/moch.js b/addon/moch/moch.js index b9687fb..470ba07 100644 --- a/addon/moch/moch.js +++ b/addon/moch/moch.js @@ -1,3 +1,4 @@ +const options = require('./options'); const realdebrid = require('./realdebrid'); const premiumize = require('./premiumize'); const alldebrid = require('./alldebrid'); @@ -25,24 +26,34 @@ async function applyMochs(streams, config) { if (!streams || !streams.length) { return streams; } + const includeDownloadLinks = options.includeDownloadLinks(config); return Promise.all(Object.keys(config) .filter(configKey => MOCHS[configKey]) .map(configKey => MOCHS[configKey]) .map(moch => moch.instance.getCachedStreams(streams, config[moch.key]) - .then(cachedStreams => ({ moch, cachedStreams })) + .then(mochStreams => ({ moch, mochStreams })) .catch(error => console.warn(error)))) .then(mochResults => mochResults - .filter(result => result && result.cachedStreams) - .reduce((resultStreams, { moch, cachedStreams }) => { + .filter(result => result && result.mochStreams) + .reduce((resultStreams, { moch, mochStreams }) => { resultStreams .filter(stream => stream.infoHash) - .filter(stream => cachedStreams[stream.infoHash]) + .filter(stream => mochStreams[stream.infoHash]) .forEach(stream => { - stream.name = `[${moch.shortName}+] ${stream.name}`; - stream.url = `${RESOLVER_HOST}/${moch.key}/${cachedStreams[stream.infoHash]}`; - delete stream.infoHash; - delete stream.fileIndex; + const cachedEntry = mochStreams[stream.infoHash]; + if (cachedEntry.cached) { + stream.name = `[${moch.shortName}+] ${stream.name}`; + stream.url = `${RESOLVER_HOST}/${moch.key}/${cachedEntry.url}`; + delete stream.infoHash; + delete stream.fileIndex; + } else if (includeDownloadLinks) { + resultStreams.push({ + name: `[${moch.shortName} download] ${stream.name}`, + title: stream.title, + url: `${RESOLVER_HOST}/${moch.key}/${cachedEntry.url}` + }) + } }); return resultStreams; }, streams)); diff --git a/addon/moch/options.js b/addon/moch/options.js new file mode 100644 index 0000000..215218c --- /dev/null +++ b/addon/moch/options.js @@ -0,0 +1,34 @@ +const DebridOptions = { + key: 'debridoptions', + options: { + cachedlinks: { + key: 'cachedlinks', + description: 'Show only cached debrid links' + }, + cachedlinksifavailable: { + key: 'cachedlinksifavailable', + description: 'Show only cached debrid links if available' + }, + downloadlinks: { + key: 'downloadlinks', + description: 'Show download to debrid links for uncached' + } + } +} + +function onlyCachedLinks(config) { + return config[DebridOptions.key] && config[DebridOptions.key] + .includes(DebridOptions.options.cachedlinks.key); +} + +function onlyCachedLinksIfAvailable(config) { + return config[DebridOptions.key] && config[DebridOptions.key] + .includes(DebridOptions.options.cachedlinksifavailable.key); +} + +function includeDownloadLinks(config) { + return config[DebridOptions.key] && config[DebridOptions.key] + .includes(DebridOptions.options.downloadlinks.key); +} + +module.exports = { DebridOptions, onlyCachedLinks, onlyCachedLinksIfAvailable, includeDownloadLinks } \ No newline at end of file diff --git a/addon/moch/premiumize.js b/addon/moch/premiumize.js index 8ddaae6..ed99e55 100644 --- a/addon/moch/premiumize.js +++ b/addon/moch/premiumize.js @@ -19,16 +19,16 @@ async function getCachedStreams(streams, apiKey) { return undefined; }); return available && streams - .reduce((cachedStreams, stream, index) => { - const isCached = available.response[index]; - if (isCached) { - const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); - const fileName = streamTitleParts[streamTitleParts.length - 1]; - const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null; - const encodedFileName = encodeURIComponent(fileName); - cachedStreams[stream.infoHash] = `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`; - } - return cachedStreams; + .reduce((mochStreams, stream, index) => { + const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); + const fileName = streamTitleParts[streamTitleParts.length - 1]; + const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null; + const encodedFileName = encodeURIComponent(fileName); + mochStreams[stream.infoHash] = { + url: `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`, + cached: available.response[index] + }; + return mochStreams; }, {}) } diff --git a/addon/moch/realdebrid.js b/addon/moch/realdebrid.js index c7bae4d..9d25fab 100644 --- a/addon/moch/realdebrid.js +++ b/addon/moch/realdebrid.js @@ -22,13 +22,15 @@ async function getCachedStreams(streams, apiKey) { return undefined; }); return available && streams - .reduce((cachedStreams, stream) => { + .reduce((mochStreams, stream) => { const cachedEntry = available[stream.infoHash]; - const cachedIds = _getCachedFileIds(stream.fileIdx, cachedEntry).join(','); - if (cachedIds.length) { - cachedStreams[stream.infoHash] = `${apiKey}/${stream.infoHash}/${cachedIds}/${stream.fileIdx}`; - } - return cachedStreams; + const cachedIds = _getCachedFileIds(stream.fileIdx, cachedEntry); + const cachedIdsString = cachedIds.length ? cachedIds.join(',') : null; + mochStreams[stream.infoHash] = { + url: `${apiKey}/${stream.infoHash}/${cachedIdsString}/${stream.fileIdx}`, + cached: !!cachedIdsString + }; + return mochStreams; }, {}) }