Skip to content

Commit

Permalink
chore(tagging): log to one line (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
buroa authored Oct 24, 2024
1 parent 3aaf4ca commit 1ee538c
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions qbtools/commands/tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def __init__(app, logger):

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

exclude_categories = [i for s in app.exclude_category for i in s]
if exclude_categories:
Expand All @@ -73,13 +73,13 @@ def __init__(app, logger):

for t in torrents:
tags_to_add = []
private_trackers = list(filter(lambda s: s.tier >= 0, t.trackers)) # Expensive
trackers = list(filter(lambda s: s.tier >= 0, t.trackers)) # Expensive

url = t.tracker
if not url and private_trackers:
url = private_trackers[0].url
if not url and trackers:
url = trackers[0].url

tracker = trackers.get(extractTLD(url).registered_domain)
tracker = config.get(extractTLD(url).registered_domain)

if app.added_on:
tags_to_add.append(calculate_date_tags("added", t.added_on, today))
Expand All @@ -94,8 +94,8 @@ def __init__(app, logger):
tags_to_add.append(f"site:unmapped")

if app.unregistered or app.tracker_down or app.not_working:
if not any(s.status is TrackerStatus.WORKING for s in private_trackers):
messages = [z.msg.upper() for z in private_trackers]
if not any(s.status is TrackerStatus.WORKING for s in trackers):
messages = [z.msg.upper() for z in trackers]
if app.unregistered and any(
match in msg for msg in messages for match in UNREGISTERED_MATCHES
):
Expand Down Expand Up @@ -130,6 +130,19 @@ def __init__(app, logger):
for tag in tags_to_add:
tags[tag].append(t)

for tag, tagged in sorted(tags.items()):
old_hashes = [t.hash for t in torrents if tag in t.tags and not t in tagged]
new_hashes = [t.hash for t in tagged if not tag in t.tags]

if old_hashes:
app.client.torrents_remove_tags(tags=tag, torrent_hashes=old_hashes)

if new_hashes:
app.client.torrents_add_tags(tags=tag, torrent_hashes=new_hashes)

if old_hashes or new_hashes:
logger.info(f"{tag} - untagged {len(new_hashes)} old and tagged {len(old_hashes)} new")

empty_tags = list(
filter(
lambda tag: tag not in tags
Expand All @@ -141,17 +154,6 @@ def __init__(app, logger):
app.client.torrents_delete_tags(tags=empty_tags)
logger.info(f"Removed {len(empty_tags)} old tags from qBittorrent")

for tag, tagged in tags.items():
old_torrents = [t.hash for t in torrents if tag in t.tags and not t in tagged]
if old_torrents:
app.client.torrents_remove_tags(tags=tag, torrent_hashes=old_torrents)
logger.info(f"Untagged {len(old_torrents)} torrents with tag: {tag}")

new_torrents = [t.hash for t in tagged if not tag in t.tags]
if new_torrents:
app.client.torrents_add_tags(tags=tag, torrent_hashes=new_torrents)
logger.info(f"Tagged {len(new_torrents)} torrents with tag: {tag}")

logger.info("Finished tagging torrents in qBittorrent")


Expand Down

0 comments on commit 1ee538c

Please sign in to comment.