Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-season packs: Added protection tag for unmonitored downloads removal #12

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/config.conf-Explained
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ REMOVE_ORPHANS = False
# Steers whether downloads belonging to unmonitored movies/TV shows are removed from the queue
# Note: Will only remove from queue if all tv shows depending on the same download are unmonitored
# Unmonitored downloads are not added to the block list
# Note: Since Sonarr does not support multi-season packs, if you download one you should protect it with the below NO_STALLED_REMOVAL_QBIT_TAG.
# Type: Boolean
# Permissible Values: True, False
# Is Mandatory: No (Defaults to False)
Expand All @@ -87,6 +88,7 @@ PERMITTED_ATTEMPTS= 3
###### NO_STALLED_REMOVAL_QBIT_TAG ######
# Downloads in qBittorrent tagged with this tag will not be killed even if they are stalled
# Tag is automatically created in qBittorrent (required qBittorrent is reachable on QBITTORRENT_URL )
# Also protects unmonitored downloads from being removed (relevant for multi-season packs)
# Type: String
# Is Mandatory: No (Defaults to "Don't Kill If Stalled")
NO_STALLED_REMOVAL_QBIT_TAG= Don't Kill If Stalled
Expand Down
33 changes: 22 additions & 11 deletions src/queue_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ async def remove_stalled(settings_dict, BASE_URL, API_KEY, deleted_downloads, de
else:
protected_downloadIDs = []
stalledItems = []
already_detected = []
for queueItem in queue['records']:
if 'errorMessage' in queueItem and 'status' in queueItem:
if queueItem['status'] == 'warning' and \
queueItem['errorMessage'] == 'The download is stalled with no connections':
if queueItem['downloadId'] in protected_downloadIDs:
logger.verbose('>>> Detected stalled download, tagged not to be killed: %s',queueItem['title'])
if queueItem['downloadId'] not in already_detected:
already_detected.append(queueItem['downloadId'])
logger.verbose('>>> Detected stalled download, tagged not to be killed: %s',queueItem['title'])
else:
stalledItems.append(queueItem)
await check_permitted_attempts(settings_dict, stalledItems, 'stalled', True, deleted_downloads, BASE_URL, API_KEY, defective_tracker)
Expand All @@ -61,11 +64,6 @@ async def test_remove_ALL(settings_dict, BASE_URL, API_KEY, deleted_downloads, d
queue = await get_queue(BASE_URL, API_KEY)
if not queue: return 0
logger.debug('test_remove_ALL/queue: %s', str(queue))
if settings_dict['QBITTORRENT_URL']:
protected_dowloadItems = await rest_get(settings_dict['QBITTORRENT_URL']+'/torrents/info',params={'tag': settings_dict['NO_STALLED_REMOVAL_QBIT_TAG']}, cookies=settings_dict['QBIT_COOKIE'] )
protected_downloadIDs = [str.upper(item['hash']) for item in protected_dowloadItems]
else:
protected_downloadIDs = []
stalledItems = []
for queueItem in queue['records']:
stalledItems.append(queueItem)
Expand Down Expand Up @@ -113,19 +111,32 @@ async def remove_unmonitored(settings_dict, BASE_URL, API_KEY, deleted_downloads
queue = await get_queue(BASE_URL, API_KEY)
if not queue: return 0
logger.debug('remove_unmonitored/queue IN: %s', str(queue))
unmonitoredItems= []
downloadItems = []
if settings_dict['QBITTORRENT_URL']:
protected_dowloadItems = await rest_get(settings_dict['QBITTORRENT_URL']+'/torrents/info',params={'tag': settings_dict['NO_STALLED_REMOVAL_QBIT_TAG']}, cookies=settings_dict['QBIT_COOKIE'] )
protected_downloadIDs = [str.upper(item['hash']) for item in protected_dowloadItems]
else:
protected_downloadIDs = []
for queueItem in queue['records']:
if arr_type == 'sonarr':
monitored = (await rest_get(f'{BASE_URL}/episode/{str(queueItem["episodeId"])}', API_KEY))['monitored']
elif arr_type == 'radarr':
monitored = (await rest_get(f'{BASE_URL}/movie/{str(queueItem["movieId"])}', API_KEY))['monitored']
elif arr_type == 'lidarr':
monitored = (await rest_get(f'{BASE_URL}/album/{str(queueItem["albumId"])}', API_KEY))['monitored']
downloadItems.append({'downloadId': queueItem['downloadId'], 'id': queueItem['id'], 'monitored': monitored})
monitored_downloadIds = [downloadItem['downloadId'] for downloadItem in downloadItems if downloadItem['monitored']]
unmonitoredItems = [downloadItem for downloadItem in downloadItems if downloadItem['downloadId'] not in monitored_downloadIds]
for unmonitoredItem in unmonitoredItems:
downloadItems.append({'downloadId': queueItem['downloadId'], 'id': queueItem['id'], 'monitored': monitored, 'title': queueItem['title']})
unmonitoredItems= []
already_detected = []
for downloadItem in downloadItems:
if not downloadItem['monitored']:
if downloadItem['downloadId'] in protected_downloadIDs:
if downloadItem['downloadId'] not in already_detected:
already_detected.append(queueItem['downloadId'])
logger.verbose('>>> Detected unmonitored download, tagged not to be killed: %s',downloadItem['title'])
else:
unmonitoredItems.append(downloadItem)

for queueItem in unmonitoredItems:
await remove_download(settings_dict, BASE_URL, API_KEY, queueItem['id'], queueItem['title'], queueItem['downloadId'], 'unmonitored', False, deleted_downloads)
logger.debug('remove_unmonitored/queue OUT: %s', str(await get_queue(BASE_URL, API_KEY) ))
return len(unmonitoredItems)
Expand Down
Loading