mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] extracts common resolve queue logic to root moch class
This commit is contained in:
@@ -9,7 +9,7 @@ const USER_AGENT_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|agent`;
|
|||||||
|
|
||||||
const STREAM_TTL = process.env.STREAM_TTL || 4 * 60 * 60; // 4 hours
|
const STREAM_TTL = process.env.STREAM_TTL || 4 * 60 * 60; // 4 hours
|
||||||
const STREAM_EMPTY_TTL = process.env.STREAM_EMPTY_TTL || 30 * 60; // 30 minutes
|
const STREAM_EMPTY_TTL = process.env.STREAM_EMPTY_TTL || 30 * 60; // 30 minutes
|
||||||
const RESOLVED_URL_TTL = 2 * 60; // 2 minutes
|
const RESOLVED_URL_TTL = 60; // 1 minutes
|
||||||
const PROXY_TTL = 60 * 60; // 60 minutes
|
const PROXY_TTL = 60 * 60; // 60 minutes
|
||||||
const USER_AGENT_TTL = 2 * 24 * 60 * 60; // 2 days
|
const USER_AGENT_TTL = 2 * 24 * 60 * 60; // 2 days
|
||||||
// When the streams are empty we want to cache it for less time in case of timeouts or failures
|
// When the streams are empty we want to cache it for less time in case of timeouts or failures
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
const AllDebridClient = require('all-debrid-api');
|
const AllDebridClient = require('all-debrid-api');
|
||||||
const namedQueue = require('named-queue');
|
|
||||||
const isVideo = require('../lib/video');
|
const isVideo = require('../lib/video');
|
||||||
const StaticResponse = require('./static');
|
const StaticResponse = require('./static');
|
||||||
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 unrestrictQueue = new namedQueue((task, callback) => task.method()
|
|
||||||
.then(result => callback(false, result))
|
|
||||||
.catch((error => callback(error))));
|
|
||||||
|
|
||||||
async function getCachedStreams(streams, apiKey) {
|
async function getCachedStreams(streams, apiKey) {
|
||||||
const options = await getDefaultOptions(apiKey);
|
const options = await getDefaultOptions(apiKey);
|
||||||
const AD = new AllDebridClient(apiKey, options);
|
const AD = new AllDebridClient(apiKey, options);
|
||||||
@@ -34,32 +29,16 @@ async function getCachedStreams(streams, apiKey) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function resolve({ ip, apiKey, infoHash, cachedEntryInfo, fileIndex }) {
|
async function resolve({ ip, 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(ip, apiKey, infoHash, cachedEntryInfo, fileIndex))
|
|
||||||
.catch(error => {
|
|
||||||
console.warn(error);
|
|
||||||
return StaticResponse.FAILED_UNEXPECTED;
|
|
||||||
});
|
|
||||||
|
|
||||||
return new Promise(((resolve, reject) => {
|
|
||||||
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function _unrestrict(ip, apiKey, infoHash, encodedFileName, fileIndex) {
|
|
||||||
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
|
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
|
||||||
const options = await getDefaultOptions(apiKey, ip);
|
const options = await getDefaultOptions(apiKey, ip);
|
||||||
const AD = new AllDebridClient(apiKey, options);
|
const AD = new AllDebridClient(apiKey, options);
|
||||||
const torrent = await _createOrFindTorrent(AD, infoHash);
|
const torrent = await _createOrFindTorrent(AD, infoHash);
|
||||||
if (torrent && statusReady(torrent.statusCode)) {
|
if (torrent && statusReady(torrent.statusCode)) {
|
||||||
return _unrestrictLink(AD, torrent, encodedFileName, fileIndex);
|
return _unrestrictLink(AD, torrent, cachedEntryInfo, fileIndex);
|
||||||
} else if (torrent && statusDownloading(torrent.statusCode)) {
|
} else if (torrent && statusDownloading(torrent.statusCode)) {
|
||||||
return StaticResponse.DOWNLOADING;
|
return StaticResponse.DOWNLOADING;
|
||||||
} else if (torrent && statusHandledError(torrent.statusCode)) {
|
} else if (torrent && statusHandledError(torrent.statusCode)) {
|
||||||
return _retryCreateTorrent(AD, infoHash, encodedFileName, fileIndex);
|
return _retryCreateTorrent(AD, infoHash, cachedEntryInfo, fileIndex);
|
||||||
} else if (torrent && errorExpiredSubscriptionError(torrent)) {
|
} else if (torrent && errorExpiredSubscriptionError(torrent)) {
|
||||||
return StaticResponse.FAILED_ACCESS;
|
return StaticResponse.FAILED_ACCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
const namedQueue = require('named-queue');
|
||||||
const options = require('./options');
|
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');
|
||||||
|
const StaticResponse = require('./static');
|
||||||
|
const { cacheWrapResolvedUrl } = require('../lib/cache');
|
||||||
|
|
||||||
const RESOLVER_HOST = process.env.RESOLVER_HOST || 'http://localhost:7050';
|
const RESOLVER_HOST = process.env.RESOLVER_HOST || 'http://localhost:7050';
|
||||||
const MOCHS = {
|
const MOCHS = {
|
||||||
@@ -22,6 +25,10 @@ const MOCHS = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const unrestrictQueue = new namedQueue((task, callback) => task.method()
|
||||||
|
.then(result => callback(false, result))
|
||||||
|
.catch((error => callback(error))));
|
||||||
|
|
||||||
async function applyMochs(streams, config) {
|
async function applyMochs(streams, config) {
|
||||||
if (!streams || !streams.length) {
|
if (!streams || !streams.length) {
|
||||||
return streams;
|
return streams;
|
||||||
@@ -64,7 +71,20 @@ async function resolve(parameters) {
|
|||||||
if (!moch) {
|
if (!moch) {
|
||||||
return Promise.reject('Not a valid moch provider');
|
return Promise.reject('Not a valid moch provider');
|
||||||
}
|
}
|
||||||
return moch.instance.resolve(parameters);
|
|
||||||
|
if (!parameters.apiKey || !parameters.infoHash || !parameters.cachedEntryInfo) {
|
||||||
|
return Promise.reject("No valid parameters passed");
|
||||||
|
}
|
||||||
|
const id = `${parameters.mochKey}_${parameters.apiKey}_${parameters.infoHash}_${parameters.fileIndex}`;
|
||||||
|
const method = () => cacheWrapResolvedUrl(id, () => moch.instance.resolve(parameters))
|
||||||
|
.catch(error => {
|
||||||
|
console.warn(error);
|
||||||
|
return StaticResponse.FAILED_UNEXPECTED;
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Promise(((resolve, reject) => {
|
||||||
|
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { applyMochs, resolve }
|
module.exports = { applyMochs, resolve }
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
const DebridOptions = {
|
const DebridOptions = {
|
||||||
key: 'debridoptions',
|
key: 'debridoptions',
|
||||||
options: {
|
options: {
|
||||||
cachedlinks: {
|
cachedLinks: {
|
||||||
key: 'cachedlinks',
|
key: 'cachedlinks',
|
||||||
description: 'Show only cached debrid links'
|
description: 'Show only cached debrid links'
|
||||||
},
|
},
|
||||||
cachedlinksifavailable: {
|
cachedLinksIfAvailable: {
|
||||||
key: 'cachedlinksifavailable',
|
key: 'cachedlinksifavailable',
|
||||||
description: 'Show only cached debrid links if available'
|
description: 'Show only cached debrid links if available'
|
||||||
},
|
},
|
||||||
downloadlinks: {
|
downloadLinks: {
|
||||||
key: 'downloadlinks',
|
key: 'downloadlinks',
|
||||||
description: 'Show download to debrid links for uncached'
|
description: 'Show download to debrid links for uncached'
|
||||||
}
|
}
|
||||||
@@ -18,17 +18,17 @@ 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.cachedLinks.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.cachedLinksIfAvailable.key);
|
||||||
}
|
}
|
||||||
|
|
||||||
function includeDownloadLinks(config) {
|
function includeDownloadLinks(config) {
|
||||||
return config[DebridOptions.key] && config[DebridOptions.key]
|
return config[DebridOptions.key] && config[DebridOptions.key]
|
||||||
.includes(DebridOptions.options.downloadlinks.key);
|
.includes(DebridOptions.options.downloadLinks.key);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { DebridOptions, onlyCachedLinks, onlyCachedLinksIfAvailable, includeDownloadLinks }
|
module.exports = { DebridOptions, onlyCachedLinks, onlyCachedLinksIfAvailable, includeDownloadLinks }
|
||||||
@@ -1,15 +1,10 @@
|
|||||||
const PremiumizeClient = require('premiumize-api');
|
const PremiumizeClient = require('premiumize-api');
|
||||||
const namedQueue = require('named-queue');
|
|
||||||
const { encode } = require('magnet-uri');
|
const { encode } = require('magnet-uri');
|
||||||
const isVideo = require('../lib/video');
|
const isVideo = require('../lib/video');
|
||||||
const StaticResponse = require('./static');
|
const StaticResponse = require('./static');
|
||||||
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 unrestrictQueue = new namedQueue((task, callback) => task.method()
|
|
||||||
.then(result => callback(false, result))
|
|
||||||
.catch((error => callback(error))));
|
|
||||||
|
|
||||||
async function getCachedStreams(streams, apiKey) {
|
async function getCachedStreams(streams, apiKey) {
|
||||||
const options = await getDefaultOptions(apiKey);
|
const options = await getDefaultOptions(apiKey);
|
||||||
const PM = new PremiumizeClient(apiKey, options);
|
const PM = new PremiumizeClient(apiKey, options);
|
||||||
@@ -34,38 +29,22 @@ async function getCachedStreams(streams, apiKey) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function resolve({ ip, apiKey, infoHash, cachedEntryInfo, fileIndex }) {
|
async function resolve({ ip, 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(ip, apiKey, infoHash, cachedEntryInfo, fileIndex))
|
|
||||||
.catch(error => {
|
|
||||||
console.warn(error);
|
|
||||||
return StaticResponse.FAILED_UNEXPECTED;
|
|
||||||
});
|
|
||||||
|
|
||||||
return new Promise(((resolve, reject) => {
|
|
||||||
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function _unrestrict(ip, apiKey, infoHash, encodedFileName, fileIndex) {
|
|
||||||
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
|
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
|
||||||
const options = await getDefaultOptions(apiKey, ip);
|
const options = await getDefaultOptions(apiKey, ip);
|
||||||
const PM = new PremiumizeClient(apiKey, options);
|
const PM = new PremiumizeClient(apiKey, options);
|
||||||
|
|
||||||
const cachedLink = await _getCachedLink(PM, infoHash, encodedFileName, fileIndex).catch(() => undefined);
|
const cachedLink = await _getCachedLink(PM, infoHash, cachedEntryInfo, fileIndex).catch(() => undefined);
|
||||||
if (cachedLink) {
|
if (cachedLink) {
|
||||||
return cachedLink;
|
return cachedLink;
|
||||||
}
|
}
|
||||||
|
|
||||||
const torrent = await _createOrFindTorrent(PM, infoHash, encodedFileName, fileIndex);
|
const torrent = await _createOrFindTorrent(PM, infoHash, cachedEntryInfo, fileIndex);
|
||||||
if (torrent && statusReady(torrent.status)) {
|
if (torrent && statusReady(torrent.status)) {
|
||||||
return _getCachedLink(PM, infoHash, encodedFileName, fileIndex);
|
return _getCachedLink(PM, infoHash, cachedEntryInfo, fileIndex);
|
||||||
} else if (torrent && statusDownloading(torrent.status)) {
|
} else if (torrent && statusDownloading(torrent.status)) {
|
||||||
return StaticResponse.DOWNLOADING;
|
return StaticResponse.DOWNLOADING;
|
||||||
} else if (torrent && statusError(torrent.status)) {
|
} else if (torrent && statusError(torrent.status)) {
|
||||||
return _retryCreateTorrent(PM, infoHash, encodedFileName, fileIndex);
|
return _retryCreateTorrent(PM, infoHash, cachedEntryInfo, fileIndex);
|
||||||
}
|
}
|
||||||
return Promise.reject("Failed Premiumize adding torrent");
|
return Promise.reject("Failed Premiumize adding torrent");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,12 @@
|
|||||||
const RealDebridClient = require('real-debrid-api');
|
const RealDebridClient = require('real-debrid-api');
|
||||||
const namedQueue = require('named-queue');
|
|
||||||
const { encode } = require('magnet-uri');
|
const { encode } = require('magnet-uri');
|
||||||
const isVideo = require('../lib/video');
|
const isVideo = require('../lib/video');
|
||||||
const StaticResponse = require('./static');
|
const StaticResponse = require('./static');
|
||||||
const { getRandomProxy, getRandomUserAgent } = require('../lib/request_helper');
|
const { getRandomProxy, getRandomUserAgent } = require('../lib/request_helper');
|
||||||
const { cacheWrapResolvedUrl, cacheWrapProxy, cacheUserAgent } = require('../lib/cache');
|
const { cacheWrapProxy, cacheUserAgent } = require('../lib/cache');
|
||||||
|
|
||||||
const MIN_SIZE = 15728640; // 15 MB
|
const MIN_SIZE = 15728640; // 15 MB
|
||||||
|
|
||||||
const unrestrictQueue = new namedQueue((task, callback) => task.method()
|
|
||||||
.then(result => callback(false, result))
|
|
||||||
.catch((error => callback(error))));
|
|
||||||
|
|
||||||
async function getCachedStreams(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);
|
||||||
@@ -34,22 +29,6 @@ async function getCachedStreams(streams, apiKey) {
|
|||||||
}, {})
|
}, {})
|
||||||
}
|
}
|
||||||
|
|
||||||
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, cachedEntryInfo, fileIndex))
|
|
||||||
.catch(error => {
|
|
||||||
console.warn(error);
|
|
||||||
return StaticResponse.FAILED_UNEXPECTED;
|
|
||||||
});
|
|
||||||
|
|
||||||
return new Promise(((resolve, reject) => {
|
|
||||||
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
function _getCachedFileIds(fileIndex, hosterResults) {
|
function _getCachedFileIds(fileIndex, hosterResults) {
|
||||||
if (!hosterResults || Array.isArray(hosterResults)) {
|
if (!hosterResults || Array.isArray(hosterResults)) {
|
||||||
return [];
|
return [];
|
||||||
@@ -64,18 +43,18 @@ function _getCachedFileIds(fileIndex, hosterResults) {
|
|||||||
return cachedTorrents.length && cachedTorrents[0] || [];
|
return cachedTorrents.length && cachedTorrents[0] || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _unrestrict(apiKey, infoHash, cachedFileIds, fileIndex) {
|
async function resolve({ apiKey, infoHash, cachedEntryInfo, fileIndex }) {
|
||||||
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
|
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
|
||||||
const options = await getDefaultOptions(apiKey);
|
const options = await getDefaultOptions(apiKey);
|
||||||
const RD = new RealDebridClient(apiKey, options);
|
const RD = new RealDebridClient(apiKey, options);
|
||||||
const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedFileIds);
|
const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedEntryInfo);
|
||||||
const torrent = torrentId && await RD.torrents.info(torrentId);
|
const torrent = torrentId && await RD.torrents.info(torrentId);
|
||||||
if (torrent && statusReady(torrent.status)) {
|
if (torrent && statusReady(torrent.status)) {
|
||||||
return _unrestrictLink(RD, torrent, fileIndex);
|
return _unrestrictLink(RD, torrent, fileIndex);
|
||||||
} else if (torrent && statusDownloading(torrent.status)) {
|
} else if (torrent && statusDownloading(torrent.status)) {
|
||||||
return StaticResponse.DOWNLOADING;
|
return StaticResponse.DOWNLOADING;
|
||||||
} else if (torrent && statusError(torrent.status)) {
|
} else if (torrent && statusError(torrent.status)) {
|
||||||
return _retryCreateTorrent(RD, infoHash, cachedFileIds, fileIndex);
|
return _retryCreateTorrent(RD, infoHash, cachedEntryInfo, fileIndex);
|
||||||
} else if (torrent && statusWaitingSelection(torrent.status)) {
|
} else if (torrent && statusWaitingSelection(torrent.status)) {
|
||||||
await _selectTorrentFiles(RD, torrent);
|
await _selectTorrentFiles(RD, torrent);
|
||||||
return StaticResponse.DOWNLOADING;
|
return StaticResponse.DOWNLOADING;
|
||||||
|
|||||||
Reference in New Issue
Block a user