Breaking changes!

Adjusted post-argument commands. Refer to -h for new list of updated commands
New feature: Built-in Scheduler
Enhancements: Code optimizations to run faster
Enhancements: Better error checking
Enhancements: Better logging
This commit is contained in:
Jon 2021-11-22 14:53:01 -05:00
parent 5ae5089b0a
commit 7ca2983f08
No known key found for this signature in database
GPG key ID: 9665BA6CF5DC2671
5 changed files with 213 additions and 233 deletions

View file

@ -10,8 +10,9 @@ jobs:
steps:
- uses: actions/checkout@v2
with:
token: ${{ secrets.PAT }}
fetch-depth: 2
- uses: salsify/action-detect-and-tag-new-version@v2
- uses: salsify/action-detect-and-tag-new-version@v1.0.3
with:
version-command: |
cat VERSION

View file

@ -10,7 +10,7 @@ This is a program used to manage your qBittorrent instance such as:
* Remove orphaned files from your root directory that are not referenced by qBittorrent
* Tag any torrents that have no hard links and allows optional cleanup to delete these torrents and contents based on maximum ratio and/or time seeded
* RecycleBin function to move files into a RecycleBin folder instead of deleting the data directly when deleting a torrent
* Built-in scheduler to run the script every x minutes. (Can use `--run` command to run without the scheduler)
## Installation
* Dependencies
@ -85,7 +85,7 @@ To run the script in an interactive terminal run:
* copy the `config.yml.sample` file to `config.yml`
* add your qBittorrent host, user and pass. If you are not using a username and password you can remove the `user` and `pass` lines.
* add your `cross_seed` and `root_dir`. If you are using a docker container you must fill out `remote_dir` as well.
* add your `cross_seed` and `root_dir`. If you're running cross-seed in a docker container you must fill out `remote_dir` as well.
* Add your categories and save path to match with what is being used in your qBittorrent instance. I suggest using the full path when defining `save_path`
* Add the `tag` definition based on tracker URL
* Modify the `nohardlinks` by specifying your completed movies/series category to match with qBittorrent. Please ensure the `root_dir` and/or `remote_dir` is added in the `directory` section
@ -100,21 +100,20 @@ python qbit_manage.py -h
| Shell Command | Description | Default Value |
| :------------ | :------------ | :------------ |
| `--run` | Run without the scheduler. Script will exit after completion. | False |
| `-r` or`--run` | Run without the scheduler. Script will exit after completion. | False |
| `-sch` or `--schedule` | Schedule to run every x minutes. (Default set to 30) | 30 |
| `-c CONFIG` or `--config-file CONFIG` | This is used if you want to use a different name for your config.yml. `Example: tv.yml` | config.yml |
| `-l LOGFILE,` or `--log-file LOGFILE,` | This is used if you want to use a different name for your log file. `Example: tv.log` | activity.log |
| `-m` or `--manage` | Use this if you would like to update your tags, categories, remove unregistered torrents, recheck/resume paused torrents, and empty recycle bin. | |
| `-s` or `--cross-seed` | Use this after running [cross-seed script](https://github.com/mmgoodnow/cross-seed) to add torrents from the cross-seed output folder to qBittorrent | |
| `-re` or `--recheck` | Recheck paused torrents sorted by lowest size. Resume if Completed. | |
| `-g` or `--cat-update` | Use this if you would like to update your categories. | |
| `-t` or `--tag-update` | Use this if you would like to update your tags. (Only adds tags to untagged torrents) | |
| `-r` or `--rem-unregistered` | Use this if you would like to remove unregistered torrents. (It will the delete data & torrent if it is not being cross-seeded, otherwise it will just remove the torrent without deleting data) | |
| `-ro` or `--rem-orphaned` | Use this if you would like to remove orphaned files from your `root_dir` directory that are not referenced by any torrents. It will scan your `root_dir` directory and compare it with what is in qBittorrent. Any data not referenced in qBittorrent will be moved into `/data/torrents/orphaned_data` folder for you to review/delete. | |
| `-tnhl` or `--tag-nohardlinks` | Use this to tag any torrents that do not have any hard links associated with any of the files. This is useful for those that use Sonarr/Radarr that hard links your media files with the torrents for seeding. When files get upgraded they no longer become linked with your media therefore will be tagged with a new tag noHL. You can then safely delete/remove these torrents to free up any extra space that is not being used by your media folder. | |
| `-er` or `--empty-recycle` | Use this to empty your Reycle Bin folder based on x number of days defined in the config. Setting empty_after_x_days to 0 in the config will immediately empty the Recycle Bin folder. | |
| `--dry-run` | If you would like to see what is gonna happen but not actually move/delete or tag/categorize anything. | |
| `--log LOGLEVEL` | Change the ouput log level. | INFO |
| `-lf LOGFILE,` or `--log-file LOGFILE,` | This is used if you want to use a different name for your log file. `Example: tv.log` | activity.log |
| `-cs` or `--cross-seed` | Use this after running [cross-seed script](https://github.com/mmgoodnow/cross-seed) to add torrents from the cross-seed output folder to qBittorrent | False |
| `-re` or `--recheck` | Recheck paused torrents sorted by lowest size. Resume if Completed. | False |
| `-cu` or `--cat-update` | Use this if you would like to update your categories. | False |
| `-tu` or `--tag-update` | Use this if you would like to update your tags. (Only adds tags to untagged torrents) | False |
| `-ru` or `--rem-unregistered` | Use this if you would like to remove unregistered torrents. (It will the delete data & torrent if it is not being cross-seeded, otherwise it will just remove the torrent without deleting data) | False |
| `-ro` or `--rem-orphaned` | Use this if you would like to remove orphaned files from your `root_dir` directory that are not referenced by any torrents. It will scan your `root_dir` directory and compare it with what is in qBittorrent. Any data not referenced in qBittorrent will be moved into `/data/torrents/orphaned_data` folder for you to review/delete. | False |
| `-tnhl` or `--tag-nohardlinks` | Use this to tag any torrents that do not have any hard links associated with any of the files. This is useful for those that use Sonarr/Radarr that hard links your media files with the torrents for seeding. When files get upgraded they no longer become linked with your media therefore will be tagged with a new tag noHL. You can then safely delete/remove these torrents to free up any extra space that is not being used by your media folder. | False |
| `-sr` or `--skip-recycle` | Use this to skip emptying the Reycle Bin folder (`/root_dir/.RecycleBin`). | False |
| `-dr` or `--dry-run` | If you would like to see what is gonna happen but not actually move/delete or tag/categorize anything. | False |
| `-ll` or `--log-level LOGLEVEL` | Change the ouput log level. | INFO |
### Config

View file

@ -1 +1 @@
2.0-develop
2.0

View file

@ -7,8 +7,9 @@ qbt:
directory:
# Do not remove these
# Cross-seed var: </your/path/here/> #Output directory of cross-seed
# root_dir var: </your/path/here/> #Root downloads directory used to check for orphaned files and used in RecycleBin
# <OPTIONAL> remote_dir var: </your/path/here/> # Path of docker host mapping of root_dir. Must be set if you are using docker!
# root_dir var: </your/path/here/> #Root downloads directory used to check for orphaned files, noHL, and RecycleBin.
# <OPTIONAL> remote_dir var: </your/path/here/> # Path of docker host mapping of root_dir.
# Must be set if you're running qbit_manage locally and qBittorrent/cross_seed is in a docker
cross_seed: "/your/path/here/"
root_dir: "/data/torrents/"
remote_dir: "/mnt/user/data/torrents/"
@ -69,10 +70,11 @@ nohardlinks:
#<OPTIONAL> seeding time var: Will set the torrent Maximum seeding time (min) until torrent is stopped from seeding
max_seeding_time: 86400
#Recycle Bin method of deletion will move files into the recycle bin instead of directly deleting them in qbit
#Recycle Bin method of deletion will move files into the recycle bin (Located in /root_dir/.RecycleBin) instead of directly deleting them in qbit
#By default the Recycle Bin will be emptied on every run of the qbit_manage script if empty_after_x_days is defined.
recyclebin:
enabled: true
#<OPTIONAL> empty_after_x_days var: Will automatically remove all files and folders in recycle bin after x days.
#<OPTIONAL> empty_after_x_days var: Will automatically remove all files and folders in recycle bin after x days. (Checks every script run)
# If this variable is not defined it, the RecycleBin will never be emptied.
# Setting this variable to 0 will delete files immediately.
# WARNING: Setting this variable to 0 will delete all files immediately upon script run!
empty_after_x_days: 60

View file

@ -2,11 +2,9 @@
import os
import shutil
import yaml
import argparse
import logging
import logging.handlers
from qbittorrentapi import Client
import urllib3
from collections import Counter
import glob
@ -15,89 +13,36 @@ import datetime
import time
import stat
import sys
import schedule
try:
from qbittorrentapi import Client
import yaml
import schedule
except ModuleNotFoundError:
print("Requirements Error: Requirements are not installed")
sys.exit(0)
if sys.version_info[0] != 3 or sys.version_info[1] < 6:
print("Version Error: Version: %s.%s.%s incompatible please use Python 3.6+" % (sys.version_info[0], sys.version_info[1], sys.version_info[2]))
sys.exit(0)
parser = argparse.ArgumentParser('qBittorrent Manager.',
description='A mix of scripts combined for managing qBittorrent.')
parser.add_argument("--run", dest="run", help="Run without the scheduler. Script will exit after completion.", action="store_true", default=False)
parser.add_argument("-sch", "--schedule", dest="sch", help="Schedule to run every x minutes. (Default set to 30)", default="30", type=str)
parser.add_argument('-c', '--config-file',
dest='config',
action='store',
default='config.yml',
help='This is used if you want to use a different name for your config.yml. Example: tv.yml')
parser.add_argument('-l', '--log-file',
dest='logfile',
action='store',
default='activity.log',
help='This is used if you want to use a different name for your log file. Example: tv.log')
parser.add_argument('-m', '--manage',
dest='manage',
action='store_const',
const='manage',
help='Use this if you would like to update your tags, categories,'
' remove unregistered torrents, recheck/resume paused torrents, and empty recycle bin.')
parser.add_argument('-s', '--cross-seed',
dest='cross_seed',
action='store_const',
const='cross_seed',
help='Use this after running cross-seed script to add torrents from the cross-seed output folder to qBittorrent')
parser.add_argument('-re', '--recheck',
dest='recheck',
action='store_const',
const='recheck',
help='Recheck paused torrents sorted by lowest size. Resume if Completed.')
parser.add_argument('-g', '--cat-update',
dest='cat_update',
action='store_const',
const='cat_update',
help='Use this if you would like to update your categories.')
parser.add_argument('-t', '--tag-update',
dest='tag_update',
action='store_const',
const='tag_update',
help='Use this if you would like to update your tags. (Only adds tags to untagged torrents)')
parser.add_argument('-r', '--rem-unregistered',
dest='rem_unregistered',
action='store_const',
const='rem_unregistered',
help='Use this if you would like to remove unregistered torrents.')
parser.add_argument('-ro', '--rem-orphaned',
dest='rem_orphaned',
action='store_const',
const='rem_orphaned',
help='Use this if you would like to remove orphaned files from your `root_dir` directory that are not referenced by any torrents.'
' It will scan your `root_dir` directory and compare it with what is in Qbitorrent. Any data not referenced in Qbitorrent will be moved into '
' `/root_dir/orphaned_data` folder for you to review/delete.')
parser.add_argument('-tnhl', '--tag-nohardlinks',
dest='tag_nohardlinks',
action='store_const',
const='tag_nohardlinks',
help='Use this to tag any torrents that do not have any hard links associated with any of the files. This is useful for those that use Sonarr/Radarr'
'that hard link your media files with the torrents for seeding. When files get upgraded they no longer become linked with your media therefore will be tagged with a new tag noHL'
'You can then safely delete/remove these torrents to free up any extra space that is not being used by your media folder.')
parser.add_argument('-er', '--empty-recycle',
dest='empty_recycle',
action='store_const',
const='empty_recycle',
help='Use this to empty your Reycle Bin folder based on x number of days defined in the config.'
'Setting "empty_after_x_days" variable to 0 will delete files immediately.'
'If this variable is not defined the RecycleBin will never be emptied.')
parser.add_argument('--dry-run',
dest='dry_run',
action='store_const',
const='dry_run',
help='If you would like to see what is gonna happen but not actually move/delete or '
'tag/categorize anything.')
parser.add_argument('--log',
dest='loglevel',
action='store',
default='INFO',
help='Change your log level. ')
parser = argparse.ArgumentParser('qBittorrent Manager.', description='A mix of scripts combined for managing qBittorrent.')
parser.add_argument('-r', '--run', dest='run', action='store_true', default=False, help='Run without the scheduler. Script will exit after completion.')
parser.add_argument('-sch', '--schedule', dest='min', default='30', type=str, help='Schedule to run every x minutes. (Default set to 30)')
parser.add_argument('-c', '--config-file', dest='configfile', action='store', default='config.yml', type=str, help='This is used if you want to use a different name for your config.yml. Example: tv.yml')
parser.add_argument('-lf', '--log-file', dest='logfile', action='store',default='activity.log', type=str, help='This is used if you want to use a different name for your log file. Example: tv.log',)
parser.add_argument('-cs', '--cross-seed', dest='cross_seed', action="store_true", default=False, help='Use this after running cross-seed script to add torrents from the cross-seed output folder to qBittorrent')
parser.add_argument('-re', '--recheck', dest='recheck', action="store_true", default=False, help='Recheck paused torrents sorted by lowest size. Resume if Completed.')
parser.add_argument('-cu', '--cat-update', dest='cat_update', action="store_true", default=False, help='Use this if you would like to update your categories.')
parser.add_argument('-tu', '--tag-update', dest='tag_update', action="store_true", default=False, help='Use this if you would like to update your tags. (Only adds tags to untagged torrents)')
parser.add_argument('-ru', '--rem-unregistered', dest='rem_unregistered', action="store_true", default=False, help='Use this if you would like to remove unregistered torrents.')
parser.add_argument('-ro', '--rem-orphaned', dest='rem_orphaned', action="store_true", default=False, help='Use this if you would like to remove unregistered torrents.')
parser.add_argument('-tnhl', '--tag-nohardlinks', dest='tag_nohardlinks', action="store_true", default=False, help='Use this to tag any torrents that do not have any hard links associated with any of the files. This is useful for those that use Sonarr/Radarr which hard link your media files with the torrents for seeding. When files get upgraded they no longer become linked with your media therefore will be tagged with a new tag noHL. You can then safely delete/remove these torrents to free up any extra space that is not being used by your media folder.')
parser.add_argument('-sr', '--skip-recycle', dest='skip_recycle', action="store_true", default=False, help='Use this to skip emptying the Reycle Bin folder.')
parser.add_argument('-dr', '--dry-run', dest='dry_run', action="store_true", default=False, help='If you would like to see what is gonna happen but not actually move/delete or tag/categorize anything.')
parser.add_argument('-ll', '--log-level', dest='log_level', action="store", default='INFO', type=str, help='Change your log level.')
args = parser.parse_args()
def get_arg(env_str, default, arg_bool=False, arg_int=False):
@ -118,19 +63,33 @@ def get_arg(env_str, default, arg_bool=False, arg_int=False):
return default
run = get_arg("QBT_RUN", args.run, arg_bool=True)
sch = get_arg("QBT_SCHEDULE", args.sch)
config_file = get_arg("QBT_CONFIG", args.config)
sch = get_arg("QBT_SCHEDULE", args.min)
config_file = get_arg("QBT_CONFIG", args.configfile)
log_file = get_arg("QBT_LOGFILE", args.logfile)
cross_seed = get_arg("QBT_CROSS_SEED", args.cross_seed, arg_bool=True)
recheck = get_arg("QBT_RECHECK", args.recheck, arg_bool=True)
cat_update = get_arg("QBT_CAT_UPDATE", args.cat_update, arg_bool=True)
tag_update = get_arg("QBT_TAG_UPDATE", args.tag_update, arg_bool=True)
rem_unregistered = get_arg("QBT_REM_UNREGISTERED", args.rem_unregistered, arg_bool=True)
rem_orphaned = get_arg("QBT_REM_ORPHANED", args.rem_orphaned, arg_bool=True)
tag_nohardlinks = get_arg("QBT_TAG_NOHARDLINKS", args.tag_nohardlinks, arg_bool=True)
skip_recycle = get_arg("QBT_TAG_SKIP_RECYCLE", args.skip_recycle, arg_bool=True)
dry_run = get_arg("QBT_DRY_RUN", args.dry_run, arg_bool=True)
log_level = get_arg("QBT_LOG_LEVEL", args.log_level)
default_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config")
root_path = '' #Global variable
remote_path = '' #Global variable
#Check if Schedule parameter is a number
if sch.isnumeric():
sch = int(sch)
else:
print(f"Schedule Error: Schedule is not a number. Current value is set to '{sch}'")
sys.exit(0)
#Config error handling
if not os.path.exists(os.path.join(default_dir, config_file)):
print(f"Config Error: config not found at {os.path.join(os.path.abspath(default_dir),config_file)}")
sys.exit(0)
@ -139,13 +98,42 @@ with open(os.path.join(default_dir,config_file), 'r') as cfg_file:
cfg = yaml.load(cfg_file, Loader=yaml.FullLoader)
#Set root and remote directories
def validate_path():
global root_path
global remote_path
#Assign root_dir
if 'root_dir' in cfg['directory']:
root_path = os.path.join(cfg['directory']['root_dir'], '')
else:
print('root_dir not defined in config.')
sys.exit(0)
#Assign remote_path
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
#Check to see if path exists
if not os.path.exists(remote_path):
print(f"Config Error: Path does not exist at '{os.path.abspath(remote_path)}'. Is your root_dir/remote_dir correctly defined in the config?")
sys.exit(0)
#Root_dir/remote_dir error handling
if cross_seed or tag_nohardlinks or rem_orphaned:
validate_path()
else:
if 'recyclebin' in cfg and cfg["recyclebin"] != None:
if 'enabled' in cfg["recyclebin"] and cfg["recyclebin"]['enabled']:
validate_path()
os.makedirs(os.path.join(default_dir, "logs"), exist_ok=True)
urllib3.disable_warnings()
file_name_format = os.path.join(default_dir, "logs", log_file)
msg_format = '%(asctime)s - %(levelname)s: %(message)s'
msg_format = f"[%(asctime)s] %(levelname)-10s %(message)s"
max_bytes = 1024 * 1024 * 2
backup_count = 10
@ -153,10 +141,11 @@ logger = logging.getLogger('qBit Manage')
logging.DRYRUN = 25
logging.addLevelName(logging.DRYRUN, 'DRY-RUN')
setattr(logger, 'dryrun', lambda dryrun, *args: logger._log(logging.DRYRUN, dryrun, args))
log_lev = getattr(logging, args.loglevel.upper())
log_lev = getattr(logging, log_level.upper())
logger.setLevel(log_lev)
file_handler = logging.handlers.RotatingFileHandler(filename=file_name_format,
delay=True, mode="w",
maxBytes=max_bytes,
backupCount=backup_count,
encoding="utf-8")
@ -195,10 +184,16 @@ client = Client(host=host,
password=password)
############FUNCTIONS##############
#truncate the value of the torrent url to remove sensitive information
def trunc_val(s, d, n=3):
return d.join(s.split(d, n)[:n])
#Get category from config file based on path provided
def get_category(path):
if 'cat' in cfg and cfg["cat"] != None:
cat_path = cfg["cat"]
@ -213,7 +208,7 @@ def get_category(path):
logger.warning('No categories matched. Check your config.yml file. - Setting category to NULL')
return category
#Get tags from config file based on keyword
def get_tags(urls):
if 'tags' in cfg and cfg["tags"] != None:
tag_path = cfg['tags']
@ -229,6 +224,8 @@ def get_tags(urls):
logger.warning('No tags matched. Check your config.yml file. Setting tag to NULL')
return tag
#Move files from source to destination, mod variable is to change the date modified of the file being moved
def move_files(src,dest,mod=False):
dest_path = os.path.dirname(dest)
if os.path.isdir(dest_path) == False:
@ -239,7 +236,7 @@ def move_files(src,dest,mod=False):
os.utime(dest,(modTime,modTime))
#Remove any empty directories after moving files
def remove_empty_directories(pathlib_root_dir,pattern):
# list all directories recursively and sort them by path,
# longest first
@ -255,8 +252,16 @@ def remove_empty_directories(pathlib_root_dir,pattern):
continue # catch and continue if non-empty
# Will create a 2D Dictionary with the torrent name as the key
# torrentdict = {'TorrentName1' : {'Category':'TV', 'save_path':'/data/torrents/TV', 'count':1, 'msg':'[]'},
# 'TorrentName2' : {'Category':'Movies', 'save_path':'/data/torrents/Movies'}, 'count':2, 'msg':'[]'}
# torrentdict = {'TorrentName1' : {'Category':'TV', 'save_path':'/data/torrents/TV', 'count':1, 'msg':'[]'...},
# 'TorrentName2' : {'Category':'Movies', 'save_path':'/data/torrents/Movies'}, 'count':2, 'msg':'[]'...}
# List of dictionary key definitions
# Category = Returns category of the torrent (str)
# save_path = Returns the save path of the torrent (str)
# count = Returns a count of the total number of torrents with the same name (int)
# msg = Returns a list of torrent messages by name (list of str)
# status = Returns the list of status numbers of the torrent by name (0: Tracker is disabled (used for DHT, PeX, and LSD), 1: Tracker has not been contacted yet, 2:Tracker has been contacted and is working, 3:Tracker is updating, 4:Tracker has been contacted, but it is not working (or doesn't send proper replies)
# is_complete = Returns the state of torrent (Returns True if at least one of the torrent with the State is categorized as Complete.)
# first_hash = Returns the hash number of the original torrent (Assuming the torrent list is sorted by date added (Asc))
def get_torrent_info(t_list):
torrentdict = {}
for torrent in t_list:
@ -283,44 +288,42 @@ def get_torrent_info(t_list):
return torrentdict
# Function used to recheck paused torrents sorted by size and resume torrents that are completed
def recheck():
if args.cross_seed == 'cross_seed' or args.manage == 'manage' or args.recheck == 'recheck':
def set_recheck():
if cross_seed or recheck:
#sort by size and paused
torrent_sorted_list = client.torrents.info(status_filter='paused',sort='size')
torrentdict = get_torrent_info(client.torrents.info(sort='added_on',reverse=True))
for torrent in torrent_sorted_list:
new_tag,t_url = get_tags([x.url for x in torrent.trackers if x.url.startswith('http')])
if torrent.tags == '' or ('cross-seed' in torrent.tags and len([e for e in torrent.tags.split(",") if not 'noHL' in e]) == 1): torrent.add_tags(tags=new_tag)
#Resume torrent if completed
if torrent.progress == 1:
#Check to see if torrent meets AutoTorrentManagement criteria
logger.debug(f'Rechecking Torrent to see if torrent meets AutoTorrentManagement Criteria\n'
f' - Torrent Name: {torrent.name}\n'
f' --Ratio vs Max Ratio: {torrent.ratio} < {torrent.max_ratio}\n'
f' --Seeding Time vs Max Seed Time: {datetime.timedelta(seconds=torrent.seeding_time)} < {datetime.timedelta(minutes=torrent.max_seeding_time)}')
if torrent.ratio < torrent.max_ratio and (torrent.seeding_time < (torrent.max_seeding_time * 60)):
if args.dry_run == 'dry_run':
logger.dryrun(f'\n - Not Resuming {new_tag} - {torrent.name}')
if torrent_sorted_list:
for torrent in torrent_sorted_list:
new_tag,t_url = get_tags([x.url for x in torrent.trackers if x.url.startswith('http')])
if torrent.tags == '' or ('cross-seed' in torrent.tags and len([e for e in torrent.tags.split(",") if not 'noHL' in e]) == 1): torrent.add_tags(tags=new_tag)
#Resume torrent if completed
if torrent.progress == 1:
#Check to see if torrent meets AutoTorrentManagement criteria
logger.debug(f'Rechecking Torrent to see if torrent meets AutoTorrentManagement Criteria\n'
f' - Torrent Name: {torrent.name}\n'
f' --Ratio vs Max Ratio: {torrent.ratio} < {torrent.max_ratio}\n'
f' --Seeding Time vs Max Seed Time: {datetime.timedelta(seconds=torrent.seeding_time)} < {datetime.timedelta(minutes=torrent.max_seeding_time)}')
if torrent.ratio < torrent.max_ratio and (torrent.seeding_time < (torrent.max_seeding_time * 60)):
if dry_run:
logger.dryrun(f'\n - Not Resuming {new_tag} - {torrent.name}')
else:
logger.info(f'\n - Resuming {new_tag} - {torrent.name}')
torrent.resume()
#Recheck
elif torrent.progress == 0 and torrentdict[torrent.name]['is_complete'] and not torrent.state_enum.is_checking:
if dry_run:
logger.dryrun(f'\n - Not Rechecking {new_tag} - {torrent.name}')
else:
logger.info(f'\n - Resuming {new_tag} - {torrent.name}')
torrent.resume()
#Recheck
elif torrent.progress == 0 and torrentdict[torrent.name]['is_complete'] and 'checking' not in torrent.state:
if args.dry_run == 'dry_run':
logger.dryrun(f'\n - Not Rechecking {new_tag} - {torrent.name}')
else:
logger.info(f'\n - Rechecking {new_tag} - {torrent.name}')
torrent.recheck()
logger.info(f'\n - Rechecking {new_tag} - {torrent.name}')
torrent.recheck()
# Function used to move any torrents from the cross seed directory to the correct save directory
def cross_seed():
if args.cross_seed == 'cross_seed':
def set_cross_seed():
if cross_seed:
# List of categories for all torrents moved
categories = []
# Keep track of total torrents moved
total = 0
# Used to output the final list torrents moved to output in the log
torrents_added = ''
#Track # of torrents tagged that are not cross-seeded
t_tagged = 0
# Only get torrent files
@ -328,8 +331,6 @@ def cross_seed():
dir_cs = os.path.join(cfg['directory']['cross_seed'], '')
dir_cs_out = os.path.join(dir_cs,'qbit_manage_added')
os.makedirs(dir_cs_out,exist_ok=True)
torrent_list = client.torrents.info(sort='added_on')
torrentdict = get_torrent_info(torrent_list)
for file in cs_files:
t_name = file.split(']', 2)[2].split('.torrent')[0]
# Substring Key match in dictionary (used because t_name might not match exactly with torrentdict key)
@ -343,7 +344,7 @@ def cross_seed():
src = os.path.join(dir_cs,file)
dir_cs_out = os.path.join(dir_cs,'qbit_manage_added',file)
categories.append(category)
if args.dry_run == 'dry_run':
if dry_run:
logger.dryrun(f'Not Adding {t_name} to qBittorrent with: '
f'\n - Category: {category}'
f'\n - Save_Path: {dest}'
@ -363,7 +364,7 @@ def cross_seed():
else:
logger.info(f'Found {t_name} in {dir_cs} but original torrent is not complete. Not adding to qBittorrent')
else:
if args.dry_run == 'dry_run':
if dry_run:
logger.dryrun(f'{t_name} not found in torrents.')
else:
logger.warning(f'{t_name} not found in torrents.')
@ -373,40 +374,36 @@ def cross_seed():
t_name = torrent.name
if 'cross-seed' not in torrent.tags and torrentdict[t_name]['count'] > 1 and torrentdict[t_name]['first_hash'] != torrent.hash:
t_tagged += 1
if args.dry_run == 'dry_run':
if dry_run:
logger.dryrun(f'Not Adding cross-seed tag to {t_name}')
else:
logger.info(f'Adding cross-seed tag to {t_name}')
torrent.add_tags(tags='cross-seed')
if args.dry_run == 'dry_run':
if dry_run:
for c in numcategory:
total += numcategory[c]
torrents_added += f'\n - {c} .torrents not added: {numcategory[c]}'
torrents_added += f'\n -- Total .torrents not added: {total}'
torrents_added += f'\n -- Total .torrents not tagged: {t_tagged}'
logger.dryrun(torrents_added)
if numcategory[c] > 0: logger.dryrun(f'{numcategory[c]} {c} cross-seed .torrents not added.')
if total > 0: logger.dryrun(f'Total {total} cross-seed .torrents not added.')
if t_tagged > 0:logger.dryrun(f'Total {t_tagged} cross-seed .torrents not tagged.')
else:
for c in numcategory:
total += numcategory[c]
torrents_added += f'\n - {c} .torrents added: {numcategory[c]}'
torrents_added += f'\n -- Total .torrents added: {total}'
torrents_added += f'\n -- Total .torrents tagged: {t_tagged}'
logger.info(torrents_added)
if numcategory[c] > 0: logger.info(f'{numcategory[c]} {c} cross-seed .torrents not added.')
if total > 0: logger.info(f'Total {total} cross-seed .torrents not added.')
if t_tagged > 0:logger.info(f'Total {t_tagged} cross-seed .torrents not tagged.')
def update_category():
if args.manage == 'manage' or args.cat_update == 'cat_update':
def set_category():
if cat_update:
num_cat = 0
torrent_list = client.torrents.info(sort='added_on',reverse=True)
for torrent in torrent_list:
if torrent.category == '':
for x in torrent.trackers:
if x.url.startswith('http'):
t_url = trunc_val(x.url, '/')
new_cat = get_category(torrent.save_path)
if args.dry_run == 'dry_run':
if dry_run:
logger.dryrun(f'\n - Torrent Name: {torrent.name}'
f'\n - New Category: {new_cat}'
f'\n - Tracker: {t_url}')
@ -417,7 +414,7 @@ def update_category():
f'\n - Tracker: {t_url}')
torrent.set_category(category=new_cat)
num_cat += 1
if args.dry_run == 'dry_run':
if dry_run:
if num_cat >= 1:
logger.dryrun(f'Did not update {num_cat} new categories.')
else:
@ -429,14 +426,13 @@ def update_category():
logger.info(f'No new torrents to categorize.')
def update_tags():
if args.manage == 'manage' or args.tag_update == 'tag_update':
def set_tags():
if tag_update:
num_tags = 0
torrent_list = client.torrents.info(sort='added_on',reverse=True)
for torrent in torrent_list:
if torrent.tags == '' or ('cross-seed' in torrent.tags and len([e for e in torrent.tags.split(",") if not 'noHL' in e]) == 1):
new_tag,t_url = get_tags([x.url for x in torrent.trackers if x.url.startswith('http')])
if args.dry_run == 'dry_run':
if dry_run:
logger.dryrun(f'\n - Torrent Name: {torrent.name}'
f'\n - New Tag: {new_tag}'
f'\n - Tracker: {t_url}')
@ -447,7 +443,7 @@ def update_tags():
f'\n - Tracker: {t_url}')
torrent.add_tags(tags=new_tag)
num_tags += 1
if args.dry_run == 'dry_run':
if dry_run:
if num_tags >= 1:
logger.dryrun(f'Did not update {num_tags} new tags.')
else:
@ -459,10 +455,8 @@ def update_tags():
logger.info('No new torrents to tag. ')
def rem_unregistered():
if args.manage == 'manage' or args.rem_unregistered == 'rem_unregistered':
torrent_list = client.torrents.info(sort='added_on',reverse=True)
torrentdict = get_torrent_info(torrent_list)
def set_rem_unregistered():
if rem_unregistered:
rem_unr = 0
del_tor = 0
pot_unr = ''
@ -496,13 +490,14 @@ def rem_unregistered():
'REDOWNLOAD' in msg_up or \
'PACKS' in msg_up or \
'REPACKED' in msg_up or \
'PACK' in msg_up \
'PACK' in msg_up or \
'TRUMP' in msg_up
) and x.status == 4 and 'DOWN' not in msg_up and 'UNREACHABLE' not in msg_up:
logger.debug(f'Torrent counts: {t_count}')
logger.debug(f'msg: {t_msg}')
logger.debug(f'status: {t_status}')
if t_count > 1:
if args.dry_run == 'dry_run':
if dry_run:
if '' in t_msg:
logger.dryrun(n_info)
rem_unr += 1
@ -520,17 +515,17 @@ def rem_unregistered():
tor_delete_recycle(torrent)
del_tor += 1
else:
if args.dry_run == 'dry_run':
if dry_run:
logger.dryrun(n_d_info)
del_tor += 1
else:
logger.info(n_d_info)
tor_delete_recycle(torrent)
del_tor += 1
if args.dry_run == 'dry_run':
if dry_run:
if rem_unr >= 1 or del_tor >= 1:
logger.dryrun(f'Did not delete {rem_unr} .torrents(s) or content files.')
logger.dryrun(f'Did not delete {del_tor} .torrents(s) or content files.')
logger.dryrun(f'Did not delete {rem_unr} .torrents(s) but not content files.')
logger.dryrun(f'Did not delete {del_tor} .torrents(s) AND content files.')
else:
logger.dryrun('No unregistered torrents found.')
else:
@ -542,24 +537,15 @@ def rem_unregistered():
if (len(pot_unr) > 0):
logger.debug(f'Potential Unregistered torrents: {pot_unr}')
def rem_orphaned():
if args.rem_orphaned == 'rem_orphaned':
torrent_list = client.torrents.info()
def set_rem_orphaned():
if rem_orphaned:
torrent_files = []
root_files = []
orphaned_files = []
if 'root_dir' in cfg['directory']:
root_path = os.path.join(cfg['directory']['root_dir'], '')
else:
logger.error('root_dir not defined in config.')
return
if ('remote_dir' in cfg['directory'] and cfg['directory']['remote_dir'] != ''):
remote_path = os.path.join(cfg['directory']['remote_dir'], '')
if (remote_path != root_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 and os.path.join(remote_path,'.RecycleBin') not in path]
else:
remote_path = root_path
root_files = [os.path.join(path, name) for path, subdirs, files in os.walk(root_path) for name in files if os.path.join(root_path,'orphaned_data') not in path and os.path.join(root_path,'.RecycleBin') not in path]
for torrent in torrent_list:
@ -576,7 +562,7 @@ def rem_orphaned():
logger.debug("\n".join(orphaned_files))
logger.debug('----------Deleting orphan files-----------')
if (orphaned_files):
if args.dry_run == 'dry_run':
if dry_run:
dir_out = os.path.join(remote_path,'orphaned_data')
logger.dryrun(f'\n----------{len(orphaned_files)} Orphan files found-----------'
f'\n - '+'\n - '.join(orphaned_files)+
@ -596,14 +582,14 @@ def rem_orphaned():
logger.info(f'Cleaning up any empty directories...')
remove_empty_directories(Path(remote_path),"**/*/*")
else:
if args.dry_run == 'dry_run':
if dry_run:
logger.dryrun('No Orphaned Files found.')
else:
logger.info('No Orphaned Files found.')
def tag_nohardlinks():
if args.tag_nohardlinks == 'tag_nohardlinks':
def set_tag_nohardlinks():
if tag_nohardlinks:
nohardlinks = cfg['nohardlinks']
n_info = ''
t_count = 0 #counter for the number of torrents that has no hard links
@ -612,15 +598,6 @@ def tag_nohardlinks():
tdel_tags = 0 #counter for number of torrents that previously had no hard links but now have hard links
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
if 'root_dir' in cfg['directory']:
root_path = os.path.join(cfg['directory']['root_dir'], '')
else:
logger.error('root_dir not defined in config.')
return
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:
torrent_list = client.torrents.info(category=category,filter='completed')
@ -635,7 +612,7 @@ def tag_nohardlinks():
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':
if not dry_run:
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
@ -664,7 +641,7 @@ def tag_nohardlinks():
else:
seeding_time_limit = -2
ratio_limit = -2
if args.dry_run != 'dry_run':
if not 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)
@ -677,7 +654,7 @@ def tag_nohardlinks():
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':
if 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.')
@ -688,7 +665,7 @@ def tag_nohardlinks():
n_info += (' Removing tags noHL.')
n_info += (' Removing ratio and seeding time limits.')
tdel_tags += 1
if args.dry_run != 'dry_run':
if not dry_run:
#Remove tags and share limits
torrent.remove_tags(tags='noHL')
client.torrents_set_share_limits(-2,-2,torrent.hash)
@ -701,13 +678,13 @@ def tag_nohardlinks():
#Double check that the content path is the same before we delete anything
if torrent['content_path'].replace(root_path,remote_path) == tdel_dict[torrent.name]:
t_del_cs += 1
if args.dry_run != 'dry_run':
if not dry_run:
if (os.path.exists(torrent['content_path'].replace(root_path,remote_path))):
tor_delete_recycle(torrent)
else:
torrent.delete(hash=torrent.hash, delete_files=False)
if args.dry_run == 'dry_run':
if 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)')
@ -747,17 +724,7 @@ def nohardlink(file):
def tor_delete_recycle(torrent):
if 'recyclebin' in cfg and cfg["recyclebin"] != None:
if 'enabled' in cfg["recyclebin"] and cfg["recyclebin"]['enabled']:
tor_files = []
if 'root_dir' in cfg['directory']:
root_path = os.path.join(cfg['directory']['root_dir'], '')
else:
logger.error('root_dir not defined in config.')
return
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
tor_files = []
#Define torrent files/folders
for file in torrent.files:
tor_files.append(os.path.join(torrent.save_path,file.name))
@ -787,8 +754,8 @@ def tor_delete_recycle(torrent):
def empty_recycle():
if args.manage == 'manage' or args.empty_recycle == 'empty_recycle':
def set_empty_recycle():
if not skip_recycle:
num_del = 0
n_info = ''
if 'recyclebin' in cfg and cfg["recyclebin"] != None:
@ -817,13 +784,13 @@ def empty_recycle():
days = (now - last_modified) / (60 * 60 * 24)
if (empty_after_x_days <= days):
num_del += 1
if args.dry_run == 'dry_run':
if dry_run:
n_info += (f'Did not delete {filename} from the recycle bin. (Last modified {round(days)} days ago).\n')
else:
n_info += (f'Deleted {filename} from the recycle bin. (Last modified {round(days)} days ago).\n')
os.remove(file)
if num_del > 0:
if args.dry_run == 'dry_run':
if dry_run:
logger.dryrun(n_info)
logger.dryrun(f'Did not delete {num_del} files from the Recycle Bin.')
else:
@ -838,17 +805,28 @@ def empty_recycle():
else:
logger.error('recyclebin not defined in config.')
return
#Define global parameters
torrent_list = None
torrentdict = None
def start():
update_category()
update_tags()
rem_unregistered()
cross_seed()
recheck()
rem_orphaned()
tag_nohardlinks()
empty_recycle()
#Global parameters to get the torrent dictionary
global torrent_list
global torrentdict
#Get an updated list of torrents
torrent_list = client.torrents.info(sort='added_on')
if recheck or cross_seed or rem_unregistered:
#Get an updated torrent dictionary information of the torrents
torrentdict = get_torrent_info(torrent_list)
set_category()
set_tags()
set_rem_unregistered()
set_cross_seed()
set_recheck()
set_rem_orphaned()
set_tag_nohardlinks()
set_empty_recycle()
if __name__ == '__main__':
logger.info(" _ _ _ ")