[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) { if (configValues.providers) {
configValues.providers = configValues.providers.split(',').map(provider => provider.toLowerCase()); configValues.providers = configValues.providers.split(',').map(provider => provider.toLowerCase());
} }
if (configValues.debridoptions) {
configValues.debridoptions = configValues.debridoptions.split(',').map(option => option.toLowerCase());
}
return configValues; return configValues;
} }

View File

@@ -33,37 +33,27 @@ async function applyMochs(streams, config) {
if (!streams || !streams.length) { if (!streams || !streams.length) {
return streams; return streams;
} }
const onlyCached = options.onlyCachedLinks(config);
const onlyCachedIfAvailable = options.onlyCachedLinksIfAvailable(config);
const includeDownloadLinks = options.includeDownloadLinks(config); const includeDownloadLinks = options.includeDownloadLinks(config);
return Promise.all(Object.keys(config) const configuredMochs = Object.keys(config)
.filter(configKey => MOCHS[configKey]) .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]) .map(moch => moch.instance.getCachedStreams(streams, config[moch.key])
.then(mochStreams => ({ moch, mochStreams })) .then(mochStreams => ({ moch, mochStreams }))
.catch(error => console.warn(error)))) .catch(error => console.warn(error))))
.then(mochResults => mochResults .then(results => results.filter(result => result && result.mochStreams));
.filter(result => result && result.mochStreams) const cachedStreams = mochResults
.reduce((resultStreams, { moch, mochStreams }) => { .reduce((resultStreams, mochResult) => populateCachedLinks(resultStreams, mochResult), streams);
resultStreams const hasCachedStreams = cachedStreams.find(stream => stream.url);
.filter(stream => stream.infoHash)
.filter(stream => mochStreams[stream.infoHash]) const resultStreams = includeDownloadLinks ? populateDownloadLinks(cachedStreams, mochResults) : cachedStreams;
.forEach(stream => { return onlyCached || onlyCachedIfAvailable && hasCachedStreams
const cachedEntry = mochStreams[stream.infoHash]; ? resultStreams.filter(stream => stream.url)
if (cachedEntry.cached) { : resultStreams;
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));
} }
async function resolve(parameters) { 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 } module.exports = { applyMochs, resolve }

View File

@@ -1,12 +1,12 @@
const DebridOptions = { const DebridOptions = {
key: 'debridoptions', key: 'debridoptions',
options: { options: {
cachedLinks: { onlyCached: {
key: 'cachedlinks', key: 'onlycached',
description: 'Show only cached debrid links' description: 'Show only cached debrid links'
}, },
cachedLinksIfAvailable: { onlyCachedIfAvailable: {
key: 'cachedlinksifavailable', key: 'onlycachedifavailable',
description: 'Show only cached debrid links if available' description: 'Show only cached debrid links if available'
}, },
downloadLinks: { downloadLinks: {
@@ -18,12 +18,12 @@ const DebridOptions = {
function onlyCachedLinks(config) { function onlyCachedLinks(config) {
return config[DebridOptions.key] && config[DebridOptions.key] return config[DebridOptions.key] && config[DebridOptions.key]
.includes(DebridOptions.options.cachedLinks.key); .includes(DebridOptions.options.onlyCached.key);
} }
function onlyCachedLinksIfAvailable(config) { function onlyCachedLinksIfAvailable(config) {
return config[DebridOptions.key] && config[DebridOptions.key] return config[DebridOptions.key] && config[DebridOptions.key]
.includes(DebridOptions.options.cachedLinksIfAvailable.key); .includes(DebridOptions.options.onlyCachedIfAvailable.key);
} }
function includeDownloadLinks(config) { function includeDownloadLinks(config) {