From c371b1cd38eae5adccbcbb849356a30a917eb6b7 Mon Sep 17 00:00:00 2001 From: Visorask <54461452+Visorask@users.noreply.github.com> Date: Sat, 20 Feb 2021 21:45:09 -0600 Subject: [PATCH] Big Update --- config.yml | 7 +- qbit_manage.py | 179 +++++++++++++++++++++++++++++++++++++++++------ requirements.txt | 5 ++ 3 files changed, 169 insertions(+), 22 deletions(-) create mode 100644 requirements.txt diff --git a/config.yml b/config.yml index 54f8c23..1a82f2e 100644 --- a/config.yml +++ b/config.yml @@ -1,12 +1,17 @@ +# qBittorrent parameters qbt: host: 'localhost:8080' user: 'username' pass: 'password' + +# Category Parameters cat: # : anime: anime movies: movies tv: tv + +# Tag Parameters tags: # : awesome-hd: AHD @@ -14,4 +19,4 @@ tags: animebytes.tv: AnimeBytes avistaz: Avistaz landof.tv: BroadcasTheNet - passthepopcorn: PassThePopcorn \ No newline at end of file + passthepopcorn: PassThePopcorn diff --git a/qbit_manage.py b/qbit_manage.py index c9ab279..8399981 100644 --- a/qbit_manage.py +++ b/qbit_manage.py @@ -3,27 +3,85 @@ from qbittorrentapi import Client import yaml import argparse +import logging +import sys +# import apprise parser = argparse.ArgumentParser("qBittorrent Manager.", description='A mix of scripts combined for managing qBittorrent.') -parser.add_argument('-c', '--config-file', dest='config', action='store', default='config.yml', +parser.add_argument('-c', '--config-file', + dest='config', + action='store', + default='config.yml', help='This is used if you want to use different names for your config.yml. Example: tv.yml') -parser.add_argument('-m', '--manage', dest='command', action='store_const', const='manage', - help='Use this if you would like to update your tags AND categories and remove unregistered ' - 'torrents.') -parser.add_argument('--cat-update', dest='command', action='store_const', const='cat-update', +parser.add_argument('-l', '--log-file', + dest='logfile', + action='store', + default='activity.log', + help='This is used if you want to use different names for your config.yml. Example: tv.yml') +parser.add_argument('-m', '--manage', + dest='manage', + action='store_const', + const='manage', + help='Use this if you would like to update your tags AND' + ' categories AND remove unregistered torrents.') +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('--tag-update', dest='command', action='store_const', const='tag-update', +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.') -parser.add_argument('--rem-unregistered', dest='command', action='store_const', const='rem-unregistered', +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('--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 delete or ' + 'tag/categorize anything.') +parser.add_argument('--log', + dest='loglevel', + action='store', + default='INFO', + help='Change your log level. ') args = parser.parse_args() + with open(args.config, "r") as cfg_file: cfg = yaml.load(cfg_file, Loader=yaml.FullLoader) + +# Logging +log_lev = getattr(logging, args.loglevel.upper()) +file_handler = logging.FileHandler(filename=args.logfile) +stdout_handler = logging.StreamHandler(sys.stdout) +handlers = [file_handler, stdout_handler] + +# noinspection PyArgumentList +logging.basicConfig(level=log_lev, + format='%(asctime)s - %(levelname)s - %(message)s', + handlers=handlers) +logger = logging.getLogger('qBit Manage') + + # Actual API call to connect to qbt. +<<<<<<< HEAD +client = Client(host=cfg["qbt"]["host"], + username=cfg["qbt"]["user"], + password=cfg["qbt"]["pass"]) +======= client = Client(host=cfg["qbt"]["host"], username=cfg["qbt"]["user"], password=cfg["qbt"]["pass"]) +<<<<<<< Updated upstream +======= +>>>>>>> e45a3f1064b6e19c352a0230194ff0293d13f548 +>>>>>>> Stashed changes torrent_list = client.torrents.info() @@ -38,6 +96,8 @@ def get_category(path): return category +<<<<<<< HEAD +======= def update_category(): num_cat = 0 for torrent in torrent_list: @@ -53,6 +113,7 @@ def update_category(): print('No new torrents to categorize.') +>>>>>>> e45a3f1064b6e19c352a0230194ff0293d13f548 def get_tags(url): tag_path = cfg["tags"] for t, n in tag_path.items(): @@ -64,7 +125,93 @@ def get_tags(url): return tag +def update_category(): + if args.manage == 'manage' or args.cat_update == 'cat_update': + num_cat = 0 + for torrent in torrent_list: + if torrent.category == '': + new_cat = get_category(torrent.save_path) + if args.dry_run == 'dry_run': + logger.info('DRY-RUN: Torrent Name: %s', torrent.name) + logger.info('DRY-RUN: - New Category: %s', new_cat) + num_cat += 1 + else: + logger.info('Torrent Name: %s', torrent.name) + logger.info(' - New Category: %s', new_cat) + torrent.set_category(category=new_cat) + num_cat += 1 + if args.dry_run == 'dry_run': + if num_cat >= 1: + logger.info('DRY-RUN: Did not update %s new categories.', num_cat) + else: + logger.info('DRY-RUN: No new torrents to categorize.') + else: + if num_cat >= 1: + logger.info('Updated %s new categories.', num_cat) + else: + logger.info('No new torrents to categorize.') + + def update_tags(): +<<<<<<< Updated upstream +======= +<<<<<<< HEAD + if args.manage == 'manage' or args.tag_update == 'tag_update': + num_tags = 0 + for torrent in torrent_list: + if torrent.tags == '': + for x in torrent.trackers: + if x.url.startswith('http'): + new_tag = get_tags(x.url) + if args.dry_run == 'dry_run': + logger.info('DRY-RUN: Torrent Name: %s', torrent.name) + logger.info('DRY-RUN: - New Tag: %s', new_tag) + num_tags += 1 + else: + logger.info('Torrent Name: %s', torrent.name) + logger.info(' - New Tag: %s', new_tag) + torrent.add_tags(tags=new_tag) + num_tags += 1 + if args.dry_run == 'dry_run': + if num_tags >= 1: + logger.info('DRY-RUN: Did not update %s new tags.', num_tags) + else: + logger.info('DRY-RUN: No new torrents to tag.') + else: + if num_tags >= 1: + logger.info('Updated %s new tags.', num_tags) + else: + logger.info('No new torrents to tag.') + + +def rem_unregistered(): + if args.manage == 'manage' or args.rem_unregistered == 'rem_unregistered': + rem_unr = 0 + for torrent in torrent_list: + for status in torrent.trackers: + if 'Unregistered torrent' in status.msg: + if args.dry_run == 'dry_run': + logger.info('DRY-RUN: %s -> %s', torrent.name, status.msg) + logger.info('DRY-RUN: - NOT Deleted') + rem_unr += 1 + else: + logger.info('%s -> %s', torrent.name, status.msg) + logger.info(' - Deleted') + torrent.delete(hash=torrent.hash, + delete_files=True) + rem_unr += 1 + if args.dry_run == 'dry_run': + if rem_unr >= 1: + logger.info('DRY-RUN: Did not delete %s torrents.', rem_unr) + else: + logger.info('DRY-RUN: No unregistered torrents found.') + else: + if rem_unr >= 1: + logger.info('Deleted %s torrents.', rem_unr) + else: + logger.info('No unregistered torrents found.') +======= +>>>>>>> Stashed changes num_tags = 0 for torrent in torrent_list: if torrent.tags == '': @@ -94,23 +241,13 @@ def rem_unregistered(): print('Deleted ', rem_unr, 'torrents.') else: print('No unregistered torrents found.') +>>>>>>> e45a3f1064b6e19c352a0230194ff0293d13f548 def main(): - if args.command == 'manage': - update_category() - update_tags() - rem_unregistered() - elif args.command == 'tag-update': - update_tags() - elif args.command == 'cat-update': - update_category() - elif args.command == 'rem-unregistered': - rem_unregistered() - else: - print("The script requires an argument. See below:") - print("") - parser.print_help() + update_category() + update_tags() + rem_unregistered() if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ddd452a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +PyYAML +qbittorrent-api + + +