diff --git a/plexpy/api2.py b/plexpy/api2.py index 4706daa7..84b52b6a 100644 --- a/plexpy/api2.py +++ b/plexpy/api2.py @@ -35,6 +35,8 @@ import database import libraries import logger import mobile_app +import notification_handler +import notifiers import users @@ -397,6 +399,50 @@ class API2: return + def notify(self, notifier_id='', subject='Tautulli', body='Test notification', **kwargs): + """ Send a notification using Tautulli. + + ``` + Required parameters: + notifier_id (int): The ID number of the notification agent + subject (str): The subject of the message + body (str): The body of the message + + Optional parameters: + None + + Returns: + None + ``` + """ + if not notifier_id: + self._api_msg = 'Notification failed: no notifier id provided.' + self._api_result_type = 'error' + return + + notifier = notifiers.get_notifier_config(notifier_id=notifier_id) + + if not notifier: + self._api_msg = 'Notification failed: invalid notifier_id provided %s.' % notifier_id + self._api_result_type = 'error' + return + + logger.api_debug(u'Tautulli APIv2 :: Sending notification.') + success = notification_handler.notify(notifier_id=notifier_id, + notify_action='api', + subject=subject, + body=body, + **kwargs) + + if success: + self._api_msg = 'Notification sent.' + self._api_result_type = 'success' + else: + self._api_msg = 'Notification failed.' + self._api_result_type = 'error' + + return + def _api_make_md(self): """ Tries to make a API.md to simplify the api docs. """ @@ -581,8 +627,8 @@ General optional parameters: if isinstance(result, (dict, list)): ret = result else: - raise - except: + raise Exception + except Exception: try: ret = json.loads(result) except (ValueError, TypeError): diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 91d297e4..01853bbe 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -332,7 +332,7 @@ def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data if not notifier_config: return - if notify_action == 'test': + if notify_action in ('test', 'api'): subject = kwargs.pop('subject', 'Tautulli') body = kwargs.pop('body', 'Test Notification') script_args = kwargs.pop('script_args', []) @@ -350,8 +350,8 @@ def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data # Set the notification state in the db notification_id = set_notify_state(session=stream_data or timeline_data, - notify_action=notify_action, notifier=notifier_config, + notify_action=notify_action, subject=subject, body=body, script_args=script_args) @@ -390,9 +390,9 @@ def get_notify_state(session): return notify_states -def set_notify_state(notify_action, notifier, subject, body, script_args, session=None): +def set_notify_state(notifier, notify_action, subject='', body='', script_args='', session=None): - if notify_action and notifier: + if notifier and notify_action: monitor_db = database.MonitorDatabase() session = session or {} diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index a12c98b0..b1e91a48 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -61,7 +61,6 @@ import mobile_app import pmsconnect import request from plexpy.config import _BLACKLIST_KEYS, _WHITELIST_KEYS -from plexpy.helpers import checked AGENT_IDS = {'growl': 0, diff --git a/plexpy/webserve.py b/plexpy/webserve.py index bdde4323..fe5e65e0 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -3079,7 +3079,6 @@ class WebInterface(object): @cherrypy.expose @requireAuth(member_of("admin")) - @addtoapi("notify") def send_notification(self, notifier_id=None, subject='Tautulli', body='Test notification', notify_action='', **kwargs): """ Send a notification using Tautulli.