diff --git a/addon/lib/configuration.js b/addon/lib/configuration.js index 79f77cd..ef4df8d 100644 --- a/addon/lib/configuration.js +++ b/addon/lib/configuration.js @@ -10,6 +10,9 @@ function parseConfiguration(configuration) { if (configValues.providers) { configValues.providers = configValues.providers.split(',').map(provider => provider.toLowerCase()); } + if (configValues.debridoptions) { + configValues.debridoptions = configValues.debridoptions.split(',').map(option => option.toLowerCase()); + } return configValues; } diff --git a/addon/moch/moch.js b/addon/moch/moch.js index 9a61f55..daa0b71 100644 --- a/addon/moch/moch.js +++ b/addon/moch/moch.js @@ -33,37 +33,27 @@ async function applyMochs(streams, config) { if (!streams || !streams.length) { return streams; } + + const onlyCached = options.onlyCachedLinks(config); + const onlyCachedIfAvailable = options.onlyCachedLinksIfAvailable(config); const includeDownloadLinks = options.includeDownloadLinks(config); - return Promise.all(Object.keys(config) + const configuredMochs = Object.keys(config) .filter(configKey => MOCHS[configKey]) - .map(configKey => MOCHS[configKey]) + .map(configKey => MOCHS[configKey]); + const mochResults = await Promise.all(configuredMochs .map(moch => moch.instance.getCachedStreams(streams, config[moch.key]) .then(mochStreams => ({ moch, mochStreams })) .catch(error => console.warn(error)))) - .then(mochResults => mochResults - .filter(result => result && result.mochStreams) - .reduce((resultStreams, { moch, mochStreams }) => { - resultStreams - .filter(stream => stream.infoHash) - .filter(stream => mochStreams[stream.infoHash]) - .forEach(stream => { - 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)); + .then(results => results.filter(result => result && result.mochStreams)); + const cachedStreams = mochResults + .reduce((resultStreams, mochResult) => populateCachedLinks(resultStreams, mochResult), streams); + const hasCachedStreams = cachedStreams.find(stream => stream.url); + + const resultStreams = includeDownloadLinks ? populateDownloadLinks(cachedStreams, mochResults) : cachedStreams; + return onlyCached || onlyCachedIfAvailable && hasCachedStreams + ? resultStreams.filter(stream => stream.url) + : resultStreams; } async function resolve(parameters) { @@ -87,4 +77,36 @@ async function resolve(parameters) { })); } +function populateCachedLinks(streams, mochResult) { + streams + .filter(stream => stream.infoHash) + .forEach(stream => { + const cachedEntry = mochResult.mochStreams[stream.infoHash]; + if (cachedEntry && cachedEntry.cached) { + stream.name = `[${mochResult.moch.shortName}+] ${stream.name}`; + stream.url = `${RESOLVER_HOST}/${mochResult.moch.key}/${cachedEntry.url}`; + delete stream.infoHash; + delete stream.fileIndex; + } + }); + return streams; +} + +function populateDownloadLinks(streams, mochResults) { + streams + .filter(stream => stream.infoHash) + .forEach(stream => mochResults + .forEach(mochResult => { + const cachedEntry = mochResult.mochStreams[stream.infoHash]; + if (!cachedEntry || !cachedEntry.cached) { + streams.push({ + name: `[${mochResult.moch.shortName} download] ${stream.name}`, + title: stream.title, + url: `${RESOLVER_HOST}/${mochResult.moch.key}/${cachedEntry.url}` + }) + } + })); + return streams; +} + module.exports = { applyMochs, resolve } \ No newline at end of file diff --git a/addon/moch/options.js b/addon/moch/options.js index 7daea76..7202634 100644 --- a/addon/moch/options.js +++ b/addon/moch/options.js @@ -1,12 +1,12 @@ const DebridOptions = { key: 'debridoptions', options: { - cachedLinks: { - key: 'cachedlinks', + onlyCached: { + key: 'onlycached', description: 'Show only cached debrid links' }, - cachedLinksIfAvailable: { - key: 'cachedlinksifavailable', + onlyCachedIfAvailable: { + key: 'onlycachedifavailable', description: 'Show only cached debrid links if available' }, downloadLinks: { @@ -18,12 +18,12 @@ const DebridOptions = { function onlyCachedLinks(config) { return config[DebridOptions.key] && config[DebridOptions.key] - .includes(DebridOptions.options.cachedLinks.key); + .includes(DebridOptions.options.onlyCached.key); } function onlyCachedLinksIfAvailable(config) { return config[DebridOptions.key] && config[DebridOptions.key] - .includes(DebridOptions.options.cachedLinksIfAvailable.key); + .includes(DebridOptions.options.onlyCachedIfAvailable.key); } function includeDownloadLinks(config) {