mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
try to handle common debrid error for catalog retrieval
This commit is contained in:
@@ -14,11 +14,8 @@ export async function getCachedStreams(streams, apiKey) {
|
||||
const hashes = streams.map(stream => stream.infoHash);
|
||||
const available = await AD.magnet.instant(hashes)
|
||||
.catch(error => {
|
||||
if (error && error.code === 'AUTH_BAD_APIKEY') {
|
||||
return Promise.reject(BadTokenError);
|
||||
}
|
||||
if (error && error.code === 'AUTH_USER_BANNED') {
|
||||
return Promise.reject(AccessDeniedError);
|
||||
if (toCommonError(error)) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
console.warn(`Failed AllDebrid cached [${hashes[0]}] torrent availability request:`, error);
|
||||
return undefined;
|
||||
@@ -166,6 +163,16 @@ async function getDefaultOptions(ip) {
|
||||
return { base_agent: AGENT, timeout: 30000 };
|
||||
}
|
||||
|
||||
export function toCommonError(error) {
|
||||
if (error && error.code === 'AUTH_BAD_APIKEY') {
|
||||
return BadTokenError;
|
||||
}
|
||||
if (error && error.code === 'AUTH_USER_BANNED') {
|
||||
return AccessDeniedError;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function statusError(statusCode) {
|
||||
return [5, 6, 7, 8, 9, 10, 11].includes(statusCode);
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ export async function getCachedStreams(streams, apiKey) {
|
||||
.then(results => results.map(result => result.value))
|
||||
.then(results => results.reduce((all, result) => Object.assign(all, result), {}))
|
||||
.catch(error => {
|
||||
if (error === 'badToken') {
|
||||
return Promise.reject(BadTokenError);
|
||||
if (toCommonError(error)) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
console.warn('Failed DebridLink cached torrent availability request:', error);
|
||||
return undefined;
|
||||
@@ -135,6 +135,13 @@ async function getDefaultOptions(ip) {
|
||||
return { ip, timeout: 30000 };
|
||||
}
|
||||
|
||||
export function toCommonError(error) {
|
||||
if (error === 'badToken') {
|
||||
return BadTokenError;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function statusDownloading(torrent) {
|
||||
return torrent.downloadPercent < 100
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import { cacheWrapResolvedUrl } from '../lib/cache.js';
|
||||
import { timeout } from '../lib/promises.js';
|
||||
import { BadTokenError, streamFilename, AccessDeniedError, enrichMeta } from './mochHelper.js';
|
||||
import { isStaticUrl } from './static.js';
|
||||
import { toCommonError } from "./realdebrid.js";
|
||||
|
||||
const RESOLVE_TIMEOUT = 2 * 60 * 1000; // 2 minutes
|
||||
const MIN_API_KEY_SYMBOLS = 15;
|
||||
@@ -81,7 +82,8 @@ export async function applyMochs(streams, config) {
|
||||
}
|
||||
return moch.instance.getCachedStreams(streams, config[moch.key])
|
||||
.then(mochStreams => ({ moch, mochStreams }))
|
||||
.catch(error => {
|
||||
.catch(rawError => {
|
||||
const error = moch.instance.toCommonError(rawError) || rawError;
|
||||
if (error === BadTokenError) {
|
||||
blackListToken(config[moch.key], moch.key);
|
||||
}
|
||||
@@ -120,7 +122,14 @@ export async function getMochCatalog(mochKey, config) {
|
||||
if (isInvalidToken(config[mochKey], mochKey)) {
|
||||
return Promise.reject(new Error(`Invalid API key for moch provider: ${mochKey}`));
|
||||
}
|
||||
return moch.instance.getCatalog(config[moch.key], config.skip, config.ip);
|
||||
return moch.instance.getCatalog(config[moch.key], config.skip, config.ip)
|
||||
.catch(rawError => {
|
||||
const commonError = moch.instance.toCommonError(rawError);
|
||||
if (commonError === BadTokenError) {
|
||||
blackListToken(config[moch.key], moch.key);
|
||||
}
|
||||
return commonError ? [] : Promise.reject(rawError);
|
||||
});
|
||||
}
|
||||
|
||||
export async function getMochItemMeta(mochKey, itemId, config) {
|
||||
|
||||
@@ -15,8 +15,8 @@ export async function getCachedStreams(streams, apiKey) {
|
||||
.then(results => results.map(result => result.cachedItems))
|
||||
.then(results => results.reduce((all, result) => all.concat(result), []))
|
||||
.catch(error => {
|
||||
if (error && error.error === 'NOAUTH') {
|
||||
return Promise.reject(BadTokenError);
|
||||
if (toCommonError(error)) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
console.warn('Failed Offcloud cached torrent availability request:', error);
|
||||
return undefined;
|
||||
@@ -43,6 +43,7 @@ export async function getCatalog(apiKey, offset = 0) {
|
||||
const options = await getDefaultOptions();
|
||||
const OC = new OffcloudClient(apiKey, options);
|
||||
return OC.cloud.history()
|
||||
.then(torrents => torrents)
|
||||
.then(torrents => (torrents || [])
|
||||
.filter(torrent => torrent && statusReady(torrent))
|
||||
.map(torrent => ({
|
||||
@@ -137,6 +138,13 @@ async function getDefaultOptions(ip) {
|
||||
return { ip, timeout: 30000 };
|
||||
}
|
||||
|
||||
export function toCommonError(error) {
|
||||
if (error?.error === 'NOAUTH' || error?.message?.startsWith('Cannot read property')) {
|
||||
return BadTokenError;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function statusDownloading(torrent) {
|
||||
return ['downloading', 'created'].includes(torrent.status);
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ async function _getCachedStreams(PM, apiKey, streams) {
|
||||
const hashes = streams.map(stream => stream.infoHash);
|
||||
return PM.cache.check(hashes)
|
||||
.catch(error => {
|
||||
if (error && error.message === 'Not logged in.') {
|
||||
return Promise.reject(BadTokenError);
|
||||
if (toCommonError(error)) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
console.warn('Failed Premiumize cached torrent availability request:', error);
|
||||
return undefined;
|
||||
@@ -163,6 +163,13 @@ async function _retryCreateTorrent(PM, infoHash, encodedFileName, fileIndex) {
|
||||
: StaticResponse.FAILED_DOWNLOAD;
|
||||
}
|
||||
|
||||
export function toCommonError(error) {
|
||||
if (error && error.message === 'Not logged in.') {
|
||||
return BadTokenError;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function statusError(status) {
|
||||
return ['deleted', 'error', 'timeout'].includes(status);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { isVideo } from '../lib/extension.js';
|
||||
import { delay } from '../lib/promises.js';
|
||||
import StaticResponse from './static.js';
|
||||
import { getMagnetLink } from '../lib/magnetHelper.js';
|
||||
import { BadTokenError } from "./mochHelper.js";
|
||||
|
||||
export async function getCachedStreams(streams, apiKey) {
|
||||
return streams
|
||||
@@ -140,6 +141,10 @@ async function _getPublicToken(Putio, targetVideoId) {
|
||||
return Putio.File.CreatePublicLink(targetVideoId).then(response => response.data.token);
|
||||
}
|
||||
|
||||
export function toCommonError(error) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function statusError(status) {
|
||||
return ['ERROR'].includes(status);
|
||||
}
|
||||
|
||||
@@ -49,11 +49,8 @@ async function _getInstantAvailable(hashes, apiKey, retries = 3, maxChunkSize =
|
||||
.then(results => cacheAvailabilityResults(results))
|
||||
.then(results => Object.assign(cachedResults, results))
|
||||
.catch(error => {
|
||||
if (error && error.code === 8) {
|
||||
return Promise.reject(BadTokenError);
|
||||
}
|
||||
if (error && accessDeniedError(error)) {
|
||||
return Promise.reject(AccessDeniedError);
|
||||
if (toCommonError(error)) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
if (!error && maxChunkSize !== 1) {
|
||||
// sometimes due to large response size RD responds with an empty body. Reduce chunk size to reduce body
|
||||
@@ -355,6 +352,16 @@ async function _unrestrictFileLink(RD, fileLink, torrent, fileIndex, isBrowser)
|
||||
});
|
||||
}
|
||||
|
||||
export function toCommonError(error) {
|
||||
if (error && error.code === 8) {
|
||||
return BadTokenError;
|
||||
}
|
||||
if (error && accessDeniedError(error)) {
|
||||
return AccessDeniedError;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function statusError(status) {
|
||||
return ['error', 'magnet_error'].includes(status);
|
||||
}
|
||||
|
||||
8
addon/package-lock.json
generated
8
addon/package-lock.json
generated
@@ -21,7 +21,7 @@
|
||||
"magnet-uri": "^6.2.0",
|
||||
"name-to-imdb": "^3.0.4",
|
||||
"named-queue": "^2.2.1",
|
||||
"offcloud-api": "^1.0.0",
|
||||
"offcloud-api": "^1.0.1",
|
||||
"parse-torrent-title": "git://github.com/TheBeastLT/parse-torrent-title.git#022408972c2a040f846331a912a6a8487746a654",
|
||||
"pg": "^8.10.0",
|
||||
"premiumize-api": "^1.0.3",
|
||||
@@ -1392,9 +1392,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/offcloud-api": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/offcloud-api/-/offcloud-api-1.0.0.tgz",
|
||||
"integrity": "sha512-cJNoQSl2CJHKrXHRV/+6FMFqToZs/ungdYGNQQOdSuI1FKY7Y5bEOsqlL8BuAxRMd91wKzA4xBmjvrHq5ZllEg==",
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/offcloud-api/-/offcloud-api-1.0.1.tgz",
|
||||
"integrity": "sha512-O49iJRy4ein/VcaINw+3wSVWonEd4BEBCLbgD/fyxIHPf7eW1CSYnyEdrm6+E1k/lMlpTcvx7yk/hvh1iLfoOg==",
|
||||
"dependencies": {
|
||||
"request": "^2.83.0"
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
"magnet-uri": "^6.2.0",
|
||||
"name-to-imdb": "^3.0.4",
|
||||
"named-queue": "^2.2.1",
|
||||
"offcloud-api": "^1.0.0",
|
||||
"offcloud-api": "^1.0.1",
|
||||
"parse-torrent-title": "git://github.com/TheBeastLT/parse-torrent-title.git#022408972c2a040f846331a912a6a8487746a654",
|
||||
"pg": "^8.10.0",
|
||||
"premiumize-api": "^1.0.3",
|
||||
|
||||
Reference in New Issue
Block a user