Fix splitting of script arguments

This commit is contained in:
JonnyWong16 2017-03-25 11:58:37 -07:00
parent 3513f7fe2c
commit 0df1bd137d
3 changed files with 39 additions and 21 deletions

View file

@ -481,7 +481,7 @@ def dbcheck():
'CREATE TABLE IF NOT EXISTS notify_log (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER, ' 'CREATE TABLE IF NOT EXISTS notify_log (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER, '
'session_key INTEGER, rating_key INTEGER, parent_rating_key INTEGER, grandparent_rating_key INTEGER, ' 'session_key INTEGER, rating_key INTEGER, parent_rating_key INTEGER, grandparent_rating_key INTEGER, '
'user_id INTEGER, user TEXT, notifier_id INTEGER, agent_id INTEGER, agent_name TEXT, notify_action TEXT, ' 'user_id INTEGER, user TEXT, notifier_id INTEGER, agent_id INTEGER, agent_name TEXT, notify_action TEXT, '
'subject_text TEXT, body_text TEXT)' 'subject_text TEXT, body_text TEXT, script_args TEXT)'
) )
# library_sections table :: This table keeps record of the servers library sections # library_sections table :: This table keeps record of the servers library sections

View file

@ -17,6 +17,7 @@
import arrow import arrow
import bleach import bleach
from itertools import groupby from itertools import groupby
import json
from operator import itemgetter from operator import itemgetter
import os import os
import re import re
@ -185,7 +186,7 @@ def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data
body_string = notifier_config['notify_text'][notify_action]['body'] body_string = notifier_config['notify_text'][notify_action]['body']
# Format the subject and body strings # Format the subject and body strings
subject, body = build_notify_text(subject=subject_string, subject, body, script_args = build_notify_text(subject=subject_string,
body=body_string, body=body_string,
notify_action=notify_action, notify_action=notify_action,
parameters=parameters, parameters=parameters,
@ -195,6 +196,7 @@ def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data
notifiers.send_notification(notifier_id=notifier_config['id'], notifiers.send_notification(notifier_id=notifier_config['id'],
subject=subject, subject=subject,
body=body, body=body,
script_args=script_args,
notify_action=notify_action, notify_action=notify_action,
parameters=parameters) parameters=parameters)
@ -203,7 +205,8 @@ def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data
notify_action=notify_action, notify_action=notify_action,
notifier=notifier_config, notifier=notifier_config,
subject=subject, subject=subject,
body=body) body=body,
script_args=script_args)
def get_notify_state(session): def get_notify_state(session):
@ -225,13 +228,15 @@ def get_notify_state(session):
return notify_states return notify_states
def set_notify_state(notify_action, notifier, subject, body, session=None): def set_notify_state(notify_action, notifier, subject, body, script_args, session=None):
if notify_action and notifier: if notify_action and notifier:
monitor_db = database.MonitorDatabase() monitor_db = database.MonitorDatabase()
session = session or {} session = session or {}
script_args = json.dumps(script_args) if script_args else None
keys = {'timestamp': int(time.time()), keys = {'timestamp': int(time.time()),
'session_key': session.get('session_key', None), 'session_key': session.get('session_key', None),
'rating_key': session.get('rating_key', None), 'rating_key': session.get('rating_key', None),
@ -245,7 +250,8 @@ def set_notify_state(notify_action, notifier, subject, body, session=None):
'user': session.get('user', None), 'user': session.get('user', None),
'agent_name': notifier['agent_name'], 'agent_name': notifier['agent_name'],
'subject_text': subject, 'subject_text': subject,
'body_text': body} 'body_text': body,
'script_args': script_args}
monitor_db.upsert(table_name='notify_log', key_dict=keys, value_dict=values) monitor_db.upsert(table_name='notify_log', key_dict=keys, value_dict=values)
else: else:
@ -638,6 +644,9 @@ def build_server_notify_params(notify_action=None, **kwargs):
def build_notify_text(subject='', body='', notify_action=None, parameters=None, agent_id=None, test=False): def build_notify_text(subject='', body='', notify_action=None, parameters=None, agent_id=None, test=False):
# Default subject and body text # Default subject and body text
if agent_id == 15:
default_subject = default_body = ''
else:
default_action = next((a for a in notifiers.available_notification_actions() if a['name'] == notify_action), {}) default_action = next((a for a in notifiers.available_notification_actions() if a['name'] == notify_action), {})
default_subject = default_action.get('subject', '') default_subject = default_action.get('subject', '')
default_body = default_action.get('body', '') default_body = default_action.get('body', '')
@ -681,13 +690,25 @@ def build_notify_text(subject='', body='', notify_action=None, parameters=None,
if test: if test:
return subject, body return subject, body
if agent_id == 15:
try:
script_args = [unicode(arg).format(**parameters) for arg in subject.split()]
except LookupError as e:
logger.error(u"PlexPy NotificationHandler :: Unable to parse field %s in script argument. Using fallback." % e)
script_args = []
except Exception as e:
logger.error(u"PlexPy NotificationHandler :: Unable to parse custom script arguments: %s. Using fallback." % e)
script_args = []
else:
script_args = []
try: try:
subject = unicode(subject).format(**parameters) subject = unicode(subject).format(**parameters)
except LookupError as e: except LookupError as e:
logger.error(u"PlexPy NotificationHandler :: Unable to parse field %s in notification subject. Using fallback." % e) logger.error(u"PlexPy NotificationHandler :: Unable to parse field %s in notification subject. Using fallback." % e)
subject = unicode(default_subject).format(**parameters) subject = unicode(default_subject).format(**parameters)
except: except Exception as e:
logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification subject. Using fallback.") logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification subject: %s. Using fallback." % e)
subject = unicode(default_subject).format(**parameters) subject = unicode(default_subject).format(**parameters)
try: try:
@ -695,11 +716,11 @@ def build_notify_text(subject='', body='', notify_action=None, parameters=None,
except LookupError as e: except LookupError as e:
logger.error(u"PlexPy NotificationHandler :: Unable to parse field %s in notification body. Using fallback." % e) logger.error(u"PlexPy NotificationHandler :: Unable to parse field %s in notification body. Using fallback." % e)
body = unicode(default_body).format(**parameters) body = unicode(default_body).format(**parameters)
except: except Exception as e:
logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.") logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body: %s. Using fallback." % e)
body = unicode(default_body).format(**parameters) body = unicode(default_body).format(**parameters)
return subject, body return subject, body, script_args
def strip_tag(data, agent_id=None): def strip_tag(data, agent_id=None):

View file

@ -2380,14 +2380,11 @@ class SCRIPTS(Notifier):
logger.error(u"PlexPy Notifiers :: No script folder specified.") logger.error(u"PlexPy Notifiers :: No script folder specified.")
return return
script_args = subject or None script_args = kwargs.get('script_args', [])
logger.debug(u"PlexPy Notifiers :: Trying to run notify script, action: %s, arguments: %s" logger.debug(u"PlexPy Notifiers :: Trying to run notify script, action: %s, arguments: %s"
% (action, script_args)) % (action, script_args))
if not script_args:
script_args = []
script = kwargs.get('script', self.config.get('script', '')) script = kwargs.get('script', self.config.get('script', ''))
# Don't try to run the script if the action does not have one # Don't try to run the script if the action does not have one