[addon] fixes rd access denied condition and adds better logging

This commit is contained in:
TheBeastLT
2020-05-14 23:47:14 +02:00
parent f0886d966a
commit 347d97fb55
2 changed files with 23 additions and 10 deletions

View File

@@ -30,8 +30,10 @@ async function resolve({ ip, apiKey, infoHash, cachedEntryInfo, fileIndex }) {
if (torrent && statusReady(torrent.status)) { if (torrent && statusReady(torrent.status)) {
return _unrestrictLink(Putio, torrent, cachedEntryInfo, fileIndex); return _unrestrictLink(Putio, torrent, cachedEntryInfo, fileIndex);
} else if (torrent && statusDownloading(torrent.status)) { } else if (torrent && statusDownloading(torrent.status)) {
console.log(`Downloading to Putio ${infoHash} [${fileIndex}]...`);
return StaticResponse.DOWNLOADING; return StaticResponse.DOWNLOADING;
} else if (torrent && statusError(torrent.status)) { } else if (torrent && statusError(torrent.status)) {
console.log(`Retrying downloading to Putio ${infoHash} [${fileIndex}]...`);
return _retryCreateTorrent(Putio, infoHash, cachedEntryInfo, fileIndex); return _retryCreateTorrent(Putio, infoHash, cachedEntryInfo, fileIndex);
} }
return Promise.reject("Failed Putio adding torrent"); return Promise.reject("Failed Putio adding torrent");
@@ -58,7 +60,7 @@ async function _findTorrent(Putio, infoHash) {
const foundTorrents = torrents.filter(torrent => torrent.source.toLowerCase().includes(infoHash)); const foundTorrents = torrents.filter(torrent => torrent.source.toLowerCase().includes(infoHash));
const nonFailedTorrent = foundTorrents.find(torrent => !statusError(torrent.status)); const nonFailedTorrent = foundTorrents.find(torrent => !statusError(torrent.status));
const foundTorrent = nonFailedTorrent || foundTorrents[0]; const foundTorrent = nonFailedTorrent || foundTorrents[0];
return foundTorrent || Promise.reject('No recent torrent found'); return foundTorrent || Promise.reject('No recent torrent found in Putio');
} }
async function _createTorrent(Putio, infoHash) { async function _createTorrent(Putio, infoHash) {
@@ -82,7 +84,7 @@ async function _unrestrictLink(Putio, torrent, encodedFileName, fileIndex) {
const publicFile = await Putio.File.Public(publicToken).then(response => response.data.parent); const publicFile = await Putio.File.Public(publicToken).then(response => response.data.parent);
if (!publicFile.stream_url || !publicFile.stream_url.length) { if (!publicFile.stream_url || !publicFile.stream_url.length) {
return Promise.reject(`No available links found for [${torrent.hash}] ${encodedFileName}`); return Promise.reject(`No Putio links found for [${torrent.hash}] ${encodedFileName}`);
} }
console.log(`Unrestricted Putio ${torrent.hash} [${fileIndex}] to ${publicFile.stream_url}`); console.log(`Unrestricted Putio ${torrent.hash} [${fileIndex}] to ${publicFile.stream_url}`);
return publicFile.stream_url; return publicFile.stream_url;

View File

@@ -45,21 +45,25 @@ function _getCachedFileIds(fileIndex, hosterResults) {
} }
async function resolve({ apiKey, infoHash, cachedEntryInfo, fileIndex }) { async function resolve({ apiKey, infoHash, cachedEntryInfo, fileIndex }) {
console.log(`Unrestricting ${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);
const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedEntryInfo); const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedEntryInfo);
const torrent = torrentId && await RD.torrents.info(torrentId); const torrent = await _getTorrentInfo(RD, torrentId);
if (torrent && statusReady(torrent.status)) { if (torrent && statusReady(torrent.status)) {
return _unrestrictLink(RD, torrent, fileIndex); return _unrestrictLink(RD, torrent, fileIndex);
} else if (torrent && statusDownloading(torrent.status)) { } else if (torrent && statusDownloading(torrent.status)) {
console.log(`Downloading to RealDebrid ${infoHash} [${fileIndex}]...`);
return StaticResponse.DOWNLOADING; return StaticResponse.DOWNLOADING;
} else if (torrent && statusError(torrent.status)) { } else if (torrent && statusError(torrent.status)) {
console.log(`Retrying downloading to RealDebrid ${infoHash} [${fileIndex}]...`);
return _retryCreateTorrent(RD, infoHash, cachedEntryInfo, fileIndex); return _retryCreateTorrent(RD, infoHash, cachedEntryInfo, fileIndex);
} else if (torrent && statusWaitingSelection(torrent.status)) { } else if (torrent && statusWaitingSelection(torrent.status)) {
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) { } else if (torrent && torrent.code === 9) {
console.log(`Access denied to RealDebrid ${infoHash} [${fileIndex}]`);
return StaticResponse.FAILED_ACCESS; return StaticResponse.FAILED_ACCESS;
} }
return Promise.reject("Failed RealDebrid adding torrent"); return Promise.reject("Failed RealDebrid adding torrent");
@@ -76,20 +80,27 @@ async function _createOrFindTorrentId(RD, infoHash, cachedFileIds) {
async function _retryCreateTorrent(RD, infoHash, cachedFileIds, fileIndex) { async function _retryCreateTorrent(RD, infoHash, cachedFileIds, fileIndex) {
const newTorrentId = await _createTorrentId(RD, infoHash, cachedFileIds); const newTorrentId = await _createTorrentId(RD, infoHash, cachedFileIds);
const newTorrent = await RD.torrents.info(newTorrentId); const newTorrent = await _getTorrentInfo(RD, newTorrentId);
return newTorrent && statusReady(newTorrent.status) return newTorrent && statusReady(newTorrent.status)
? _unrestrictLink(RD, newTorrent, fileIndex) ? _unrestrictLink(RD, newTorrent, fileIndex)
: StaticResponse.FAILED_DOWNLOAD; : 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 => !statusError(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');
} }
async function _getTorrentInfo(RD, torrentId) {
if (!torrentId || typeof torrentId === 'object') {
return torrentId || Promise.reject('No RealDebrid torrentId provided')
}
return RD.torrents.info(torrentId);
}
async function _createTorrentId(RD, infoHash, cachedFileIds) { async function _createTorrentId(RD, infoHash, cachedFileIds) {
const addedMagnet = await RD.torrents.addMagnet(encode({ infoHash })); const addedMagnet = await RD.torrents.addMagnet(encode({ infoHash }));
await _selectTorrentFiles(RD, { id: addedMagnet.id }, cachedFileIds); await _selectTorrentFiles(RD, { id: addedMagnet.id }, cachedFileIds);
@@ -101,10 +112,10 @@ async function _selectTorrentFiles(RD, torrent, cachedFileIds) {
return RD.torrents.selectFiles(torrent.id, cachedFileIds); return RD.torrents.selectFiles(torrent.id, cachedFileIds);
} }
torrent = torrent.status ? torrent : await RD.torrents.info(torrent.id); torrent = torrent.status ? torrent : await _getTorrentInfo(RD, torrent.id);
if (torrent && statusOpening(torrent.status)) { 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
torrent = await delay(2000).then(() => RD.torrents.info(torrent.id)); torrent = await delay(2000).then(() => _getTorrentInfo(RD, torrent.id));
} }
if (torrent && torrent.files && statusWaitingSelection(torrent.status)) { if (torrent && torrent.files && statusWaitingSelection(torrent.status)) {
const videoFileIds = torrent.files const videoFileIds = torrent.files
@@ -131,7 +142,7 @@ async function _unrestrictLink(RD, torrent, fileIndex) {
: torrent.links[selectedFiles.indexOf(targetFile)]; : torrent.links[selectedFiles.indexOf(targetFile)];
if (!fileLink || !fileLink.length) { if (!fileLink || !fileLink.length) {
return Promise.reject("No available links found"); return Promise.reject(`No RealDebrid links found for ${torrent.hash} [${fileIndex}]`);
} }
const unrestrictedLink = await RD.unrestrict.link(fileLink).then(response => response.download); const unrestrictedLink = await RD.unrestrict.link(fileLink).then(response => response.download);
@@ -139,7 +150,7 @@ async function _unrestrictLink(RD, torrent, fileIndex) {
return StaticResponse.FAILED_RAR; return StaticResponse.FAILED_RAR;
} }
// const transcodedLink = await RD.streaming.transcode(unrestrictedLink.id); // const transcodedLink = await RD.streaming.transcode(unrestrictedLink.id);
console.log(`Unrestricted ${torrent.hash} [${fileIndex}] to ${unrestrictedLink}`); console.log(`Unrestricted RealDebrid ${torrent.hash} [${fileIndex}] to ${unrestrictedLink}`);
return unrestrictedLink; return unrestrictedLink;
} }