Skip to content

Commit

Permalink
chore(qbtools): refactor (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
buroa authored Oct 8, 2024
1 parent c2c0757 commit 929fb1c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 49 deletions.
4 changes: 2 additions & 2 deletions qbtools/commands/orphaned.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def __init__(app, logger):
logger.info(f"Checking for orphaned files on disk not in qBittorrent...")
logger.info("Checking for orphaned files on disk not in qBittorrent...")

completed_dir = app.client.application.preferences.save_path
categories = [
Expand Down Expand Up @@ -57,7 +57,7 @@ def cleanup_dir(folder_path, owned_files):
owned_subfiles = set(
filter(lambda x: x.startswith(item_path), owned_files)
)
if len(owned_subfiles) == 0 and item_path not in categories:
if not owned_subfiles and item_path not in categories:
delete(item_path)
else:
cleanup_dir(item_path, owned_subfiles)
Expand Down
18 changes: 9 additions & 9 deletions qbtools/commands/prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@ def __init__(app, logger):

if not categories:
logger.info(
f"No torrents can be pruned since no categories were included based on selectors"
"No torrents can be pruned since no categories were included based on selectors"
)

torrents = app.client.torrents.info()
torrents = list(
filter(lambda x: x.category in categories, torrents)
)
torrents = list(filter(lambda x: x.category in categories, torrents))

include_tags = [i for s in app.include_tag for i in s]
if include_tags:
torrents = list(
filter(lambda x: all(y in x.tags for y in include_tags), torrents)
)

exclude_tags = [i for s in app.exclude_tag for i in s]
if exclude_tags:
torrents = list(
filter(
lambda x: not any(y in x.tags for y in exclude_tags), torrents
)
filter(lambda x: not any(y in x.tags for y in exclude_tags), torrents)
)

logger.info(
f"Pruning torrents with tags [{' AND '.join(include_tags)}] but does not contain tags [{' OR '.join(exclude_tags)}]..."
f"Pruning torrents with tags [{' AND '.join(include_tags)}] "
f"but does not contain tags [{' OR '.join(exclude_tags)}]..."
)

for t in 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 [{utils.dhms(t['seeding_time'])}]"
f"Pruned torrent {t['name']} with category [{t.category}] "
f"and tags [{t.tags}] and ratio [{round(t['ratio'], 2)}] "
f"and seeding time [{utils.dhms(t['seeding_time'])}]"
)
if not app.dry_run:
t.delete(delete_files=app.with_data)
Expand Down
16 changes: 3 additions & 13 deletions qbtools/commands/reannounce.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,21 @@ def process_torrents(status):
peers = t.num_seeds + t.num_leechs
if peers:
logger.debug(
"Torrent %s (%s) has %d peer(s) - not reannouncing",
t.name,
t.hash,
peers,
f"Torrent {t.name} ({t.hash}) has {peers} peer(s) - not reannouncing"
)
continue

torrent_retries = torrents_retries.get(t.hash, 0)
if torrent_retries >= max_tries:
logger.debug(
"Torrent %s (%s) has reached %s reannounce tries - not reannouncing",
t.name,
t.hash,
retries,
f"Torrent {t.name} ({t.hash}) has reached {retries} reannounce tries - not reannouncing",
)
continue

t.reannounce()
torrents_retries[t.hash] = torrent_retries + 1
logger.info(
"Reannounced torrent %s (%s) %s/%s",
t.name,
t.hash,
torrent_retries,
max_tries,
f"Reannounced torrent {t.name} ({t.hash}) {torrent_retries}/{max_tries}",
)

retries[status] = torrents_retries
Expand Down
47 changes: 22 additions & 25 deletions qbtools/commands/tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,13 @@


def __init__(app, logger):
logger.info(f"Tagging torrents in qBittorrent...")

tags = collections.defaultdict(list)
content_paths = []
logger.info("Tagging torrents in qBittorrent...")

today = datetime.today()
extractTLD = tldextract.TLDExtract(cache_dir=None)

torrents = app.client.torrents.info()
trackers = app.config.get("trackers", [])
trackers = {y: x for x in trackers for y in x["urls"]}

unregistered_matches = app.config.get("unregistered_matches", UNREGISTERED_MATCHES)
maintenance_matches = app.config.get("maintenance_matches", MAINTENANCE_MATCHES)

exclude_categories = [i for s in app.exclude_category for i in s]
if exclude_categories:
torrents = list(
Expand All @@ -70,20 +63,20 @@ def __init__(app, logger):
exclude_tags = [i for s in app.exclude_tag for i in s]
if exclude_tags:
torrents = list(
filter(
lambda x: any(y not in x.tags for y in exclude_tags), torrents
)
filter(lambda x: any(y not in x.tags for y in exclude_tags), torrents)
)

torrents = app.client.torrents.info()
extractTLD = tldextract.TLDExtract(cache_dir=None)
tags = collections.defaultdict(list)
paths = []

for t in torrents:
tags_to_add = []
filtered = []

url = t.tracker
if not url:
filtered = [s for s in t.trackers if s.tier >= 0] # Expensive
filtered = [s for s in t.trackers if s.tier >= 0] # Expensive
if not filtered:
continue
url = filtered[0].url
Expand All @@ -92,7 +85,7 @@ def __init__(app, logger):

if app.added_on:
tags_to_add.append(calculate_date_tags("added", t.added_on, today))

if app.last_activity:
tags_to_add.append(calculate_date_tags("activity", t.last_activity, today))

Expand All @@ -103,33 +96,34 @@ def __init__(app, logger):
tags_to_add.append(f"site:unmapped")

if (app.unregistered or app.tracker_down or app.not_working) and filtered:
tracker_messages = [z.msg.upper() for z in filtered]
messages = [z.msg.upper() for z in filtered]
if app.unregistered and any(
x in msg for msg in tracker_messages for x in unregistered_matches
match in msg for msg in messages for match in UNREGISTERED_MATCHES
):
tags_to_add.append("unregistered")
elif app.tracker_down and any(
x in msg for msg in tracker_messages for x in maintenance_matches
match in msg for msg in messages for match in MAINTENANCE_MATCHES
):
tags_to_add.append("tracker-down")
elif app.not_working:
tags_to_add.append("not-working")

if app.expired and tracker and t.state_enum.is_complete:
if (
tracker["required_seed_ratio"] != 0
tracker["required_seed_ratio"]
and t.ratio >= tracker["required_seed_ratio"]
):
tags_to_add.append("expired")
elif tracker["required_seed_days"] != 0 and t.seeding_time >= utils.seconds(
elif tracker["required_seed_days"] and t.seeding_time >= utils.seconds(
tracker["required_seed_days"]
):
tags_to_add.append("expired")

if app.duplicates:
if t.content_path in content_paths and not t.content_path == t.save_path:
if t.content_path in paths and not t.content_path == t.save_path:
tags_to_add.append("dupe")
content_paths.append(t.content_path)
else:
paths.append(t.content_path)

if app.not_linked and not utils.is_linked(t.content_path):
tags_to_add.append("not-linked")
Expand All @@ -139,8 +133,9 @@ def __init__(app, logger):

empty_tags = list(
filter(
lambda tag: tag not in tags and any(tag.lower().startswith(x.lower()) for x in DEFAULT_TAGS),
app.client.torrents_tags()
lambda tag: tag not in tags
and any(tag.lower().startswith(x.lower()) for x in DEFAULT_TAGS),
app.client.torrents_tags(),
)
)
if empty_tags:
Expand Down Expand Up @@ -235,7 +230,9 @@ def add_arguments(command, subparser):
help="Tag torrents with not working tracker status",
)
parser.add_argument(
"--sites", action="store_true", help="Tag torrents with site names (defined in config.yaml)"
"--sites",
action="store_true",
help="Tag torrents with site names (defined in config.yaml)",
)
parser.add_argument(
"--tracker-down",
Expand Down

0 comments on commit 929fb1c

Please sign in to comment.