mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] refreshes and blacklists failed proxies
This commit is contained in:
@@ -68,5 +68,9 @@ function cacheUserAgent(id, method) {
|
|||||||
return cacheWrap(remoteCache, `${USER_AGENT_KEY_PREFIX}:${id}`, method, { ttl: USER_AGENT_TTL });
|
return cacheWrap(remoteCache, `${USER_AGENT_KEY_PREFIX}:${id}`, method, { ttl: USER_AGENT_TTL });
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { cacheWrapStream, cacheWrapResolvedUrl, cacheWrapProxy, cacheUserAgent };
|
function uncacheProxy(id) {
|
||||||
|
return remoteCache.del(`${PROXY_KEY_PREFIX}:${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { cacheWrapStream, cacheWrapResolvedUrl, cacheWrapProxy, cacheUserAgent, uncacheProxy };
|
||||||
|
|
||||||
|
|||||||
@@ -17,4 +17,12 @@ function getRandomProxy() {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { getRandomUserAgent, getRandomProxy };
|
function blacklistProxy(proxy) {
|
||||||
|
const proxyHost = proxy.replace(/.*@/, '');
|
||||||
|
console.warn(`Blacklisting ${proxyHost}`);
|
||||||
|
if (PROXY_HOSTS && PROXY_HOSTS.indexOf(proxyHost) > -1) {
|
||||||
|
PROXY_HOSTS.splice(PROXY_HOSTS.indexOf(proxyHost), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { getRandomUserAgent, getRandomProxy, blacklistProxy };
|
||||||
@@ -3,20 +3,14 @@ const { encode } = require('magnet-uri');
|
|||||||
const { isVideo, isArchive } = require('../lib/extension');
|
const { isVideo, isArchive } = require('../lib/extension');
|
||||||
const delay = require('./delay');
|
const delay = require('./delay');
|
||||||
const StaticResponse = require('./static');
|
const StaticResponse = require('./static');
|
||||||
const { getRandomProxy, getRandomUserAgent } = require('../lib/request_helper');
|
const { getRandomProxy, getRandomUserAgent, blacklistProxy } = require('../lib/request_helper');
|
||||||
const { cacheWrapProxy, cacheUserAgent } = require('../lib/cache');
|
const { cacheWrapProxy, cacheUserAgent, uncacheProxy } = require('../lib/cache');
|
||||||
|
|
||||||
const MIN_SIZE = 15728640; // 15 MB
|
const MIN_SIZE = 15728640; // 15 MB
|
||||||
|
|
||||||
async function getCachedStreams(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);
|
const hashes = streams.map(stream => stream.infoHash);
|
||||||
const available = await RD.torrents.instantAvailability(hashes)
|
const available = await _getInstantAvailable(hashes, apiKey);
|
||||||
.catch(error => {
|
|
||||||
console.warn('Failed RealDebrid cached torrent availability request: ', error);
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
return available && streams
|
return available && streams
|
||||||
.reduce((mochStreams, stream) => {
|
.reduce((mochStreams, stream) => {
|
||||||
const cachedEntry = available[stream.infoHash];
|
const cachedEntry = available[stream.infoHash];
|
||||||
@@ -30,6 +24,20 @@ async function getCachedStreams(streams, apiKey) {
|
|||||||
}, {})
|
}, {})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function _getInstantAvailable(hashes, apiKey, retries = 3) {
|
||||||
|
const options = await getDefaultOptions(apiKey);
|
||||||
|
const RD = new RealDebridClient(apiKey, options);
|
||||||
|
return RD.torrents.instantAvailability(hashes)
|
||||||
|
.catch(error => {
|
||||||
|
if (retries > 0 && ['ENOTFOUND', 'ETIMEDOUT'].some(v => error.message && error.message.includes(v))) {
|
||||||
|
blacklistProxy(options.proxy);
|
||||||
|
return uncacheProxy('moch').then(() => _getInstantAvailable(hashes, apiKey, retries - 1));
|
||||||
|
}
|
||||||
|
console.warn('Failed RealDebrid cached torrent availability request: ', error);
|
||||||
|
return undefined;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function _getCachedFileIds(fileIndex, hosterResults) {
|
function _getCachedFileIds(fileIndex, hosterResults) {
|
||||||
if (!hosterResults || Array.isArray(hosterResults)) {
|
if (!hosterResults || Array.isArray(hosterResults)) {
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
Reference in New Issue
Block a user