mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] extracts rd status codes to methods
This commit is contained in:
@@ -70,17 +70,13 @@ async function _unrestrict(apiKey, infoHash, cachedFileIds, fileIndex) {
|
|||||||
const RD = new RealDebridClient(apiKey, options);
|
const RD = new RealDebridClient(apiKey, options);
|
||||||
const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedFileIds);
|
const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedFileIds);
|
||||||
const torrent = torrentId && await RD.torrents.info(torrentId);
|
const torrent = torrentId && await RD.torrents.info(torrentId);
|
||||||
if (torrent && torrent.status === 'downloaded') {
|
if (torrent && statusReady(torrent.status)) {
|
||||||
return _unrestrictLink(RD, torrent, fileIndex);
|
return _unrestrictLink(RD, torrent, fileIndex);
|
||||||
} else if (torrent && ['downloading', 'queued'].includes(torrent.status)) {
|
} else if (torrent && statusDownloading(torrent.status)) {
|
||||||
return StaticResponse.DOWNLOADING;
|
return StaticResponse.DOWNLOADING;
|
||||||
} else if (torrent && ['error', 'dead', 'magnet_error'].includes(torrent.status)) {
|
} else if (torrent && statusError(torrent.status)) {
|
||||||
const newTorrentId = await _createTorrentId(RD, infoHash, cachedFileIds);
|
return _retryCreateTorrent(RD, infoHash, cachedFileIds, fileIndex);
|
||||||
const newTorrent = await RD.torrents.info(newTorrentId);
|
} else if (torrent && statusWaitingSelection(torrent.status)) {
|
||||||
return newTorrent && newTorrent.status === 'downloaded'
|
|
||||||
? _unrestrictLink(RD, newTorrent, fileIndex)
|
|
||||||
: StaticResponse.FAILED_DOWNLOAD;
|
|
||||||
} else if (torrent && torrent.status === 'waiting_files_selection') {
|
|
||||||
await _selectTorrentFiles(RD, torrent);
|
await _selectTorrentFiles(RD, torrent);
|
||||||
return StaticResponse.DOWNLOADING;
|
return StaticResponse.DOWNLOADING;
|
||||||
} else if (torrent && torrent.code === 9) {
|
} else if (torrent && torrent.code === 9) {
|
||||||
@@ -98,10 +94,18 @@ async function _createOrFindTorrentId(RD, infoHash, cachedFileIds) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function _retryCreateTorrent(RD, infoHash, cachedFileIds, fileIndex) {
|
||||||
|
const newTorrentId = await _createTorrentId(RD, infoHash, cachedFileIds);
|
||||||
|
const newTorrent = await RD.torrents.info(newTorrentId);
|
||||||
|
return newTorrent && statusReady(newTorrent.status)
|
||||||
|
? _unrestrictLink(RD, newTorrent, fileIndex)
|
||||||
|
: StaticResponse.FAILED_DOWNLOAD;
|
||||||
|
}
|
||||||
|
|
||||||
async function _findTorrent(RD, infoHash) {
|
async function _findTorrent(RD, infoHash) {
|
||||||
const torrents = await RD.torrents.get(0, 1);
|
const torrents = await RD.torrents.get(0, 1);
|
||||||
const foundTorrents = torrents.filter(torrent => torrent.hash.toLowerCase() === infoHash);
|
const foundTorrents = torrents.filter(torrent => torrent.hash.toLowerCase() === infoHash);
|
||||||
const nonFailedTorrent = foundTorrents.find(torrent => ['error', 'dead', 'magnet_error'].includes(torrent.status));
|
const nonFailedTorrent = foundTorrents.find(torrent => !statusError(torrent.status));
|
||||||
const foundTorrent = nonFailedTorrent || foundTorrents[0];
|
const foundTorrent = nonFailedTorrent || foundTorrents[0];
|
||||||
return foundTorrent && foundTorrent.id || Promise.reject('No recent torrent found');
|
return foundTorrent && foundTorrent.id || Promise.reject('No recent torrent found');
|
||||||
}
|
}
|
||||||
@@ -118,12 +122,12 @@ async function _selectTorrentFiles(RD, torrent, cachedFileIds) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
torrent = torrent.status ? torrent : await RD.torrents.info(torrent.id);
|
torrent = torrent.status ? torrent : await RD.torrents.info(torrent.id);
|
||||||
if (torrent && torrent.status === 'magnet_conversion') {
|
if (torrent && statusOpening(torrent.status)) {
|
||||||
// sleep for 2 seconds, maybe the torrent will be converted
|
// sleep for 2 seconds, maybe the torrent will be converted
|
||||||
await new Promise((resolve) => setTimeout(resolve, 2000));
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
||||||
torrent = await RD.torrents.info(torrent.id);
|
torrent = await RD.torrents.info(torrent.id);
|
||||||
}
|
}
|
||||||
if (torrent && torrent.files && torrent.status === 'waiting_files_selection') {
|
if (torrent && torrent.files && statusWaitingSelection(torrent.status)) {
|
||||||
const videoFileIds = torrent.files
|
const videoFileIds = torrent.files
|
||||||
.filter(file => isVideo(file.path))
|
.filter(file => isVideo(file.path))
|
||||||
.filter(file => file.bytes > MIN_SIZE)
|
.filter(file => file.bytes > MIN_SIZE)
|
||||||
@@ -160,6 +164,26 @@ async function _unrestrictLink(RD, torrent, fileIndex) {
|
|||||||
return unrestrictedLink;
|
return unrestrictedLink;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function statusError(status) {
|
||||||
|
return ['error', 'dead', 'magnet_error'].includes(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
function statusOpening(status) {
|
||||||
|
return status === 'magnet_conversion';
|
||||||
|
}
|
||||||
|
|
||||||
|
function statusWaitingSelection(status) {
|
||||||
|
return status === 'waiting_files_selection';
|
||||||
|
}
|
||||||
|
|
||||||
|
function statusDownloading(status) {
|
||||||
|
return ['downloading', 'queued'].includes(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
function statusReady(status) {
|
||||||
|
return status === 'downloaded';
|
||||||
|
}
|
||||||
|
|
||||||
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