updates categories to use group notifications

This commit is contained in:
bobokun 2023-06-03 20:03:38 -04:00
parent e936e4526f
commit c013ff755b
No known key found for this signature in database
GPG key ID: B73932169607D927
2 changed files with 49 additions and 4 deletions

View file

@ -1,6 +1,7 @@
from qbittorrentapi import Conflict409Error
from modules import util
from modules.webhooks import GROUP_NOTIFICATION_LIMIT
logger = util.logger
@ -11,8 +12,11 @@ class Category:
self.config = qbit_manager.config
self.client = qbit_manager.client
self.stats = 0
self.torrents_updated = [] # List of torrents updated
self.notify_attr = [] # List of single torrent attributes to send to notifiarr
self.category()
self.notify()
def category(self):
"""Update category for torrents that don't have any category defined and returns total number categories updated"""
@ -40,6 +44,7 @@ class Category:
def update_cat(self, torrent, new_cat, cat_change):
"""Update category based on the torrent information"""
tracker = self.qbt.get_tags(torrent.trackers)
t_name = torrent.name
old_cat = torrent.category
if not self.config.dry_run:
try:
@ -55,7 +60,7 @@ class Category:
self.client.torrent_categories.create_category(name=new_cat, save_path=torrent.save_path)
torrent.set_category(category=new_cat)
body = []
body += logger.print_line(logger.insert_space(f"Torrent Name: {torrent.name}", 3), self.config.loglevel)
body += logger.print_line(logger.insert_space(f"Torrent Name: {t_name}", 3), self.config.loglevel)
if cat_change:
body += logger.print_line(logger.insert_space(f"Old Category: {old_cat}", 3), self.config.loglevel)
title = "Moving Categories"
@ -67,10 +72,49 @@ class Category:
"function": "cat_update",
"title": title,
"body": "\n".join(body),
"torrent_name": torrent.name,
"torrents": [t_name],
"torrent_category": new_cat,
"torrent_tracker": tracker["url"],
"notifiarr_indexer": tracker["notifiarr"],
}
self.config.send_notifications(attr)
self.notify_attr.append(attr)
self.torrents_updated.append(t_name)
self.stats += 1
def notify(self):
"""Send notifications"""
def group_notifications_by_category(self):
group_attr = {}
"""Group notifications by category"""
for attr in self.notify_attr:
category = attr["torrent_category"]
if category not in group_attr:
group_attr[category] = {
"torrent_category": category,
"torrents": [attr["torrents"][0]],
}
else:
group_attr[category]["torrents"].append(attr["torrents"][0])
return group_attr
if len(self.torrents_updated) > GROUP_NOTIFICATION_LIMIT:
logger.trace(
f"Number of torrents updated > {GROUP_NOTIFICATION_LIMIT}, grouping notifications by category.",
)
group_attr = group_notifications_by_category(self)
for category in group_attr:
attr = {
"function": "cat_update",
"title": f"Updating Category for {category}",
"body": f"Updated {len(group_attr[category]['torrents'])} "
f"{'torrents' if len(group_attr[category]['torrents']) > 1 else 'torrent'} with category '{category}'",
"torrents": group_attr[category]["torrents"],
"torrent_category": category,
"torrent_tracker": None,
"notifiarr_indexer": None,
}
self.config.send_notifications(attr)
else:
for attr in self.notify_attr:
self.config.send_notifications(attr)

View file

@ -13,6 +13,7 @@ class Tags:
self.share_limits_suffix_tag = qbit_manager.config.share_limits_suffix_tag # suffix tag for share limits
self.torrents_updated = [] # List of torrents updated
self.notify_attr = [] # List of single torrent attributes to send to notifiarr
self.tags()
self.notify()
@ -78,7 +79,7 @@ class Tags:
if len(self.torrents_updated) > GROUP_NOTIFICATION_LIMIT:
logger.trace(
f"Number of tags > {GROUP_NOTIFICATION_LIMIT}, grouping notifications by tag.",
f"Number of torrents updated > {GROUP_NOTIFICATION_LIMIT}, grouping notifications by tag.",
)
group_attr = group_notifications_by_tag(self)
for tag in group_attr: