Skip to content

Commit

Permalink
chore(qbtools): clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
buroa committed Oct 7, 2024
1 parent 3e101cb commit 7f9aa1a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ trackers: {}
# - bgp.technology
# - empirehost.me
# - stackoverflow.tech
# required_seed_ratio: 1.05
# required_seed_ratio: 1.05
# required_seed_days: 14.5
19 changes: 10 additions & 9 deletions qbtools/commands/prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def __init__(app, logger):
categories = list(
filter(lambda c: any(fnmatch(c, p) for p in includes), categories)
)

if app.exclude_category:
excludes = [i for s in app.exclude_category for i in s]
categories = list(
Expand All @@ -21,36 +22,36 @@ def __init__(app, logger):
f"No torrents can be pruned since no categories were included based on selectors"
)

filtered_torrents = app.client.torrents.info()
filtered_torrents = list(
filter(lambda x: x.category in categories, filtered_torrents)
torrents = app.client.torrents.info()
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:
filtered_torrents = list(
filter(lambda x: all(y in x.tags for y in include_tags), filtered_torrents)
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:
filtered_torrents = list(
torrents = list(
filter(
lambda x: not any(y in x.tags for y in exclude_tags), filtered_torrents
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)}]..."
)

for t in filtered_torrents:
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'])}]"
)
if not app.dry_run:
t.delete(delete_files=app.with_data)

logger.info(f"Deleted {len(filtered_torrents)} torrents")
logger.info(f"Deleted {len(torrents)} torrents")


def add_arguments(command, subparser):
Expand Down
29 changes: 16 additions & 13 deletions qbtools/commands/tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,45 @@ def __init__(app, logger):
tag_sizes = collections.defaultdict(int)
content_paths = []

filtered_torrents = app.client.torrents.info()
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:
filtered_torrents = list(
filter(lambda x: x.category not in exclude_categories, filtered_torrents)
torrents = list(
filter(lambda x: x.category not in exclude_categories, torrents)
)

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

# Gather items to tag in qBittorrent
for t in filtered_torrents:
for t in torrents:
tags_to_add = []

filtered_trackers = (
list(filter(lambda s: not s.tier == -1, t.trackers))
if t.properties.is_private
else t.trackers
)

if not filtered_trackers:
continue
domain = extractTLD(
sorted(filtered_trackers, key=lambda x: x.url)[0].url
).registered_domain
tracker = utils.filter_tracker_by_domain(domain, trackers)
else:
filtered_trackers = sorted(filtered_trackers, key=lambda x: x.url)

domain = extractTLD(filtered_trackers[0].url).registered_domain
tracker = trackers.get(domain)

if app.added_on:
added_on = datetime.fromtimestamp(t.added_on)
Expand Down Expand Up @@ -189,7 +193,7 @@ def __init__(app, logger):
)
)
if default_tags:
hashes = list(map(lambda t: t.hash, filtered_torrents))
hashes = list(map(lambda t: t.hash, torrents))
app.client.torrents_remove_tags(tags=default_tags, torrent_hashes=hashes)
empty_tags = list(
filter(
Expand All @@ -200,9 +204,8 @@ def __init__(app, logger):
default_tags,
)
)
logger.info(f"Removing {len(empty_tags)} old tags from qBittorrent...")
app.client.torrents_delete_tags(tags=empty_tags)
logger.info(f"Done removing {len(empty_tags)} old tags from qBittorrent")
logger.info(f"Removed {len(empty_tags)} old tags from qBittorrent")

unique_hashes = set()
for hash_list in tag_hashes.values():
Expand Down
6 changes: 0 additions & 6 deletions qbtools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ def format_bytes(size):
return f"{formatted} {power_labels[n]}"


def filter_tracker_by_domain(domain, trackers=[]):
for tracker in trackers:
if any(domain in url for url in tracker["urls"]):
return tracker


def seconds(days: int) -> int:
seconds_in_a_day = 86400
seconds = days * seconds_in_a_day
Expand Down

0 comments on commit 7f9aa1a

Please sign in to comment.