Merge pull request #20 from StuffAnThings/develop

Feat release: Excluding tags for no hard links
This commit is contained in:
bobokun 2021-09-29 14:03:32 -04:00 committed by GitHub
commit ce1e009dac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 72 deletions

View file

@ -40,7 +40,11 @@ tags:
nohardlinks: nohardlinks:
# Mandatory to fill out directory parameter above to use this function (root_dir/remote_dir) # 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 # 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:
#<OPTIONAL> exclude_tags var: Will exclude the following tags when searching through the category.
exclude_tags:
- Beyond-HD
- AnimeBytes
#<OPTIONAL> 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 #<OPTIONAL> 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 cleanup: false
#<OPTIONAL> max_ratio var: Will set the torrent Maximum share ratio until torrent is stopped from seeding/uploading #<OPTIONAL> 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. #Can have additional categories set with separate ratio/seeding times defined.
series-completed: series-completed:
#<OPTIONAL> exclude_tags var: Will exclude the following tags when searching through the category.
exclude_tags:
- Beyond-HD
- BroadcasTheNet
#<OPTIONAL> 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 #<OPTIONAL> 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 cleanup: false
#<OPTIONAL> max_ratio var: Will set the torrent Maximum share ratio until torrent is stopped from seeding/uploading #<OPTIONAL> max_ratio var: Will set the torrent Maximum share ratio until torrent is stopped from seeding/uploading

View file

@ -422,7 +422,7 @@ def rem_orphaned():
logger.error('root_dir not defined in config.') logger.error('root_dir not defined in config.')
return 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'], '') 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] 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: else:
@ -480,7 +480,7 @@ def tag_nohardlinks():
else: else:
logger.error('root_dir not defined in config.') logger.error('root_dir not defined in config.')
return 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'], '') remote_path = os.path.join(cfg['directory']['remote_dir'], '')
else: else:
remote_path = root_path remote_path = root_path
@ -489,57 +489,81 @@ def tag_nohardlinks():
t_count = 0 #counter for the number of torrents that has no hard links 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 = 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 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 = '' n_info = ''
tdel_dict = {} #dictionary to track the torrent names and content path that meet the deletion criteria 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') 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: 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 continue
for torrent in torrent_list: for torrent in torrent_list:
if args.dry_run != 'dry_run': if args.dry_run != 'dry_run':
torrent.resume() torrent.resume()
#Checks for any hard links and not already tagged if('exclude_tags' in nohardlinks[category] and (any(tag in torrent.tags for tag in t_excl_tags))):
if (nohardlink(torrent['content_path'].replace(root_path,remote_path))): #Skip to the next torrent if we find any torrents that are in the exclude tag
continue
#Will only tag new torrents that don't have noHL tag else:
if('noHL' not in torrent.tags): #Checks for any hard links and not already tagged
t_count += 1 if (nohardlink(torrent['content_path'].replace(root_path,remote_path))):
n_info += (f'\n - Torrent Name: {torrent.name} has no hard links found.') #Will only tag new torrents that don't have noHL tag
n_info += (' Adding tags noHL.') if('noHL' not in torrent.tags):
if(nohardlinks[category] != None): t_count += 1
#set the max seeding time for the torrent n_info += (f'\n - Torrent Name: {torrent.name} has no hard links found.')
if ('max_seeding_time' in nohardlinks[category]): n_info += (' Adding tags noHL.')
seeding_time_limit = nohardlinks[category]['max_seeding_time'] if(nohardlinks[category] != None):
n_info += (' \n Setting max seed time to ' + str(seeding_time_limit) + '.') #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: else:
seeding_time_limit = -2 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 ratio_limit = -2
else: if args.dry_run != 'dry_run':
seeding_time_limit = -2 #set the tag for no hard links
ratio_limit = -2 torrent.add_tags(tags='noHL')
if args.dry_run != 'dry_run': client.torrents_set_share_limits(ratio_limit,seeding_time_limit,torrent.hash)
#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 #Cleans up previously tagged noHL torrents
else: else:
if(nohardlinks[category] != None): if(nohardlinks[category] != None):
# Deletes torrent with data if cleanup is set to true and meets the ratio/seeding requirements # 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): if ('cleanup' in nohardlinks[category] and nohardlinks[category]['cleanup'] and torrent.state_enum.is_paused and len(nohardlinks[category])>0):
t_del += 1 t_del += 1
n_info += (f'\n - Torrent Name: {torrent.name} has no hard links found and meets ratio/seeding requirements.') 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) tdel_dict[torrent.name] = torrent['content_path'].replace(root_path,remote_path)
if args.dry_run == 'dry_run': if args.dry_run == 'dry_run':
n_info += (' \n Cleanup flag set to true. NOT Deleting torrent + contents.') n_info += (' \n Cleanup flag set to true. NOT Deleting torrent + contents.')
else: else:
n_info += (' \n Cleanup flag set to true. Deleting torrent + contents.') 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.')
tdel_tags += 1
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): if(nohardlinks[category] != None):
#loop through torrent list again for cleanup purposes #loop through torrent list again for cleanup purposes
if ('cleanup' in nohardlinks[category] and nohardlinks[category]['cleanup']): if ('cleanup' in nohardlinks[category] and nohardlinks[category]['cleanup']):
@ -554,36 +578,29 @@ def tag_nohardlinks():
else: else:
torrent.delete(hash=torrent.hash, delete_files=False) torrent.delete(hash=torrent.hash, delete_files=False)
if args.dry_run == 'dry_run':
#Checks to see if previous noHL tagged torrents now have hard links. if t_count >= 1 or len(n_info) > 1:
if (not (nohardlink(torrent['content_path'].replace(root_path,remote_path))) and ('noHL' in torrent.tags)): logger.dryrun(n_info)
n_info += (f'\n - Previous Tagged noHL Torrent Name: {torrent.name} has hard links found now.') logger.dryrun(f'Did not tag/set ratio limit/seeding time for {t_count} .torrents(s)')
n_info += (' Removing tags noHL.') if t_del >= 1:
n_info += (' Removing ratio and seeding time limits.') logger.dryrun(f'Did not delete {t_del} .torrents(s) or content files.')
if args.dry_run != 'dry_run': logger.dryrun(f'Did not delete {t_del_cs} .torrents(s) (including cross-seed) or content files.')
#Remove tags and share limits if tdel_tags >= 1:
torrent.remove_tags(tags='noHL') logger.dryrun(f'Did not delete noHL tags/ remove ratio limit/seeding time for {tdel_tags} .torrents(s)')
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)
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.')
else: else:
logger.dryrun('No torrents to tag with no hard links.')
if t_count >= 1 or len(n_info) > 1: else:
logger.info(n_info)
logger.info(f'tag/set ratio limit/seeding time for {t_count} .torrents(s)') if t_count >= 1 or len(n_info) > 1:
if t_del >= 1: logger.info(n_info)
logger.info(f'Deleted {t_del} .torrents(s) AND content files.') logger.info(f'tag/set ratio limit/seeding time for {t_count} .torrents(s)')
logger.info(f'Deleted {t_del_cs} .torrents(s) (includes cross-seed torrents) AND content files.') if t_del >= 1:
else: logger.info(f'Deleted {t_del} .torrents(s) AND content files.')
logger.info('No torrents to tag with no hard links.') 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 #will check if there are any hard links if it passes a file or folder