refactor webhook to group notifications

This commit is contained in:
bobokun 2023-06-03 23:24:56 -04:00
parent 2ce622f6b9
commit 38c8ab9c11
No known key found for this signature in database
GPG key ID: B73932169607D927
3 changed files with 64 additions and 100 deletions

View file

@ -1,7 +1,6 @@
from qbittorrentapi import Conflict409Error from qbittorrentapi import Conflict409Error
from modules import util from modules import util
from modules.webhooks import GROUP_NOTIFICATION_LIMIT
logger = util.logger logger = util.logger
@ -16,7 +15,7 @@ class Category:
self.notify_attr = [] # List of single torrent attributes to send to notifiarr self.notify_attr = [] # List of single torrent attributes to send to notifiarr
self.category() self.category()
self.notify() self.config.webhooks_factory.notify(self.torrents_updated, self.notify_attr, group_by="category")
def category(self): def category(self):
"""Update category for torrents that don't have any category defined and returns total number categories updated""" """Update category for torrents that don't have any category defined and returns total number categories updated"""
@ -81,51 +80,3 @@ class Category:
self.notify_attr.append(attr) self.notify_attr.append(attr)
self.torrents_updated.append(t_name) self.torrents_updated.append(t_name)
self.stats += 1 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,
"body": attr["body"],
"torrents": [attr["torrents"][0]],
"torrent_tag": attr["torrent_tag"],
"torrent_tracker": attr["torrent_tracker"],
"notifiarr_indexer": attr["notifiarr_indexer"],
}
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:
num_torrents_updated = len(group_attr[category]["torrents"])
only_one_torrent_updated = num_torrents_updated == 1
attr = {
"function": "cat_update",
"title": f"Updating Category for {category}",
"body": group_attr[category]["body"]
if only_one_torrent_updated
else f"Updated {num_torrents_updated} "
f"{'torrent' if only_one_torrent_updated else 'torrents'} with category '{category}'",
"torrents": group_attr[category]["torrents"],
"torrent_category": category,
"torrent_tag": group_attr[category]["torrent_tag"] if only_one_torrent_updated else None,
"torrent_tracker": group_attr[category]["torrent_tracker"] if only_one_torrent_updated else None,
"notifiarr_indexer": group_attr[category]["notifiarr_indexer"] if only_one_torrent_updated else None,
}
self.config.send_notifications(attr)
else:
for attr in self.notify_attr:
self.config.send_notifications(attr)

View file

@ -1,5 +1,4 @@
from modules import util from modules import util
from modules.webhooks import GROUP_NOTIFICATION_LIMIT
logger = util.logger logger = util.logger
@ -15,7 +14,7 @@ class Tags:
self.notify_attr = [] # List of single torrent attributes to send to notifiarr self.notify_attr = [] # List of single torrent attributes to send to notifiarr
self.tags() self.tags()
self.notify() self.config.webhooks_factory.notify(self.torrents_updated, self.notify_attr, group_by="tag")
def tags(self): def tags(self):
"""Update tags for torrents""" """Update tags for torrents"""
@ -57,51 +56,3 @@ class Tags:
) )
else: else:
logger.print_line("No new torrents to tag.", self.config.loglevel) logger.print_line("No new torrents to tag.", self.config.loglevel)
def notify(self):
"""Send notifications"""
def group_notifications_by_tag(self):
group_attr = {}
"""Group notifications by tag"""
for attr in self.notify_attr:
tag = attr["torrent_tag"]
if tag not in group_attr:
group_attr[tag] = {
"torrent_tag": tag,
"body": attr["body"],
"torrents": [attr["torrents"][0]],
"torrent_category": attr["torrent_category"],
"torrent_tracker": attr["torrent_tracker"],
"notifiarr_indexer": attr["notifiarr_indexer"],
}
else:
group_attr[tag]["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 tag.",
)
group_attr = group_notifications_by_tag(self)
for tag in group_attr:
num_torrents_updated = len(group_attr[tag]["torrents"])
only_one_torrent_updated = num_torrents_updated == 1
attr = {
"function": "tag_update",
"title": f"Updating Tags for {tag}",
"body": group_attr[tag]["body"]
if only_one_torrent_updated
else f"Updated {num_torrents_updated} "
f"{'torrent' if only_one_torrent_updated else 'torrents'} with tag '{tag}'",
"torrents": group_attr[tag]["torrents"],
"torrent_category": group_attr[tag]["torrent_category"] if only_one_torrent_updated else None,
"torrent_tag": tag,
"torrent_tracker": group_attr[tag]["torrent_tracker"],
"notifiarr_indexer": group_attr[tag]["notifiarr_indexer"],
}
self.config.send_notifications(attr)
else:
for attr in self.notify_attr:
self.config.send_notifications(attr)

View file

@ -165,3 +165,65 @@ class Webhooks:
"""Send a webhook to notify that a function has completed.""" """Send a webhook to notify that a function has completed."""
if self.function_webhooks: if self.function_webhooks:
self._request(webhook, json) self._request(webhook, json)
def notify(self, torrents_updated=[], payload={}, group_by=""):
if len(torrents_updated) > GROUP_NOTIFICATION_LIMIT:
logger.trace(
f"Number of torrents updated > {GROUP_NOTIFICATION_LIMIT}, grouping notifications"
f"{f' by {group_by}' if group_by else ''}",
)
if group_by == "category":
group_attr = group_notifications_by_key(payload, "torrent_category")
elif group_by == "tag":
group_attr = group_notifications_by_key(payload, "torrent_tag")
# group notifications by grouping attribute
for group in group_attr:
num_torrents_updated = len(group_attr[group]["torrents"])
only_one_torrent_updated = num_torrents_updated == 1
attr = {
"function": group_attr[group]["function"],
"title": f"{group_attr[group]['title']} for {group}",
"body": group_attr[group]["body"]
if only_one_torrent_updated
else f"Updated {num_torrents_updated} "
f"{'torrent' if only_one_torrent_updated else 'torrents'} with {group_by} '{group}'",
"torrents": group_attr[group]["torrents"],
}
if group_by == "category":
attr["torrent_category"] = group
attr["torrent_tag"] = group_attr[group]["torrent_tag"] if only_one_torrent_updated else None
attr["torrent_tracker"] = group_attr[group]["torrent_tracker"] if only_one_torrent_updated else None
attr["notifiarr_indexer"] = group_attr[group]["notifiarr_indexer"] if only_one_torrent_updated else None
elif group_by == "tag":
attr["torrent_tag"] = group
attr["torrent_category"] = group_attr[group]["torrent_category"] if only_one_torrent_updated else None
attr["torrent_tracker"] = group_attr[group]["torrent_tracker"]
attr["notifiarr_indexer"] = group_attr[group]["notifiarr_indexer"]
self.config.send_notifications(attr)
else:
for attr in payload:
self.config.send_notifications(attr)
def group_notifications_by_key(payload, key):
"""Group notifications by key"""
group_attr = {}
for attr in payload:
group = attr[key]
if group not in group_attr:
group_attr[group] = {
"function": attr["function"],
"title": attr["title"],
"body": attr["body"],
"torrent_category": attr["torrent_category"],
"torrent_tag": attr["torrent_tag"],
"torrents": [attr["torrents"][0]],
"torrent_tracker": attr["torrent_tracker"],
"notifiarr_indexer": attr["notifiarr_indexer"],
}
else:
group_attr[group]["torrents"].append(attr["torrents"][0])
return group_attr