Skip to content

Commit

Permalink
chore(qbtools): refactor tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
buroa committed Oct 7, 2024
1 parent 1d6ad34 commit b6856c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
44 changes: 19 additions & 25 deletions qbtools/commands/tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@
def __init__(app, logger):
logger.info(f"Tagging torrents in qBittorrent...")

extractTLD = tldextract.TLDExtract(cache_dir=None)
today = datetime.today()
tagged = collections.defaultdict(list)
tags = collections.defaultdict(list)
content_paths = []

torrents = app.client.torrents.info()
today = datetime.today()
extractTLD = tldextract.TLDExtract(cache_dir=None)

trackers = app.config.get("trackers", [])
trackers = {y: x for x in trackers for y in x["urls"]}
Expand All @@ -78,21 +77,20 @@ def __init__(app, logger):
)
)

torrents = app.client.torrents.info()

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

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
else:
filtered_trackers = sorted(filtered_trackers, key=lambda x: x.url)
not_working = not t.tracker
if not_working:
filtered_trackers = [s for s in t.trackers if s.tier >= 0] # Expensive
if not filtered_trackers:
continue

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

if app.added_on:
Expand Down Expand Up @@ -134,7 +132,7 @@ def __init__(app, logger):
if app.domains:
tags_to_add.append(f"domain:{domain}")

if app.unregistered or app.tracker_down or app.not_working:
if (app.unregistered or app.tracker_down or app.not_working) and not_working:
if not any(s.status == TrackerStatus.WORKING for s in filtered_trackers):
tracker_messages = [z.msg.upper() for z in filtered_trackers]
if app.unregistered and any(
Expand Down Expand Up @@ -168,30 +166,26 @@ def __init__(app, logger):
tags_to_add.append("not-linked")

for tag in tags_to_add:
tagged[tag].append(t)

logger.info("Done figuring out tags...")
tags[tag].append(t)

default_tags = list(
filter(
lambda tag: any(tag.lower().startswith(x.lower()) for x in DEFAULT_TAGS),
app.client.torrents_tags(),
)
)
empty_tags = list(filter(lambda tag: tag not in tagged, default_tags))
empty_tags = list(filter(lambda tag: tag not in tags, default_tags))
if empty_tags:
app.client.torrents_delete_tags(tags=empty_tags)
logger.info(f"Removed {len(empty_tags)} old tags from qBittorrent")

logger.info("Done removing old tags...")

for tag, tagged_torrents in tagged.items():
old_torrents = [t.hash for t in torrents if tag in t.tags and not t in tagged_torrents]
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"Removed {len(old_torrents)} torrents with {tag}")

new_torrents = [t.hash for t in tagged_torrents if not tag in t.tags]
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}")
Expand Down
1 change: 1 addition & 0 deletions qbtools/qbtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def get_config(app):

def main():
logging.getLogger("filelock").setLevel(logging.ERROR) # Suppress lock messages
logging.getLogger("urllib3").setLevel(logging.DEBUG) # Enable urllib3 logging
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
Expand Down

0 comments on commit b6856c2

Please sign in to comment.