[addon] adds debrid download link option an logic

This commit is contained in:
TheBeastLT
2020-05-10 21:33:53 +02:00
parent 3662eda2bc
commit 0c8c813756
5 changed files with 82 additions and 35 deletions

View File

@@ -17,18 +17,18 @@ async function getCachedStreams(streams, apiKey) {
console.warn('Failed AllDebrid cached torrent availability request: ', error); console.warn('Failed AllDebrid cached torrent availability request: ', error);
return undefined; return undefined;
}); });
return available && available.data && available.data.magnets return available && available.data && streams
.filter(magnet => magnet.instant) .reduce((mochStreams, stream) => {
.reduce((cachedStreams, magnet) => { const cachedEntry = available.data.magnets.find(magnet => stream.infoHash === magnet.hash.toLowerCase());
const stream = streams.find(stream => stream.infoHash === magnet.hash.toLowerCase()); const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n');
if (stream) { const fileName = streamTitleParts[streamTitleParts.length - 1];
const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null;
const fileName = streamTitleParts[streamTitleParts.length - 1]; const encodedFileName = encodeURIComponent(fileName);
const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null; mochStreams[stream.infoHash] = {
const encodedFileName = encodeURIComponent(fileName); url: `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`,
cachedStreams[stream.infoHash] = `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`; cached: cachedEntry && cachedEntry.instant
} }
return cachedStreams; return mochStreams;
}, {}) }, {})
} }

View File

@@ -1,3 +1,4 @@
const options = require('./options');
const realdebrid = require('./realdebrid'); const realdebrid = require('./realdebrid');
const premiumize = require('./premiumize'); const premiumize = require('./premiumize');
const alldebrid = require('./alldebrid'); const alldebrid = require('./alldebrid');
@@ -25,24 +26,34 @@ async function applyMochs(streams, config) {
if (!streams || !streams.length) { if (!streams || !streams.length) {
return streams; return streams;
} }
const includeDownloadLinks = options.includeDownloadLinks(config);
return Promise.all(Object.keys(config) return Promise.all(Object.keys(config)
.filter(configKey => MOCHS[configKey]) .filter(configKey => MOCHS[configKey])
.map(configKey => MOCHS[configKey]) .map(configKey => MOCHS[configKey])
.map(moch => moch.instance.getCachedStreams(streams, config[moch.key]) .map(moch => moch.instance.getCachedStreams(streams, config[moch.key])
.then(cachedStreams => ({ moch, cachedStreams })) .then(mochStreams => ({ moch, mochStreams }))
.catch(error => console.warn(error)))) .catch(error => console.warn(error))))
.then(mochResults => mochResults .then(mochResults => mochResults
.filter(result => result && result.cachedStreams) .filter(result => result && result.mochStreams)
.reduce((resultStreams, { moch, cachedStreams }) => { .reduce((resultStreams, { moch, mochStreams }) => {
resultStreams resultStreams
.filter(stream => stream.infoHash) .filter(stream => stream.infoHash)
.filter(stream => cachedStreams[stream.infoHash]) .filter(stream => mochStreams[stream.infoHash])
.forEach(stream => { .forEach(stream => {
stream.name = `[${moch.shortName}+] ${stream.name}`; const cachedEntry = mochStreams[stream.infoHash];
stream.url = `${RESOLVER_HOST}/${moch.key}/${cachedStreams[stream.infoHash]}`; if (cachedEntry.cached) {
delete stream.infoHash; stream.name = `[${moch.shortName}+] ${stream.name}`;
delete stream.fileIndex; 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; return resultStreams;
}, streams)); }, streams));

34
addon/moch/options.js Normal file
View File

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

View File

@@ -19,16 +19,16 @@ async function getCachedStreams(streams, apiKey) {
return undefined; return undefined;
}); });
return available && streams return available && streams
.reduce((cachedStreams, stream, index) => { .reduce((mochStreams, stream, index) => {
const isCached = available.response[index]; const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n');
if (isCached) { const fileName = streamTitleParts[streamTitleParts.length - 1];
const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null;
const fileName = streamTitleParts[streamTitleParts.length - 1]; const encodedFileName = encodeURIComponent(fileName);
const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null; mochStreams[stream.infoHash] = {
const encodedFileName = encodeURIComponent(fileName); url: `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`,
cachedStreams[stream.infoHash] = `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`; cached: available.response[index]
} };
return cachedStreams; return mochStreams;
}, {}) }, {})
} }

View File

@@ -22,13 +22,15 @@ async function getCachedStreams(streams, apiKey) {
return undefined; return undefined;
}); });
return available && streams return available && streams
.reduce((cachedStreams, stream) => { .reduce((mochStreams, stream) => {
const cachedEntry = available[stream.infoHash]; const cachedEntry = available[stream.infoHash];
const cachedIds = _getCachedFileIds(stream.fileIdx, cachedEntry).join(','); const cachedIds = _getCachedFileIds(stream.fileIdx, cachedEntry);
if (cachedIds.length) { const cachedIdsString = cachedIds.length ? cachedIds.join(',') : null;
cachedStreams[stream.infoHash] = `${apiKey}/${stream.infoHash}/${cachedIds}/${stream.fileIdx}`; mochStreams[stream.infoHash] = {
} url: `${apiKey}/${stream.infoHash}/${cachedIdsString}/${stream.fileIdx}`,
return cachedStreams; cached: !!cachedIdsString
};
return mochStreams;
}, {}) }, {})
} }