From c748b30934ffc7ef594a22bb24ce765342bd4515 Mon Sep 17 00:00:00 2001 From: Jon Date: Tue, 28 Sep 2021 22:26:42 -0400 Subject: [PATCH 1/5] ability to exclude tags when taggin noHL --- config.yml.sample | 10 +++++- qbit_manage.py | 90 +++++++++++++++++++++++++++-------------------- 2 files changed, 60 insertions(+), 40 deletions(-) diff --git a/config.yml.sample b/config.yml.sample index 90b8d2a3..da36277c 100644 --- a/config.yml.sample +++ b/config.yml.sample @@ -40,7 +40,11 @@ tags: nohardlinks: # Mandatory to fill out directory parameter above to use this function (root_dir/remote_dir) # This variable should be set to your category name of your completed movies/completed series in qbit. Acceptable variable can be any category you would like to tag if there are no hardlinks found - movies-completed: + movies-completed: + # exclude_tags var: Will exclude the following tags when searching through the category. + exclude_tags: + - Beyond-HD + - AnimeBytes # cleanup var: WARNING!! Setting this as true Will remove and delete contents of any torrents that are in paused state and has the NoHL tag cleanup: false # max_ratio var: Will set the torrent Maximum share ratio until torrent is stopped from seeding/uploading @@ -50,6 +54,10 @@ nohardlinks: #Can have additional categories set with separate ratio/seeding times defined. series-completed: + # exclude_tags var: Will exclude the following tags when searching through the category. + exclude_tags: + - Beyond-HD + - BroadcasTheNet # cleanup var: WARNING!! Setting this as true Will remove and delete contents of any torrents that are in paused state and has the NoHL tag cleanup: false # max_ratio var: Will set the torrent Maximum share ratio until torrent is stopped from seeding/uploading diff --git a/qbit_manage.py b/qbit_manage.py index 05c6fdbf..fc190bf9 100644 --- a/qbit_manage.py +++ b/qbit_manage.py @@ -491,54 +491,66 @@ def tag_nohardlinks(): t_del_cs = 0 #counter for the number of torrents that has no hard links and meets the criteria for ratio limit/seed limit for deletion including cross-seeds n_info = '' tdel_dict = {} #dictionary to track the torrent names and content path that meet the deletion criteria + t_excl_tags = []#list of tags to exclude based on config.yml torrent_list = client.torrents.info(category=category,filter='completed') + + #Convert string to list if only one tag defined. + if ('exclude_tags' in nohardlinks[category]): + if isinstance(nohardlinks[category]['exclude_tags'],str): + t_excl_tags.append(nohardlinks[category]['exclude_tags']) + else: + t_excl_tags = nohardlinks[category]['exclude_tags'] + if len(torrent_list) == 0: - logger.error('The category ('+category+') defined in config.yml inside the nohardlinks section does not match any category in qbittorrent. Please make sure the category defined in config matches with one in qbittorrent.') + logger.error('No torrents found in the category ('+category+') defined in config.yml inside the nohardlinks section. Please check if this matches with any category in qbittorrent and has 1 or more torrents.') continue for torrent in torrent_list: if args.dry_run != 'dry_run': - torrent.resume() - #Checks for any hard links and not already tagged - if (nohardlink(torrent['content_path'].replace(root_path,remote_path))): - - #Will only tag new torrents that don't have noHL tag - if('noHL' not in torrent.tags): - t_count += 1 - n_info += (f'\n - Torrent Name: {torrent.name} has no hard links found.') - n_info += (' Adding tags noHL.') - if(nohardlinks[category] != None): - #set the max seeding time for the torrent - if ('max_seeding_time' in nohardlinks[category]): - seeding_time_limit = nohardlinks[category]['max_seeding_time'] - n_info += (' \n Setting max seed time to ' + str(seeding_time_limit) + '.') + torrent.resume() + if('exclude_tags' in nohardlinks[category] and (any(tag in torrent.tags for tag in t_excl_tags))): + #Skip to the next torrent if we find any torrents that are in the exclude tag + continue + else: + #Checks for any hard links and not already tagged + if (nohardlink(torrent['content_path'].replace(root_path,remote_path))): + #Will only tag new torrents that don't have noHL tag + if('noHL' not in torrent.tags): + t_count += 1 + n_info += (f'\n - Torrent Name: {torrent.name} has no hard links found.') + n_info += (' Adding tags noHL.') + if(nohardlinks[category] != None): + #set the max seeding time for the torrent + if ('max_seeding_time' in nohardlinks[category]): + seeding_time_limit = nohardlinks[category]['max_seeding_time'] + n_info += (' \n Setting max seed time to ' + str(seeding_time_limit) + '.') + else: + seeding_time_limit = -2 + #set the max ratio for the torrent + if ('max_ratio' in nohardlinks[category]): + ratio_limit = nohardlinks[category]['max_ratio'] + n_info += (' \n Setting max ratio to ' + str(ratio_limit)+ '.') + else: + ratio_limit = -2 else: seeding_time_limit = -2 - #set the max ratio for the torrent - if ('max_ratio' in nohardlinks[category]): - ratio_limit = nohardlinks[category]['max_ratio'] - n_info += (' \n Setting max ratio to ' + str(ratio_limit)+ '.') - else: ratio_limit = -2 + if args.dry_run != 'dry_run': + #set the tag for no hard links + torrent.add_tags(tags='noHL') + client.torrents_set_share_limits(ratio_limit,seeding_time_limit,torrent.hash) + + #Cleans up previously tagged noHL torrents else: - seeding_time_limit = -2 - ratio_limit = -2 - if args.dry_run != 'dry_run': - #set the tag for no hard links - torrent.add_tags(tags='noHL') - client.torrents_set_share_limits(ratio_limit,seeding_time_limit,torrent.hash) - - #Cleans up previously tagged noHL torrents - else: - if(nohardlinks[category] != None): - # Deletes torrent with data if cleanup is set to true and meets the ratio/seeding requirements - if ('cleanup' in nohardlinks[category] and nohardlinks[category]['cleanup'] and torrent.state_enum.is_paused and len(nohardlinks[category])>0): - t_del += 1 - n_info += (f'\n - Torrent Name: {torrent.name} has no hard links found and meets ratio/seeding requirements.') - tdel_dict[torrent.name] = torrent['content_path'].replace(root_path,remote_path) - if args.dry_run == 'dry_run': - n_info += (' \n Cleanup flag set to true. NOT Deleting torrent + contents.') - else: - n_info += (' \n Cleanup flag set to true. Deleting torrent + contents.') + if(nohardlinks[category] != None): + # Deletes torrent with data if cleanup is set to true and meets the ratio/seeding requirements + if ('cleanup' in nohardlinks[category] and nohardlinks[category]['cleanup'] and torrent.state_enum.is_paused and len(nohardlinks[category])>0): + t_del += 1 + n_info += (f'\n - Torrent Name: {torrent.name} has no hard links found and meets ratio/seeding requirements.') + tdel_dict[torrent.name] = torrent['content_path'].replace(root_path,remote_path) + if args.dry_run == 'dry_run': + n_info += (' \n Cleanup flag set to true. NOT Deleting torrent + contents.') + else: + n_info += (' \n Cleanup flag set to true. Deleting torrent + contents.') if(nohardlinks[category] != None): #loop through torrent list again for cleanup purposes From 63d5f6abde4ebe90b6149cfaff7843b7b1f702bd Mon Sep 17 00:00:00 2001 From: Jon Date: Wed, 29 Sep 2021 10:35:55 -0400 Subject: [PATCH 2/5] bug_fix: allow empty remote_dir bug_fix: check previous noHL tagged torrents that now have HL. --- qbit_manage.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/qbit_manage.py b/qbit_manage.py index fc190bf9..c14b3179 100644 --- a/qbit_manage.py +++ b/qbit_manage.py @@ -422,7 +422,7 @@ def rem_orphaned(): logger.error('root_dir not defined in config.') return - if 'remote_dir' in cfg['directory']: + if ('remote_dir' in cfg['directory'] and cfg['directory']['remote_dir'] != ''): remote_path = os.path.join(cfg['directory']['remote_dir'], '') root_files = [os.path.join(path.replace(remote_path,root_path), name) for path, subdirs, files in os.walk(remote_path) for name in files if os.path.join(remote_path,'orphaned_data') not in path] else: @@ -480,11 +480,11 @@ def tag_nohardlinks(): else: logger.error('root_dir not defined in config.') return - if 'remote_dir' in cfg['directory']: + if ('remote_dir' in cfg['directory'] and cfg['directory']['remote_dir'] != ''): remote_path = os.path.join(cfg['directory']['remote_dir'], '') else: remote_path = root_path - + for category in nohardlinks: t_count = 0 #counter for the number of torrents that has no hard links t_del = 0 #counter for the number of torrents that has no hard links and meets the criteria for ratio limit/seed limit for deletion @@ -551,7 +551,17 @@ def tag_nohardlinks(): n_info += (' \n Cleanup flag set to true. NOT Deleting torrent + contents.') else: n_info += (' \n Cleanup flag set to true. Deleting torrent + contents.') - + + #Checks to see if previous noHL tagged torrents now have hard links. + if (not (nohardlink(torrent['content_path'].replace(root_path,remote_path))) and ('noHL' in torrent.tags)): + n_info += (f'\n - Previous Tagged noHL Torrent Name: {torrent.name} has hard links found now.') + n_info += (' Removing tags noHL.') + n_info += (' Removing ratio and seeding time limits.') + if args.dry_run != 'dry_run': + #Remove tags and share limits + torrent.remove_tags(tags='noHL') + client.torrents_set_share_limits(-2,-2,torrent.hash) + if(nohardlinks[category] != None): #loop through torrent list again for cleanup purposes if ('cleanup' in nohardlinks[category] and nohardlinks[category]['cleanup']): @@ -566,17 +576,6 @@ def tag_nohardlinks(): else: torrent.delete(hash=torrent.hash, delete_files=False) - - #Checks to see if previous noHL tagged torrents now have hard links. - if (not (nohardlink(torrent['content_path'].replace(root_path,remote_path))) and ('noHL' in torrent.tags)): - n_info += (f'\n - Previous Tagged noHL Torrent Name: {torrent.name} has hard links found now.') - n_info += (' Removing tags noHL.') - n_info += (' Removing ratio and seeding time limits.') - if args.dry_run != 'dry_run': - #Remove tags and share limits - torrent.remove_tags(tags='noHL') - client.torrents_set_share_limits(-2,-2,torrent.hash) - if args.dry_run == 'dry_run': if t_count >= 1 or len(n_info) > 1: logger.dryrun(n_info) From 5b8b47a7977f7e2141b78399816d41ebdbba2ee3 Mon Sep 17 00:00:00 2001 From: Jon Date: Wed, 29 Sep 2021 10:56:53 -0400 Subject: [PATCH 3/5] update better logging for deleting noHL tags --- qbit_manage.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/qbit_manage.py b/qbit_manage.py index c14b3179..4607bd96 100644 --- a/qbit_manage.py +++ b/qbit_manage.py @@ -489,6 +489,7 @@ def tag_nohardlinks(): t_count = 0 #counter for the number of torrents that has no hard links t_del = 0 #counter for the number of torrents that has no hard links and meets the criteria for ratio limit/seed limit for deletion t_del_cs = 0 #counter for the number of torrents that has no hard links and meets the criteria for ratio limit/seed limit for deletion including cross-seeds + tdel_tags = 0 #counter for number of torrents that previously had no hard links but now have hard links n_info = '' tdel_dict = {} #dictionary to track the torrent names and content path that meet the deletion criteria t_excl_tags = []#list of tags to exclude based on config.yml @@ -557,6 +558,7 @@ def tag_nohardlinks(): n_info += (f'\n - Previous Tagged noHL Torrent Name: {torrent.name} has hard links found now.') n_info += (' Removing tags noHL.') n_info += (' Removing ratio and seeding time limits.') + tdel_tags += 1 if args.dry_run != 'dry_run': #Remove tags and share limits torrent.remove_tags(tags='noHL') @@ -576,25 +578,29 @@ def tag_nohardlinks(): else: torrent.delete(hash=torrent.hash, delete_files=False) - if args.dry_run == 'dry_run': - if t_count >= 1 or len(n_info) > 1: - logger.dryrun(n_info) - logger.dryrun(f'Did not tag/set ratio limit/seeding time for {t_count} .torrents(s)') - if t_del >= 1: - logger.dryrun(f'Did not delete {t_del} .torrents(s) or content files.') - logger.dryrun(f'Did not delete {t_del_cs} .torrents(s) (including cross-seed) or content files.') - else: - logger.dryrun('No torrents to tag with no hard links.') + if args.dry_run == 'dry_run': + if t_count >= 1 or len(n_info) > 1: + logger.dryrun(n_info) + logger.dryrun(f'Did not tag/set ratio limit/seeding time for {t_count} .torrents(s)') + if t_del >= 1: + logger.dryrun(f'Did not delete {t_del} .torrents(s) or content files.') + logger.dryrun(f'Did not delete {t_del_cs} .torrents(s) (including cross-seed) or content files.') + if tdel_tags >= 1: + logger.dryrun(f'Did not delete noHL tags/unset ratio limit/seeding time for {tdel_tags} .torrents(s)') else: - - if t_count >= 1 or len(n_info) > 1: - logger.info(n_info) - logger.info(f'tag/set ratio limit/seeding time for {t_count} .torrents(s)') - if t_del >= 1: - logger.info(f'Deleted {t_del} .torrents(s) AND content files.') - logger.info(f'Deleted {t_del_cs} .torrents(s) (includes cross-seed torrents) AND content files.') - else: - logger.info('No torrents to tag with no hard links.') + logger.dryrun('No torrents to tag with no hard links.') + else: + + if t_count >= 1 or len(n_info) > 1: + logger.info(n_info) + logger.info(f'tag/set ratio limit/seeding time for {t_count} .torrents(s)') + if t_del >= 1: + logger.info(f'Deleted {t_del} .torrents(s) AND content files.') + logger.info(f'Deleted {t_del_cs} .torrents(s) (includes cross-seed torrents) AND content files.') + if tdel_tags >= 1: + logger.info(f'Deleted noHL tags/ remove ratio limit/seeding time for {tdel_tags} .torrents(s)') + else: + logger.info('No torrents to tag with no hard links.') #will check if there are any hard links if it passes a file or folder From 9f9e3a52d6021d4ac7f1373146ff466e1aa74001 Mon Sep 17 00:00:00 2001 From: Jon Date: Wed, 29 Sep 2021 10:58:45 -0400 Subject: [PATCH 4/5] consistency in dryrun --- qbit_manage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qbit_manage.py b/qbit_manage.py index 4607bd96..d66a57b3 100644 --- a/qbit_manage.py +++ b/qbit_manage.py @@ -586,7 +586,7 @@ def tag_nohardlinks(): logger.dryrun(f'Did not delete {t_del} .torrents(s) or content files.') logger.dryrun(f'Did not delete {t_del_cs} .torrents(s) (including cross-seed) or content files.') if tdel_tags >= 1: - logger.dryrun(f'Did not delete noHL tags/unset ratio limit/seeding time for {tdel_tags} .torrents(s)') + logger.dryrun(f'Did not delete noHL tags/ remove ratio limit/seeding time for {tdel_tags} .torrents(s)') else: logger.dryrun('No torrents to tag with no hard links.') else: From f58713fa2053433910b976cb2bc821081ee0311e Mon Sep 17 00:00:00 2001 From: Jon Date: Wed, 29 Sep 2021 13:42:59 -0400 Subject: [PATCH 5/5] removed whitespace --- qbit_manage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qbit_manage.py b/qbit_manage.py index d66a57b3..0ea0d4bf 100644 --- a/qbit_manage.py +++ b/qbit_manage.py @@ -484,7 +484,7 @@ def tag_nohardlinks(): remote_path = os.path.join(cfg['directory']['remote_dir'], '') else: remote_path = root_path - + for category in nohardlinks: t_count = 0 #counter for the number of torrents that has no hard links t_del = 0 #counter for the number of torrents that has no hard links and meets the criteria for ratio limit/seed limit for deletion