Skip to content

Commit

Permalink
fix(qbtools): utils package and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
buroa committed Oct 2, 2024
1 parent 52b09ee commit 25ee3d7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion qbtools/commands/orphaned.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import shutil

from fnmatch import fnmatch


Expand Down Expand Up @@ -103,4 +104,3 @@ def add_arguments(subparser):
default=False,
required=False,
)
return parser
5 changes: 2 additions & 3 deletions qbtools/commands/prune.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import qbtools
from qbtools import utils
from fnmatch import fnmatch


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -114,4 +114,3 @@ def add_arguments(subparser):
default=False,
required=False,
)
return parser
3 changes: 2 additions & 1 deletion qbtools/commands/reannounce.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import time

from qbtools import utils
from qbittorrentapi import TrackerStatus


Expand Down Expand Up @@ -65,4 +67,3 @@ def add_arguments(subparser):
action="store_true",
help="Include seeding torrents for reannouncements.",
)
return parser
13 changes: 7 additions & 6 deletions qbtools/commands/tagging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import qbtools
import tldextract
import collections

from qbtools import utils
from datetime import datetime
from qbittorrentapi import TrackerStatus

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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:
Expand Down Expand Up @@ -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]
)
Expand All @@ -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",
Expand Down Expand Up @@ -298,4 +300,3 @@ def add_arguments(subparser):
action="store_true",
help="Tag torrents with unregistered tracker status message",
)
return parser
18 changes: 11 additions & 7 deletions qbtools/qbtools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import os
import utils
import importlib
import qbittorrentapi
import argparse
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 25ee3d7

Please sign in to comment.