mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] improves RD access denied error handling
This commit is contained in:
@@ -121,6 +121,18 @@ async function resolve({ apiKey, infoHash, cachedEntryInfo, fileIndex }) {
|
|||||||
console.log(`Unrestricting RealDebrid ${infoHash} [${fileIndex}]`);
|
console.log(`Unrestricting RealDebrid ${infoHash} [${fileIndex}]`);
|
||||||
const options = await getDefaultOptions(apiKey);
|
const options = await getDefaultOptions(apiKey);
|
||||||
const RD = new RealDebridClient(apiKey, options);
|
const RD = new RealDebridClient(apiKey, options);
|
||||||
|
|
||||||
|
return _resolve(RD, infoHash, cachedEntryInfo, fileIndex)
|
||||||
|
.catch(error => {
|
||||||
|
if (accessDeniedError(error)) {
|
||||||
|
console.log(`Access denied to RealDebrid ${infoHash} [${fileIndex}]`);
|
||||||
|
return StaticResponse.FAILED_ACCESS;
|
||||||
|
}
|
||||||
|
return Promise.reject(`Failed RealDebrid adding torrent ${JSON.stringify(error)}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function _resolve(RD, infoHash, cachedEntryInfo, fileIndex) {
|
||||||
const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedEntryInfo, fileIndex);
|
const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedEntryInfo, fileIndex);
|
||||||
const torrent = await _getTorrentInfo(RD, torrentId);
|
const torrent = await _getTorrentInfo(RD, torrentId);
|
||||||
if (torrent && statusReady(torrent.status)) {
|
if (torrent && statusReady(torrent.status)) {
|
||||||
@@ -135,20 +147,13 @@ async function resolve({ apiKey, infoHash, cachedEntryInfo, fileIndex }) {
|
|||||||
console.log(`Trying to select files on RealDebrid ${infoHash} [${fileIndex}]...`);
|
console.log(`Trying to select files on RealDebrid ${infoHash} [${fileIndex}]...`);
|
||||||
await _selectTorrentFiles(RD, torrent);
|
await _selectTorrentFiles(RD, torrent);
|
||||||
return StaticResponse.DOWNLOADING;
|
return StaticResponse.DOWNLOADING;
|
||||||
} else if (torrent && torrent.code === 9) {
|
|
||||||
console.log(`Access denied to RealDebrid ${infoHash} [${fileIndex}]`);
|
|
||||||
return StaticResponse.FAILED_ACCESS;
|
|
||||||
}
|
}
|
||||||
return Promise.reject("Failed RealDebrid adding torrent");
|
return Promise.reject("Failed RealDebrid adding torrent");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _createOrFindTorrentId(RD, infoHash, cachedFileIds, fileIndex) {
|
async function _createOrFindTorrentId(RD, infoHash, cachedFileIds, fileIndex) {
|
||||||
return _findTorrent(RD, infoHash, fileIndex)
|
return _findTorrent(RD, infoHash, fileIndex)
|
||||||
.catch(() => _createTorrentId(RD, infoHash, cachedFileIds))
|
.catch(() => _createTorrentId(RD, infoHash, cachedFileIds));
|
||||||
.catch(error => {
|
|
||||||
console.warn('Failed RealDebrid torrent retrieval', error);
|
|
||||||
return error;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _retryCreateTorrent(RD, infoHash, cachedFileIds, fileIndex) {
|
async function _retryCreateTorrent(RD, infoHash, cachedFileIds, fileIndex) {
|
||||||
@@ -276,6 +281,10 @@ function statusReady(status) {
|
|||||||
return ['downloaded', 'dead'].includes(status);
|
return ['downloaded', 'dead'].includes(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function accessDeniedError(error) {
|
||||||
|
return [9, 20].includes(error && error.code);
|
||||||
|
}
|
||||||
|
|
||||||
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('moch', () => getRandomProxy()).catch(() => getRandomProxy());
|
const proxy = await cacheWrapProxy('moch', () => getRandomProxy()).catch(() => getRandomProxy());
|
||||||
|
|||||||
Reference in New Issue
Block a user