mirror of
				https://github.com/StuffAnThings/qbit_manage.git
				synced 2025-11-04 21:09:08 +08:00 
			
		
		
		
	Merge pull request #20 from StuffAnThings/develop
Feat release: Excluding tags for no hard links
This commit is contained in:
		
						commit
						ce1e009dac
					
				
					 2 changed files with 97 additions and 72 deletions
				
			
		| 
						 | 
				
			
			@ -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:
 | 
			
		||||
    #<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
 | 
			
		||||
    cleanup: false
 | 
			
		||||
    #<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.
 | 
			
		||||
  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
 | 
			
		||||
    cleanup: false
 | 
			
		||||
    #<OPTIONAL> max_ratio var: Will set the torrent Maximum share ratio until torrent is stopped from seeding/uploading
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										159
									
								
								qbit_manage.py
									
										
									
									
									
								
							
							
						
						
									
										159
									
								
								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,7 +480,7 @@ 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
 | 
			
		||||
| 
						 | 
				
			
			@ -489,57 +489,81 @@ 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
 | 
			
		||||
            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
 | 
			
		||||
                        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)
 | 
			
		||||
                            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.')
 | 
			
		||||
            
 | 
			
		||||
                        #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.')
 | 
			
		||||
                
 | 
			
		||||
                #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):
 | 
			
		||||
                #loop through torrent list again for cleanup purposes
 | 
			
		||||
                if ('cleanup' in nohardlinks[category] and nohardlinks[category]['cleanup']):
 | 
			
		||||
| 
						 | 
				
			
			@ -554,36 +578,29 @@ 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)
 | 
			
		||||
                    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/ remove 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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue