mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] prepares moch structure for multiple providers
This commit is contained in:
@@ -6,7 +6,7 @@ const { cacheWrapStream } = require('./lib/cache');
|
|||||||
const { toStreamInfo } = require('./lib/streamInfo');
|
const { toStreamInfo } = require('./lib/streamInfo');
|
||||||
const repository = require('./lib/repository');
|
const repository = require('./lib/repository');
|
||||||
const applySorting = require('./lib/sort');
|
const applySorting = require('./lib/sort');
|
||||||
const applyMochs = require('./moch/moch');
|
const { applyMochs } = require('./moch/moch');
|
||||||
|
|
||||||
const CACHE_MAX_AGE = process.env.CACHE_MAX_AGE || 4 * 60 * 60; // 4 hours in seconds
|
const CACHE_MAX_AGE = process.env.CACHE_MAX_AGE || 4 * 60 * 60; // 4 hours in seconds
|
||||||
const CACHE_MAX_AGE_EMPTY = 30 * 60; // 30 minutes
|
const CACHE_MAX_AGE_EMPTY = 30 * 60; // 30 minutes
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
const realdebrid = require('./realdebrid');
|
const realdebrid = require('./realdebrid');
|
||||||
|
|
||||||
|
const RESOLVER_HOST = process.env.RESOLVER_HOST || 'http://localhost:7050';
|
||||||
const MOCHS = {
|
const MOCHS = {
|
||||||
'realdebrid': realdebrid
|
'realdebrid': {
|
||||||
|
key: 'realdebrid',
|
||||||
|
instance: realdebrid,
|
||||||
|
shortName: 'RD'
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function applyMochs(streams, config) {
|
async function applyMochs(streams, config) {
|
||||||
@@ -9,15 +14,34 @@ async function applyMochs(streams, config) {
|
|||||||
return streams;
|
return streams;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.keys(config)
|
return Promise.all(Object.keys(config)
|
||||||
.filter(configKey => MOCHS[configKey])
|
.filter(configKey => MOCHS[configKey])
|
||||||
.reduce(async (streams, moch) => {
|
.map(configKey => MOCHS[configKey])
|
||||||
return await MOCHS[moch].applyMoch(streams, config[moch])
|
.map(moch => moch.instance.getCachedStreams(streams, config[moch.key])
|
||||||
.catch(error => {
|
.then(cachedStreams => ({ moch, cachedStreams }))
|
||||||
console.warn(error);
|
.catch(error => console.warn(error))))
|
||||||
return streams;
|
.then(mochResults => mochResults
|
||||||
});
|
.filter(result => result && result.cachedStreams)
|
||||||
}, streams);
|
.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 }
|
||||||
@@ -5,13 +5,11 @@ const isVideo = require('../lib/video');
|
|||||||
const { getRandomProxy, getRandomUserAgent } = require('../lib/request_helper');
|
const { getRandomProxy, getRandomUserAgent } = require('../lib/request_helper');
|
||||||
const { cacheWrapResolvedUrl, cacheWrapProxy, cacheUserAgent } = require('../lib/cache');
|
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()
|
const unrestrictQueue = new namedQueue((task, callback) => task.method()
|
||||||
.then(result => callback(false, result))
|
.then(result => callback(false, result))
|
||||||
.catch((error => callback(error))));
|
.catch((error => callback(error))));
|
||||||
|
|
||||||
async function applyMoch(streams, apiKey) {
|
async function getCachedStreams(streams, apiKey) {
|
||||||
const options = await getDefaultOptions(apiKey);
|
const options = await getDefaultOptions(apiKey);
|
||||||
const RD = new RealDebridClient(apiKey, options);
|
const RD = new RealDebridClient(apiKey, options);
|
||||||
const hashes = streams.map(stream => stream.infoHash);
|
const hashes = streams.map(stream => stream.infoHash);
|
||||||
@@ -20,28 +18,23 @@ async function applyMoch(streams, apiKey) {
|
|||||||
console.warn('Failed cached torrent availability request: ', error);
|
console.warn('Failed cached torrent availability request: ', error);
|
||||||
return undefined;
|
return undefined;
|
||||||
});
|
});
|
||||||
if (available) {
|
return available && streams
|
||||||
streams.forEach(stream => {
|
.reduce((cachedStreams, stream) => {
|
||||||
const cachedEntry = available[stream.infoHash];
|
const cachedEntry = available[stream.infoHash];
|
||||||
const cachedIds = _getCachedFileIds(stream.fileIdx, cachedEntry).join(',');
|
const cachedIds = _getCachedFileIds(stream.fileIdx, cachedEntry).join(',');
|
||||||
if (cachedIds.length) {
|
if (cachedIds.length) {
|
||||||
stream.name = `[RD+] ${stream.name}`;
|
cachedStreams[stream.infoHash] = `${apiKey}/${stream.infoHash}/${cachedIds}/${stream.fileIdx}`;
|
||||||
stream.url = `${RESOLVER_HOST}/realdebrid/${apiKey}/${stream.infoHash}/${cachedIds}/${stream.fileIdx}`;
|
}
|
||||||
delete stream.infoHash;
|
return cachedStreams;
|
||||||
delete stream.fileIndex;
|
}, {})
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return streams;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolve(apiKey, infoHash, cachedFileIds, fileIndex) {
|
async function resolve({ apiKey, infoHash, cachedEntryInfo, fileIndex }) {
|
||||||
if (!apiKey || !infoHash || !cachedFileIds || !cachedFileIds.length) {
|
if (!apiKey || !infoHash || !cachedEntryInfo) {
|
||||||
return Promise.reject("No valid parameters passed");
|
return Promise.reject("No valid parameters passed");
|
||||||
}
|
}
|
||||||
const id = `${apiKey}_${infoHash}_${fileIndex}`;
|
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) => {
|
return new Promise(((resolve, reject) => {
|
||||||
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
||||||
@@ -112,9 +105,9 @@ async function _unrestrictLink(RD, link) {
|
|||||||
|
|
||||||
async function getDefaultOptions(id) {
|
async function getDefaultOptions(id) {
|
||||||
const userAgent = await cacheUserAgent(id, () => getRandomUserAgent()).catch(() => getRandomUserAgent());
|
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 } };
|
return { proxy: proxy, headers: { 'User-Agent': userAgent } };
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { applyMoch, resolve };
|
module.exports = { getCachedStreams, resolve };
|
||||||
4
addon/package-lock.json
generated
4
addon/package-lock.json
generated
@@ -1604,8 +1604,8 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"parse-torrent-title": {
|
"parse-torrent-title": {
|
||||||
"version": "git://github.com/TheBeastLT/parse-torrent-title.git#6f837b460175695757395a0e1b40f760a40f0f59",
|
"version": "git://github.com/TheBeastLT/parse-torrent-title.git#49be4a2b4ab14e26fca4e52de82f6ad08948fdc7",
|
||||||
"from": "git://github.com/TheBeastLT/parse-torrent-title.git#6f837b460175695757395a0e1b40f760a40f0f59",
|
"from": "git://github.com/TheBeastLT/parse-torrent-title.git#49be4a2b4ab14e26fca4e52de82f6ad08948fdc7",
|
||||||
"requires": {
|
"requires": {
|
||||||
"moment": "^2.24.0"
|
"moment": "^2.24.0"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
"named-queue": "^2.2.1",
|
"named-queue": "^2.2.1",
|
||||||
"needle": "^2.2.4",
|
"needle": "^2.2.4",
|
||||||
"magnet-uri": "^5.1.7",
|
"magnet-uri": "^5.1.7",
|
||||||
"parse-torrent-title": "git://github.com/TheBeastLT/parse-torrent-title.git#6f837b460175695757395a0e1b40f760a40f0f59",
|
"parse-torrent-title": "git://github.com/TheBeastLT/parse-torrent-title.git#49be4a2b4ab14e26fca4e52de82f6ad08948fdc7",
|
||||||
"pg": "^7.8.2",
|
"pg": "^7.8.2",
|
||||||
"pg-hstore": "^2.3.2",
|
"pg-hstore": "^2.3.2",
|
||||||
"real-debrid-api": "git://github.com/TheBeastLT/node-real-debrid.git#935a5c23ae809edbcd2a111526a7f74d6767c50d",
|
"real-debrid-api": "git://github.com/TheBeastLT/node-real-debrid.git#935a5c23ae809edbcd2a111526a7f74d6767c50d",
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ const addonInterface = require('./addon');
|
|||||||
const { manifest } = require('./lib/manifest');
|
const { manifest } = require('./lib/manifest');
|
||||||
const parseConfiguration = require('./lib/configuration');
|
const parseConfiguration = require('./lib/configuration');
|
||||||
const landingTemplate = require('./lib/landingTemplate');
|
const landingTemplate = require('./lib/landingTemplate');
|
||||||
const realDebrid = require('./moch/realdebrid');
|
const moch = require('./moch/moch');
|
||||||
|
|
||||||
const router = getRouter(addonInterface);
|
const router = getRouter(addonInterface);
|
||||||
const limiter = rateLimit({
|
const limiter = rateLimit({
|
||||||
@@ -69,9 +69,16 @@ router.get('/:configuration/:resource/:type/:id.json', (req, res, next) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/realdebrid/:apiKey/:infoHash/:cachedFileIds/:fileIndex?', (req, res) => {
|
router.get('/:moch/:apiKey/:infoHash/:cachedEntryInfo/:fileIndex?', (req, res) => {
|
||||||
const { apiKey, infoHash, cachedFileIds, fileIndex } = req.params;
|
const parameters = {
|
||||||
realDebrid.resolve(apiKey, infoHash, cachedFileIds, isNaN(fileIndex) ? undefined : parseInt(fileIndex))
|
mochKey: req.params.moch,
|
||||||
|
apiKey: req.params.apiKey,
|
||||||
|
infoHash: req.params.infoHash,
|
||||||
|
fileIndex: isNaN(req.params.fileIndex) ? undefined : parseInt(req.params.fileIndex),
|
||||||
|
cachedEntryInfo: req.params.cachedEntryInfo,
|
||||||
|
ip: req.ip
|
||||||
|
}
|
||||||
|
moch.resolve(parameters)
|
||||||
.then(url => {
|
.then(url => {
|
||||||
res.writeHead(302, { Location: url });
|
res.writeHead(302, { Location: url });
|
||||||
res.end();
|
res.end();
|
||||||
|
|||||||
Reference in New Issue
Block a user