try to free last active item in TB when active limit reached

This commit is contained in:
TheBeastLT 2024-11-29 11:09:54 +02:00
parent dac72da784
commit 01f5ebd90f

View file

@ -120,7 +120,7 @@ async function _findTorrent(apiKey, infoHash) {
return foundTorrent || Promise.reject('No recent torrent found'); return foundTorrent || Promise.reject('No recent torrent found');
} }
async function _createTorrent(apiKey, infoHash) { async function _createTorrent(apiKey, infoHash, attempts = 1) {
const magnetLink = await getMagnetLink(infoHash); const magnetLink = await getMagnetLink(infoHash);
return createTorrent(apiKey, magnetLink) return createTorrent(apiKey, magnetLink)
.then(data => { .then(data => {
@ -130,6 +130,10 @@ async function _createTorrent(apiKey, infoHash) {
if (data.queued_id) { if (data.queued_id) {
return Promise.resolve({ ...data, download_state: 'metaDL' }) return Promise.resolve({ ...data, download_state: 'metaDL' })
} }
if (data?.error === 'ACTIVE_LIMIT' && attempts > 0) {
return freeLastActiveTorrent(apiKey)
.then(() => _createTorrent(apiKey, infoHash, attempts - 1));
}
return Promise.reject(`Unexpected create data: ${JSON.stringify(data)}`); return Promise.reject(`Unexpected create data: ${JSON.stringify(data)}`);
}); });
} }
@ -141,6 +145,18 @@ async function _retryCreateTorrent(apiKey, infoHash, cachedEntryInfo, fileIndex)
: StaticResponse.FAILED_DOWNLOAD; : StaticResponse.FAILED_DOWNLOAD;
} }
async function freeLastActiveTorrent(apiKey) {
const torrents = await getTorrentList(apiKey);
const seedingTorrent = torrents.filter(statusSeeding).pop();
if (seedingTorrent) {
return controlTorrent(apiKey, seedingTorrent.id, 'stop_seeding');
}
if (torrents.filter(statusDownloading).pop()) {
return controlTorrent(apiKey, seedingTorrent.id, 'delete');
}
return Promise.reject({ detail: 'No torrent to pause found' });
}
async function _unrestrictLink(apiKey, infoHash, torrent, cachedEntryInfo, fileIndex, ip) { async function _unrestrictLink(apiKey, infoHash, torrent, cachedEntryInfo, fileIndex, ip) {
const targetFileName = decodeURIComponent(cachedEntryInfo); const targetFileName = decodeURIComponent(cachedEntryInfo);
const videos = torrent.files const videos = torrent.files
@ -263,6 +279,10 @@ function statusReady(torrent) {
return torrent?.download_present; return torrent?.download_present;
} }
function statusSeeding(torrent) {
return ['seeding', 'uploading (no peers)'].includes(torrent?.download_state);
}
function isAccessDeniedError(error) { function isAccessDeniedError(error) {
return ['AUTH_ERROR', 'BAD_TOKEN', 'PLAN_RESTRICTED_FEATURE'].includes(error?.error); return ['AUTH_ERROR', 'BAD_TOKEN', 'PLAN_RESTRICTED_FEATURE'].includes(error?.error);
} }