[addon] prepares moch structure for multiple providers

This commit is contained in:
TheBeastLT
2020-05-08 21:45:06 +02:00
parent 51144e3833
commit 0e7d7d0b32
6 changed files with 64 additions and 40 deletions

View File

@@ -1,7 +1,12 @@
const realdebrid = require('./realdebrid');
const RESOLVER_HOST = process.env.RESOLVER_HOST || 'http://localhost:7050';
const MOCHS = {
'realdebrid': realdebrid
'realdebrid': {
key: 'realdebrid',
instance: realdebrid,
shortName: 'RD'
}
};
async function applyMochs(streams, config) {
@@ -9,15 +14,34 @@ async function applyMochs(streams, config) {
return streams;
}
return Object.keys(config)
return Promise.all(Object.keys(config)
.filter(configKey => MOCHS[configKey])
.reduce(async (streams, moch) => {
return await MOCHS[moch].applyMoch(streams, config[moch])
.catch(error => {
console.warn(error);
return streams;
});
}, streams);
.map(configKey => MOCHS[configKey])
.map(moch => moch.instance.getCachedStreams(streams, config[moch.key])
.then(cachedStreams => ({ moch, cachedStreams }))
.catch(error => console.warn(error))))
.then(mochResults => mochResults
.filter(result => result && result.cachedStreams)
.reduce((resultStreams, { moch, cachedStreams }) => {
resultStreams
.filter(stream => stream.infoHash)
.filter(stream => cachedStreams[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;
});
return resultStreams;
}, streams));
}
module.exports = applyMochs;
async function resolve(parameters) {
const moch = MOCHS[parameters.mochKey];
if (!moch) {
return Promise.reject('Not a valid moch provider');
}
return moch.instance.resolve(parameters);
}
module.exports = { applyMochs, resolve }

View File

@@ -5,13 +5,11 @@ const isVideo = require('../lib/video');
const { getRandomProxy, getRandomUserAgent } = require('../lib/request_helper');
const { cacheWrapResolvedUrl, cacheWrapProxy, cacheUserAgent } = require('../lib/cache');
const RESOLVER_HOST = process.env.RESOLVER_HOST || 'http://localhost:7050';
const unrestrictQueue = new namedQueue((task, callback) => task.method()
.then(result => callback(false, result))
.catch((error => callback(error))));
async function applyMoch(streams, apiKey) {
async function getCachedStreams(streams, apiKey) {
const options = await getDefaultOptions(apiKey);
const RD = new RealDebridClient(apiKey, options);
const hashes = streams.map(stream => stream.infoHash);
@@ -20,28 +18,23 @@ async function applyMoch(streams, apiKey) {
console.warn('Failed cached torrent availability request: ', error);
return undefined;
});
if (available) {
streams.forEach(stream => {
const cachedEntry = available[stream.infoHash];
const cachedIds = _getCachedFileIds(stream.fileIdx, cachedEntry).join(',');
if (cachedIds.length) {
stream.name = `[RD+] ${stream.name}`;
stream.url = `${RESOLVER_HOST}/realdebrid/${apiKey}/${stream.infoHash}/${cachedIds}/${stream.fileIdx}`;
delete stream.infoHash;
delete stream.fileIndex;
}
});
}
return streams;
return available && streams
.reduce((cachedStreams, 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;
}, {})
}
async function resolve(apiKey, infoHash, cachedFileIds, fileIndex) {
if (!apiKey || !infoHash || !cachedFileIds || !cachedFileIds.length) {
async function resolve({ apiKey, infoHash, cachedEntryInfo, fileIndex }) {
if (!apiKey || !infoHash || !cachedEntryInfo) {
return Promise.reject("No valid parameters passed");
}
const id = `${apiKey}_${infoHash}_${fileIndex}`;
const method = () => cacheWrapResolvedUrl(id, () => _unrestrict(apiKey, infoHash, cachedFileIds, fileIndex));
const method = () => cacheWrapResolvedUrl(id, () => _unrestrict(apiKey, infoHash, cachedEntryInfo, fileIndex));
return new Promise(((resolve, reject) => {
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
@@ -112,9 +105,9 @@ async function _unrestrictLink(RD, link) {
async function getDefaultOptions(id) {
const userAgent = await cacheUserAgent(id, () => getRandomUserAgent()).catch(() => getRandomUserAgent());
const proxy = await cacheWrapProxy('realdebrid', () => getRandomProxy()).catch(() => getRandomProxy());
const proxy = await cacheWrapProxy('moch', () => getRandomProxy()).catch(() => getRandomProxy());
return { proxy: proxy, headers: { 'User-Agent': userAgent } };
}
module.exports = { applyMoch, resolve };
module.exports = { getCachedStreams, resolve };