Skip to content

Commit

Permalink
chore: clean up reannounce and tagging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
buroa committed Aug 30, 2024
1 parent 5fcde29 commit c15f78f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
10 changes: 5 additions & 5 deletions qbtools/commands/prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ def __init__(args, logger):
client = qbtools.qbit_client(args)

categories = list(client.torrent_categories.categories.keys())
if len(args.include_category) > 0:
if args.include_category:
includes = [i for s in args.include_category for i in s]
categories = list(
filter(lambda c: any(fnmatch(c, p) for p in includes), categories)
)
if len(args.exclude_category) > 0:
if args.exclude_category:
excludes = [i for s in args.exclude_category for i in s]
categories = list(
filter(lambda c: not any(fnmatch(c, p) for p in excludes), categories)
)

if len(categories) == 0:
if not categories:
logger.info(
f"No torrents can be pruned since no categories were included based on selectors"
)
Expand All @@ -29,12 +29,12 @@ def __init__(args, logger):
)

include_tags = [i for s in args.include_tag for i in s]
if len(include_tags):
if include_tags:
filtered_torrents = list(
filter(lambda x: all(y in x.tags for y in include_tags), filtered_torrents)
)
exclude_tags = [i for s in args.exclude_tag for i in s]
if len(exclude_tags):
if exclude_tags:
filtered_torrents = list(
filter(
lambda x: not any(y in x.tags for y in exclude_tags), filtered_torrents
Expand Down
12 changes: 6 additions & 6 deletions qbtools/commands/reannounce.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ def process_torrents(status):
torrents_retries.clear()

for t in torrents:
invalid_trackers = list(filter(lambda s: s.status == 4, t.trackers))
if not invalid_trackers:
not_working = list(filter(lambda s: s.status == 4, t.trackers))
if not not_working:
continue

peers = t.num_seeds + t.num_leechs
if peers > 0:
logger.debug("Torrent %s already has %d peer(s) - not reannouncing", t.name, peers)
if peers:
logger.debug("Torrent %s (%s) has %d peer(s) - not reannouncing", t.name, t.hash, peers)
continue

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

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

Expand Down
9 changes: 3 additions & 6 deletions qbtools/commands/tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@

MAINTENANCE_MATCHES = ["tracker is down", "maintenance"]

DHT_MATCHES = ["** [DHT] **", "** [PeX] **", "** [LSD] **"]


def __init__(args, logger):
logger.info(f"Tagging torrents in qBittorrent...")
Expand All @@ -62,11 +60,11 @@ def __init__(args, logger):
filtered_torrents = client.torrents.info()
exclude_categories = [i for s in args.exclude_category for i in s]
exclude_tags = [i for s in args.exclude_tag for i in s]
if len(exclude_categories):
if exclude_categories:
filtered_torrents = list(
filter(lambda x: x.category not in exclude_categories, filtered_torrents)
)
if len(exclude_tags):
if exclude_tags:
filtered_torrents = list(
filter(
lambda x: any(y not in x.tags for y in exclude_tags), filtered_torrents
Expand All @@ -77,8 +75,7 @@ def __init__(args, logger):
for t in filtered_torrents:
tags_to_add = []

# TODO: Optimize - this slows down the script a lot
filtered_trackers = list(filter(lambda s: not s.url in DHT_MATCHES, t.trackers))
filtered_trackers = list(filter(lambda s: not s.status == 0, t.trackers))
if not filtered_trackers:
continue
domain = extractTLD(
Expand Down

0 comments on commit c15f78f

Please sign in to comment.