From 25ee3d7c661e6523657cf006898a4c66595d397a Mon Sep 17 00:00:00 2001 From: Steven Kreitzer Date: Wed, 2 Oct 2024 14:57:26 -0500 Subject: [PATCH] fix(qbtools): utils package and refactoring --- qbtools/commands/orphaned.py | 2 +- qbtools/commands/prune.py | 5 ++--- qbtools/commands/reannounce.py | 3 ++- qbtools/commands/tagging.py | 13 +++++++------ qbtools/qbtools.py | 18 +++++++++++------- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/qbtools/commands/orphaned.py b/qbtools/commands/orphaned.py index 3f15fe8..f41d99c 100644 --- a/qbtools/commands/orphaned.py +++ b/qbtools/commands/orphaned.py @@ -1,5 +1,6 @@ import os import shutil + from fnmatch import fnmatch @@ -103,4 +104,3 @@ def add_arguments(subparser): default=False, required=False, ) - return parser diff --git a/qbtools/commands/prune.py b/qbtools/commands/prune.py index 16837f3..bbdd0ac 100644 --- a/qbtools/commands/prune.py +++ b/qbtools/commands/prune.py @@ -1,4 +1,4 @@ -import qbtools +from qbtools import utils from fnmatch import fnmatch @@ -45,7 +45,7 @@ def __init__(app, logger): for t in filtered_torrents: logger.info( - f"Pruned torrent {t['name']} with category [{t.category}] and tags [{t.tags}] and ratio [{round(t['ratio'], 2)}] and seeding time [{qbtools.utils.dhms(t['seeding_time'])}]" + f"Pruned torrent {t['name']} with category [{t.category}] and tags [{t.tags}] and ratio [{round(t['ratio'], 2)}] and seeding time [{utils.dhms(t['seeding_time'])}]" ) if not app.dry_run: t.delete(delete_files=app.with_data) @@ -114,4 +114,3 @@ def add_arguments(subparser): default=False, required=False, ) - return parser diff --git a/qbtools/commands/reannounce.py b/qbtools/commands/reannounce.py index 0151829..4d037be 100755 --- a/qbtools/commands/reannounce.py +++ b/qbtools/commands/reannounce.py @@ -1,4 +1,6 @@ import time + +from qbtools import utils from qbittorrentapi import TrackerStatus @@ -65,4 +67,3 @@ def add_arguments(subparser): action="store_true", help="Include seeding torrents for reannouncements.", ) - return parser diff --git a/qbtools/commands/tagging.py b/qbtools/commands/tagging.py index 9b98ee6..02cf4a4 100755 --- a/qbtools/commands/tagging.py +++ b/qbtools/commands/tagging.py @@ -1,6 +1,7 @@ -import qbtools import tldextract import collections + +from qbtools import utils from datetime import datetime from qbittorrentapi import TrackerStatus @@ -88,7 +89,7 @@ def __init__(app, logger): domain = extractTLD( sorted(filtered_trackers, key=lambda x: x.url)[0].url ).registered_domain - tracker = qbtools.utils.filter_tracker_by_domain(domain, trackers) + tracker = utils.filter_tracker_by_domain(domain, trackers) if app.added_on: added_on = datetime.fromtimestamp(t.added_on) @@ -149,7 +150,7 @@ def __init__(app, logger): tags_to_add.append("expired") elif tracker[ "required_seed_days" - ] != 0 and t.seeding_time >= qbtools.utils.seconds( + ] != 0 and t.seeding_time >= utils.seconds( tracker["required_seed_days"] ): tags_to_add.append("expired") @@ -168,7 +169,7 @@ def __init__(app, logger): content_paths.append((t.hash, t.content_path, t.size)) - if app.not_linked and not qbtools.utils.is_linked(t.content_path): + if app.not_linked and not utils.is_linked(t.content_path): tags_to_add.append("not-linked") for tag in tags_to_add: @@ -206,7 +207,7 @@ def __init__(app, logger): # Apply tags for tag in tag_hashes: if app.size: - size = qbtools.utils.format_bytes(tag_sizes[tag]) + size = utils.format_bytes(tag_sizes[tag]) app.client.torrents_add_tags( tags=f"{tag} [{size}]", torrent_hashes=tag_hashes[tag] ) @@ -228,6 +229,7 @@ def add_arguments(subparser): # Tag torrents qbtools.py tagging --exclude-category manual --added-on --expired --last-activity --sites --unregistered """ + print(__name__) parser = subparser.add_parser("tagging") parser.add_argument( "--exclude-category", @@ -298,4 +300,3 @@ def add_arguments(subparser): action="store_true", help="Tag torrents with unregistered tracker status message", ) - return parser diff --git a/qbtools/qbtools.py b/qbtools/qbtools.py index ca65261..b7e9949 100755 --- a/qbtools/qbtools.py +++ b/qbtools/qbtools.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import os +import utils import importlib import qbittorrentapi import argparse @@ -38,21 +39,24 @@ def add_default_args(parser): def load_commands(subparsers): - for cmd in os.listdir(f"{os.path.dirname(__file__)}/commands"): - if cmd.startswith("__") or not cmd.endswith(".py"): - continue - - name = cmd[:-3] + def load_command(name): try: mod = importlib.import_module(f"commands.{name}") - parser = mod.add_arguments(subparsers) - add_default_args(parser) + mod.add_arguments(subparsers) + subparser = subparsers.choices.get(name) + if subparser: + add_default_args(subparser) except ImportError: logger.error(f"Error loading module: {name}", exc_info=True) sys.exit(1) else: globals()[name] = mod + for cmd in os.listdir(f"{os.path.dirname(__file__)}/commands"): + if cmd.startswith("__") or not cmd.endswith(".py"): + continue + load_command(cmd[:-3]) + def qbit_client(app): client = qbittorrentapi.Client(