-
-
Notifications
You must be signed in to change notification settings - Fork 337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Apprise Integration #2796
Apprise Integration #2796
Changes from all commits
f8129e2
1b04ecb
43847c3
e39d44a
5efb9df
cc97db6
6cff4ce
bd882fe
82d1b23
a34ef89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,8 @@ | |
import http.client | ||
import json | ||
from threading import Thread | ||
from typing import Optional, Dict | ||
from typing import Optional, Dict, Union | ||
import apprise | ||
Safihre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import sabnzbd | ||
import sabnzbd.cfg | ||
|
@@ -115,6 +116,20 @@ def get_prio(notification_type: str, section: str) -> int: | |
return -1000 | ||
|
||
|
||
def get_target(notification_type: str, section: str) -> Union[str, bool, None]: | ||
"""Check target of `notification_type` in `section` if enabled is set""" | ||
try: | ||
if sabnzbd.config.get_config(section, "%s_target_%s_enable" % (section, notification_type))() > 0: | ||
if result := sabnzbd.config.get_config(section, "%s_target_%s" % (section, notification_type))(): | ||
return result | ||
# Use Default | ||
return True | ||
except TypeError: | ||
logging.debug("Incorrect Notify option %s:%s_target_%s", section, section, notification_type) | ||
return False | ||
return False | ||
|
||
|
||
def check_cat(section: str, job_cat: str, keyword: Optional[str] = None) -> bool: | ||
"""Check if `job_cat` is enabled in `section`. | ||
* = All, if no other categories selected. | ||
|
@@ -165,6 +180,11 @@ def send_notification( | |
if sabnzbd.cfg.pushbullet_apikey() and check_classes(notification_type, "pushbullet"): | ||
Thread(target=send_pushbullet, args=(title, msg, notification_type)).start() | ||
|
||
# Apprise | ||
if sabnzbd.cfg.apprise_enable() and check_cat("apprise", job_cat): | ||
if sabnzbd.cfg.apprise_urls() and check_classes(notification_type, "apprise"): | ||
Thread(target=send_apprise, args=(title, msg, notification_type)).start() | ||
|
||
# Notification script. | ||
if sabnzbd.cfg.nscript_enable() and check_cat("nscript", job_cat): | ||
if sabnzbd.cfg.nscript_script(): | ||
|
@@ -265,6 +285,90 @@ def send_prowl(title, msg, notification_type, force=False, test=None): | |
return "" | ||
|
||
|
||
def send_apprise(title, msg, notification_type, force=False, test=None): | ||
"""send apprise message""" | ||
logging.debug("Sending Apprise notification") | ||
if test: | ||
urls = test.get("apprise_urls") | ||
else: | ||
urls = sabnzbd.cfg.apprise_urls() | ||
|
||
# Notification mapper | ||
n_map = { | ||
# Startup/Shutdown | ||
"startup": apprise.common.NotifyType.INFO, | ||
# Pause/Resume | ||
"pause_resume": apprise.common.NotifyType.INFO, | ||
# Added NZB | ||
"download": apprise.common.NotifyType.INFO, | ||
# Post-processing started | ||
"pp": apprise.common.NotifyType.INFO, | ||
# Job finished | ||
"complete": apprise.common.NotifyType.SUCCESS, | ||
# Job failed | ||
"failed": apprise.common.NotifyType.FAILURE, | ||
# Warning | ||
"warning": apprise.common.NotifyType.WARNING, | ||
# Error | ||
"error": apprise.common.NotifyType.FAILURE, | ||
# Disk full | ||
"disk_full": apprise.common.NotifyType.WARNING, | ||
# Queue finished | ||
"queue_done": apprise.common.NotifyType.INFO, | ||
# User logged in | ||
"new_login": apprise.common.NotifyType.INFO, | ||
# Other Messages | ||
"other": apprise.common.NotifyType.INFO, | ||
} | ||
|
||
# Prepare our Asset Object | ||
asset = apprise.AppriseAsset( | ||
app_id="SABnzbd", | ||
app_desc="SABnzbd Notification", | ||
app_url="https://sabnzbd.org/", | ||
image_path_mask=os.path.join(sabnzbd.DIR_PROG, "icons", "apprise", "apprise-{TYPE}.png"), | ||
image_url_mask="https://sabnzbd.org/images/icons/apprise/{TYPE}.png", | ||
image_url_logo="https://sabnzbd.org/images/icons/apple-touch-icon-180x180-precomposed.png", | ||
) | ||
|
||
# Initialize our Apprise Instance | ||
apobj = apprise.Apprise(asset=asset) | ||
|
||
if not test: | ||
# Get a list of tags that are set to use the common list | ||
if target := get_target(notification_type, "apprise"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah sorry I missed that about the empty string! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no worries; it makes a bit more sense now.. True - use Default, otherwise string means there are URLs to load. |
||
if target is True: | ||
# Use default list | ||
apobj.add(urls) | ||
elif not apobj.add(target): | ||
# Target is string of URLs to over-ride with | ||
# Store our URL and assign our key | ||
logging.warning("%s - %s", notification_type, T("One or more Apprise URLs could not be loaded.")) | ||
else: | ||
# Nothing to notify | ||
return "" | ||
else: | ||
# Use default list | ||
apobj.add(urls) | ||
|
||
try: | ||
# The below notifies anything added to our list | ||
if not apobj.notify( | ||
body=msg, | ||
title=title, | ||
notify_type=n_map[notification_type], | ||
body_format=apprise.NotifyFormat.TEXT, | ||
): | ||
return T("Failed to send one or more Apprise Notifications") | ||
|
||
except: | ||
logging.warning(T("Failed to send Apprise message")) | ||
logging.info("Traceback: ", exc_info=True) | ||
return T("Failed to send Apprise message") | ||
|
||
return "" | ||
|
||
|
||
def send_pushover(title, msg, notification_type, force=False, test=None): | ||
"""Send message to pushover""" | ||
logging.debug("Sending Pushover notification") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pyYAML is needed by Apprise. Zoggys advice to define these 2 entries was a really good one. I don't think we should remove it from the requirements.txt.
Also, the way it was gave every system that had the ability to leverage the binary package to do so yet still be compatible for the systems that couldn't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not removed, it's added a few lines down:
https://github.com/sabnzbd/sabnzbd/pull/2796/files#diff-4d7c51b1efe9043e44439a949dfd92e5827321b34082903477fd04876edb7552R55
But without the special
darwin
stuff, as this is handled already here:https://github.com/sabnzbd/sabnzbd/pull/2796/files#diff-ec12760430cbac327a0a49aff7d95b2803a24a5d066c654f37a78f9177fdf36aR108