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 fb18945 commit d8fc031
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 67 deletions.
98 changes: 31 additions & 67 deletions qbtools/commands/tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@
def __init__(app, logger):
logger.info(f"Tagging torrents in qBittorrent...")

extractTLD = tldextract.TLDExtract(cache_dir=None)
today = datetime.today()
tag_hashes = collections.defaultdict(list)
tag_sizes = collections.defaultdict(int)
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 @@ -79,22 +77,20 @@ def __init__(app, logger):
)
)

# Gather items to tag in qBittorrent
torrents = app.client.torrents.info()

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

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)
url = t.tracker
if not url:
filtered = [s for s in t.trackers if s.tier >= 0] # Expensive
if not filtered:
continue
url = filtered[0].url

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

if app.added_on:
Expand Down Expand Up @@ -136,9 +132,9 @@ 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 not any(s.status == TrackerStatus.WORKING for s in filtered_trackers):
tracker_messages = [z.msg.upper() for z in filtered_trackers]
if (app.unregistered or app.tracker_down or app.not_working) and filtered:
if not any(s.status == TrackerStatus.WORKING for s in filtered):
tracker_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
):
Expand All @@ -162,66 +158,39 @@ def __init__(app, logger):
tags_to_add.append("expired")

if app.duplicates:
match = [
(infohash, path, size)
for infohash, path, size in content_paths
if path == t.content_path and not t.content_path == t.save_path
]
if match:
if t.content_path in content_paths and not t.content_path == t.save_path:
tags_to_add.append("dupe")
tag_hashes["dupe"].append(match[0][0])
if app.size:
tag_sizes["dupe"] += match[0][2]

content_paths.append((t.hash, t.content_path, t.size))
content_paths.append(t.content_path)

if app.not_linked and not utils.is_linked(t.content_path):
tags_to_add.append("not-linked")

for tag in tags_to_add:
tag_hashes[tag].append(t.hash)
if app.size:
tag_sizes[tag] += t.size
tags[tag].append(t)

# Remove old tags
default_tags = list(
filter(
lambda tag: any(tag.lower().startswith(x.lower()) for x in DEFAULT_TAGS),
app.client.torrents_tags(),
)
)
if default_tags:
hashes = list(map(lambda t: t.hash, torrents))
app.client.torrents_remove_tags(tags=default_tags, torrent_hashes=hashes)
empty_tags = list(
filter(
lambda tag: len(
list(filter(lambda t: tag in t.tags, app.client.torrents.info()))
)
== 0,
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")

unique_hashes = set()
for hash_list in tag_hashes.values():
unique_hashes.update(hash_list)
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}")

# Apply tags
for tag in tag_hashes:
if app.size:
size = utils.format_bytes(tag_sizes[tag])
app.client.torrents_add_tags(
tags=f"{tag} [{size}]", torrent_hashes=tag_hashes[tag]
)
else:
app.client.torrents_add_tags(tags=tag, torrent_hashes=tag_hashes[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}")

logger.info(
f"Completed tagging {len(unique_hashes)} torrents with {len(tag_hashes)} tags"
)
logger.info("Finished tagging torrents in qBittorrent")


def add_arguments(command, subparser):
Expand Down Expand Up @@ -289,11 +258,6 @@ def add_arguments(command, subparser):
parser.add_argument(
"--sites", action="store_true", help="Tag torrents with known site names"
)
parser.add_argument(
"--size",
action="store_true",
help="Add size of tagged torrents to created tags",
)
parser.add_argument(
"--tracker-down",
action="store_true",
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 d8fc031

Please sign in to comment.