[addon] refactor applyMochs method and include other debrid options

This commit is contained in:
TheBeastLT
2020-05-11 21:54:10 +02:00
parent 8613a6500f
commit a4f28aedc8
3 changed files with 56 additions and 31 deletions

View File

@@ -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;
}

View File

@@ -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 }

View File

@@ -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) {