mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] fix rd retry functionality
This commit is contained in:
@@ -4,7 +4,7 @@ const { isVideo, isArchive } = require('../lib/extension');
|
|||||||
const delay = require('./delay');
|
const delay = require('./delay');
|
||||||
const StaticResponse = require('./static');
|
const StaticResponse = require('./static');
|
||||||
const { getMagnetLink } = require('../lib/magnetHelper');
|
const { getMagnetLink } = require('../lib/magnetHelper');
|
||||||
const { chunkArray } = require('./mochHelper');
|
const { chunkArray, BadTokenError } = require('./mochHelper');
|
||||||
|
|
||||||
const MIN_SIZE = 5 * 1024 * 1024; // 5 MB
|
const MIN_SIZE = 5 * 1024 * 1024; // 5 MB
|
||||||
const CATALOG_MAX_PAGE = 5;
|
const CATALOG_MAX_PAGE = 5;
|
||||||
@@ -120,6 +120,7 @@ async function resolve({ ip, apiKey, infoHash, cachedEntryInfo, fileIndex }) {
|
|||||||
console.log(`Access denied to RealDebrid ${infoHash} [${fileIndex}]`);
|
console.log(`Access denied to RealDebrid ${infoHash} [${fileIndex}]`);
|
||||||
return StaticResponse.FAILED_ACCESS;
|
return StaticResponse.FAILED_ACCESS;
|
||||||
}
|
}
|
||||||
|
console.log('RealDebrid resolve error: ', error);
|
||||||
return Promise.reject(`Failed RealDebrid adding torrent ${JSON.stringify(error)}`);
|
return Promise.reject(`Failed RealDebrid adding torrent ${JSON.stringify(error)}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -191,6 +192,16 @@ async function _createTorrentId(RD, infoHash, cachedFileIds) {
|
|||||||
return addedMagnet.id;
|
return addedMagnet.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function _retryCreateTorrent(RD, infoHash, fileIndex) {
|
||||||
|
console.log(`Retry failed download in RealDebrid ${infoHash} [${fileIndex}]...`);
|
||||||
|
const newTorrentId = await _createTorrentId(RD, infoHash);
|
||||||
|
await _selectTorrentFiles(RD, { id: newTorrentId });
|
||||||
|
const newTorrent = await _getTorrentInfo(RD, newTorrentId);
|
||||||
|
return newTorrent && statusReady(newTorrent.status)
|
||||||
|
? _unrestrictLink(RD, newTorrent, fileIndex)
|
||||||
|
: StaticResponse.FAILED_DOWNLOAD;
|
||||||
|
}
|
||||||
|
|
||||||
async function _selectTorrentFiles(RD, torrent) {
|
async function _selectTorrentFiles(RD, torrent) {
|
||||||
torrent = statusWaitingSelection(torrent.status) ? torrent : await _openTorrent(RD, torrent.id);
|
torrent = statusWaitingSelection(torrent.status) ? torrent : await _openTorrent(RD, torrent.id);
|
||||||
if (torrent && torrent.files && statusWaitingSelection(torrent.status)) {
|
if (torrent && torrent.files && statusWaitingSelection(torrent.status)) {
|
||||||
@@ -215,7 +226,7 @@ async function _unrestrictLink(RD, torrent, fileIndex) {
|
|||||||
const targetFile = torrent.files.find(file => file.id === fileIndex + 1)
|
const targetFile = torrent.files.find(file => file.id === fileIndex + 1)
|
||||||
|| torrent.files.filter(file => file.selected).sort((a, b) => b.bytes - a.bytes)[0];
|
|| torrent.files.filter(file => file.selected).sort((a, b) => b.bytes - a.bytes)[0];
|
||||||
if (!targetFile.selected) {
|
if (!targetFile.selected) {
|
||||||
await _retryCreateTorrent(RD, torrent.hash.toLowerCase(), undefined, fileIndex);
|
await _retryCreateTorrent(RD, torrent.hash.toLowerCase(), fileIndex);
|
||||||
return StaticResponse.DOWNLOADING;
|
return StaticResponse.DOWNLOADING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,14 +249,12 @@ async function _unrestrictFileLink(RD, fileLink, torrent, fileIndex) {
|
|||||||
if (isArchive(unrestrictedLink)) {
|
if (isArchive(unrestrictedLink)) {
|
||||||
return StaticResponse.FAILED_RAR;
|
return StaticResponse.FAILED_RAR;
|
||||||
}
|
}
|
||||||
// const transcodedLink = await RD.streaming.transcode(unrestrictedLink.id);
|
|
||||||
console.log(`Unrestricted RealDebrid ${torrent.hash} [${fileIndex}] to ${unrestrictedLink}`);
|
console.log(`Unrestricted RealDebrid ${torrent.hash} [${fileIndex}] to ${unrestrictedLink}`);
|
||||||
return unrestrictedLink;
|
return unrestrictedLink;
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
if (error.code === 19) {
|
if (error.code === 19) {
|
||||||
return _retryCreateTorrent(RD, torrent.hash.toLowerCase(), undefined, fileIndex)
|
return _retryCreateTorrent(RD, torrent.hash.toLowerCase(), fileIndex);
|
||||||
.then(() => StaticResponse.FAILED_DOWNLOAD);
|
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user