From 8ad50a1d155470c547906e142be78836a1ddf553 Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Mon, 21 Dec 2020 16:41:36 +0100 Subject: [PATCH] [addon] improves RD access denied error handling --- addon/moch/realdebrid.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/addon/moch/realdebrid.js b/addon/moch/realdebrid.js index a62b367..218cf3f 100644 --- a/addon/moch/realdebrid.js +++ b/addon/moch/realdebrid.js @@ -121,6 +121,18 @@ async function resolve({ apiKey, infoHash, cachedEntryInfo, fileIndex }) { console.log(`Unrestricting RealDebrid ${infoHash} [${fileIndex}]`); const options = await getDefaultOptions(apiKey); 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 torrent = await _getTorrentInfo(RD, torrentId); 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}]...`); await _selectTorrentFiles(RD, torrent); 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"); } async function _createOrFindTorrentId(RD, infoHash, cachedFileIds, fileIndex) { return _findTorrent(RD, infoHash, fileIndex) - .catch(() => _createTorrentId(RD, infoHash, cachedFileIds)) - .catch(error => { - console.warn('Failed RealDebrid torrent retrieval', error); - return error; - }); + .catch(() => _createTorrentId(RD, infoHash, cachedFileIds)); } async function _retryCreateTorrent(RD, infoHash, cachedFileIds, fileIndex) { @@ -276,6 +281,10 @@ function statusReady(status) { return ['downloaded', 'dead'].includes(status); } +function accessDeniedError(error) { + return [9, 20].includes(error && error.code); +} + async function getDefaultOptions(id) { const userAgent = await cacheUserAgent(id, () => getRandomUserAgent()).catch(() => getRandomUserAgent()); const proxy = await cacheWrapProxy('moch', () => getRandomProxy()).catch(() => getRandomProxy());