From 2b6fa769f7d63e01b87b6abc4cfd4852b16548f7 Mon Sep 17 00:00:00 2001 From: aboron Date: Thu, 7 Jul 2016 15:09:48 -0400 Subject: [PATCH 1/6] Added Hipchat integration notifier. --- API.md | 1 + plexpy/config.py | 17 ++++++++ plexpy/notifiers.py | 97 ++++++++++++++++++++++++++++++++++++++++++++- plexpy/webserve.py | 1 + 4 files changed, 115 insertions(+), 1 deletion(-) diff --git a/API.md b/API.md index 8ad58846..f0aa5ee5 100644 --- a/API.md +++ b/API.md @@ -1737,6 +1737,7 @@ Required parameters: 10 # Email 16 # Facebook 0 # Growl + 19 # Hipchat 12 # IFTTT 18 # Join 4 # NotifyMyAndroid diff --git a/plexpy/config.py b/plexpy/config.py index 9dfd5400..7afbee09 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -250,6 +250,23 @@ _CONFIG_DEFINITIONS = { 'JOIN_ON_PMSUPDATE': (int, 'Join', 0), 'JOIN_ON_CONCURRENT': (int, 'Join', 0), 'JOIN_ON_NEWDEVICE': (int, 'Join', 0), + 'HIPCHAT_URL': (str, 'Hipchat', ''), + 'HIPCHAT_COLOR': (str, 'Hipchat', 'green'), + 'HIPCHAT_ENABLED': (int, 'Hipchat', 0), + 'HIPCHAT_ON_PLAY': (int, 'Hipchat', 0), + 'HIPCHAT_ON_STOP': (int, 'Hipchat', 0), + 'HIPCHAT_ON_PAUSE': (int, 'Hipchat', 0), + 'HIPCHAT_ON_RESUME': (int, 'Hipchat', 0), + 'HIPCHAT_ON_BUFFER': (int, 'Hipchat', 0), + 'HIPCHAT_ON_WATCHED': (int, 'Hipchat', 0), + 'HIPCHAT_ON_CREATED': (int, 'Hipchat', 0), + 'HIPCHAT_ON_EXTDOWN': (int, 'Hipchat', 0), + 'HIPCHAT_ON_INTDOWN': (int, 'Hipchat', 0), + 'HIPCHAT_ON_EXTUP': (int, 'Hipchat', 0), + 'HIPCHAT_ON_INTUP': (int, 'Hipchat', 0), + 'HIPCHAT_ON_PMSUPDATE': (int, 'Hipchat', 0), + 'HIPCHAT_ON_CONCURRENT': (int, 'Hipchat', 0), + 'HIPCHAT_ON_NEWDEVICE': (int, 'Hipchat', 0), 'JOURNAL_MODE': (str, 'Advanced', 'wal'), 'LAUNCH_BROWSER': (int, 'General', 1), 'LOG_BLACKLIST': (int, 'General', 1), diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 9addef67..8c513ae7 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -62,7 +62,8 @@ AGENT_IDS = {"Growl": 0, "Scripts": 15, "Facebook": 16, "Browser": 17, - "Join": 18} + "Join": 18, + "Hipchat": 19} def available_notification_agents(): @@ -425,6 +426,26 @@ def available_notification_agents(): 'on_pmsupdate': plexpy.CONFIG.JOIN_ON_PMSUPDATE, 'on_concurrent': plexpy.CONFIG.JOIN_ON_CONCURRENT, 'on_newdevice': plexpy.CONFIG.JOIN_ON_NEWDEVICE + }, + {'name': 'Hipchat', + 'id': AGENT_IDS['Hipchat'], + 'config_prefix': 'hipchat', + 'has_config': True, + 'state': checked(plexpy.CONFIG.HIPCHAT_ENABLED), + 'on_play': plexpy.CONFIG.HIPCHAT_ON_PLAY, + 'on_stop': plexpy.CONFIG.HIPCHAT_ON_STOP, + 'on_pause': plexpy.CONFIG.HIPCHAT_ON_PAUSE, + 'on_resume': plexpy.CONFIG.HIPCHAT_ON_RESUME, + 'on_buffer': plexpy.CONFIG.HIPCHAT_ON_BUFFER, + 'on_watched': plexpy.CONFIG.HIPCHAT_ON_WATCHED, + 'on_created': plexpy.CONFIG.HIPCHAT_ON_CREATED, + 'on_extdown': plexpy.CONFIG.HIPCHAT_ON_EXTDOWN, + 'on_intdown': plexpy.CONFIG.HIPCHAT_ON_INTDOWN, + 'on_extup': plexpy.CONFIG.HIPCHAT_ON_EXTUP, + 'on_intup': plexpy.CONFIG.HIPCHAT_ON_INTUP, + 'on_pmsupdate': plexpy.CONFIG.HIPCHAT_ON_PMSUPDATE, + 'on_concurrent': plexpy.CONFIG.HIPCHAT_ON_CONCURRENT, + 'on_newdevice': plexpy.CONFIG.HIPCHAT_ON_NEWDEVICE } ] @@ -516,6 +537,9 @@ def get_notification_agent_config(agent_id): elif agent_id == 18: join = JOIN() return join.return_config_options() + elif agent_id == 19: + hipchat = HIPCHAT() + return hipchat.return_config_options() else: return [] else: @@ -583,6 +607,9 @@ def send_notification(agent_id, subject, body, notify_action, **kwargs): elif agent_id == 18: join = JOIN() return join.notify(message=body, subject=subject) + elif agent_id == 19: + hipchat = HIPCHAT() + return hipchat.notify(message=body, subject=subject) else: logger.debug(u"PlexPy Notifiers :: Unknown agent id received.") else: @@ -2721,3 +2748,71 @@ class JOIN(object): ] return config_option + +class HIPCHAT(object): + + def __init__(self): + self.apiurl = plexpy.CONFIG.HIPCHAT_URL + self.color = plexpy.CONFIG.HIPCHAT_COLOR + + def notify(self, message, subject): + if not message or not subject: + return + + text = '(plex) ' + subject.encode('utf-8') + ': ' + message.encode('utf-8') + + data = {'color': self.color, + 'message': text, + 'notify': 'false', + 'message_format': 'text'} + + hiphost = urlparse(self.apiurl).hostname + hippath = urlparse(self.apiurl).path + hipquery = urlparse(self.apiurl).query + hipfullq = hippath + '?' + hipquery + + http_handler = HTTPSConnection(hiphost) + http_handler.request("POST", + "%s" % (hipfullq), + headers={'Content-type': "application/json"}, + body=json.dumps(data)) + response = http_handler.getresponse() + request_status = response.status + + if request_status == 200 or request_status == 204: + logger.info(u"PlexPy Notifiers :: Hipchat notification sent.") + return True + elif request_status >= 400 and request_status < 500: + logger.warn(u"PlexPy Notifiers :: Hipchat notification failed: [%s] %s" % (request_status, response.reason)) + return False + else: + logger.warn(u"PlexPy Notifiers :: Hipchat notification failed.") + return False + + def test(self, apiurl, color): + + self.enabled = True + self.apiurl = apiurl + self.color = color + + return self.notify('PlexPy', 'Test Message') + + def return_config_options(self): + config_option = [{'label': 'Hipchat Custom Integrations Full URL', + 'value': self.apiurl, + 'name': 'hipchat_url', + 'description': 'Your Hipchat integration URL. You can get a key from' + ' here.', + 'input_type': 'text' + }, + {'label': 'Hipchat Color', + 'value': self.color, + 'name': 'hipchat_color', + 'description': 'Color for the message to show up in your room. You' + ' may use any valid Hipchat message color value.', + 'input_type': 'text' + } + + ] + + return config_option \ No newline at end of file diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 04112258..363eedbb 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -2884,6 +2884,7 @@ class WebInterface(object): 10 # Email 16 # Facebook 0 # Growl + 19 # Hipchat 12 # IFTTT 18 # Join 4 # NotifyMyAndroid From e5bcd1f94e5ad6b960e6c0b8647d044644f071fa Mon Sep 17 00:00:00 2001 From: aboron Date: Fri, 8 Jul 2016 09:36:45 -0400 Subject: [PATCH 2/6] Made notification subject optional per single-line standard. Added option to include custom (plex) emoticon at the beginning of each notification. (Feature only relevant to Hipchat.) --- plexpy/config.py | 36 +++++++++++++++++++----------------- plexpy/notifiers.py | 24 ++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/plexpy/config.py b/plexpy/config.py index 7afbee09..027965ff 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -212,6 +212,25 @@ _CONFIG_DEFINITIONS = { 'HTTP_PROXY': (int, 'General', 0), 'HTTP_ROOT': (str, 'General', ''), 'HTTP_USERNAME': (str, 'General', ''), + 'HIPCHAT_URL': (str, 'Hipchat', ''), + 'HIPCHAT_COLOR': (str, 'Hipchat', 'green'), + 'HIPCHAT_INCL_SUBJECT': (int, 'Hipchat', 1), + 'HIPCHAT_INCL_EMOTICON': (int, 'Hipchat', 0), + 'HIPCHAT_ENABLED': (int, 'Hipchat', 0), + 'HIPCHAT_ON_PLAY': (int, 'Hipchat', 0), + 'HIPCHAT_ON_STOP': (int, 'Hipchat', 0), + 'HIPCHAT_ON_PAUSE': (int, 'Hipchat', 0), + 'HIPCHAT_ON_RESUME': (int, 'Hipchat', 0), + 'HIPCHAT_ON_BUFFER': (int, 'Hipchat', 0), + 'HIPCHAT_ON_WATCHED': (int, 'Hipchat', 0), + 'HIPCHAT_ON_CREATED': (int, 'Hipchat', 0), + 'HIPCHAT_ON_EXTDOWN': (int, 'Hipchat', 0), + 'HIPCHAT_ON_INTDOWN': (int, 'Hipchat', 0), + 'HIPCHAT_ON_EXTUP': (int, 'Hipchat', 0), + 'HIPCHAT_ON_INTUP': (int, 'Hipchat', 0), + 'HIPCHAT_ON_PMSUPDATE': (int, 'Hipchat', 0), + 'HIPCHAT_ON_CONCURRENT': (int, 'Hipchat', 0), + 'HIPCHAT_ON_NEWDEVICE': (int, 'Hipchat', 0), 'INTERFACE': (str, 'General', 'default'), 'IP_LOGGING_ENABLE': (int, 'General', 0), 'IFTTT_KEY': (str, 'IFTTT', ''), @@ -250,23 +269,6 @@ _CONFIG_DEFINITIONS = { 'JOIN_ON_PMSUPDATE': (int, 'Join', 0), 'JOIN_ON_CONCURRENT': (int, 'Join', 0), 'JOIN_ON_NEWDEVICE': (int, 'Join', 0), - 'HIPCHAT_URL': (str, 'Hipchat', ''), - 'HIPCHAT_COLOR': (str, 'Hipchat', 'green'), - 'HIPCHAT_ENABLED': (int, 'Hipchat', 0), - 'HIPCHAT_ON_PLAY': (int, 'Hipchat', 0), - 'HIPCHAT_ON_STOP': (int, 'Hipchat', 0), - 'HIPCHAT_ON_PAUSE': (int, 'Hipchat', 0), - 'HIPCHAT_ON_RESUME': (int, 'Hipchat', 0), - 'HIPCHAT_ON_BUFFER': (int, 'Hipchat', 0), - 'HIPCHAT_ON_WATCHED': (int, 'Hipchat', 0), - 'HIPCHAT_ON_CREATED': (int, 'Hipchat', 0), - 'HIPCHAT_ON_EXTDOWN': (int, 'Hipchat', 0), - 'HIPCHAT_ON_INTDOWN': (int, 'Hipchat', 0), - 'HIPCHAT_ON_EXTUP': (int, 'Hipchat', 0), - 'HIPCHAT_ON_INTUP': (int, 'Hipchat', 0), - 'HIPCHAT_ON_PMSUPDATE': (int, 'Hipchat', 0), - 'HIPCHAT_ON_CONCURRENT': (int, 'Hipchat', 0), - 'HIPCHAT_ON_NEWDEVICE': (int, 'Hipchat', 0), 'JOURNAL_MODE': (str, 'Advanced', 'wal'), 'LAUNCH_BROWSER': (int, 'General', 1), 'LOG_BLACKLIST': (int, 'General', 1), diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 8c513ae7..d8064f42 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -2754,12 +2754,20 @@ class HIPCHAT(object): def __init__(self): self.apiurl = plexpy.CONFIG.HIPCHAT_URL self.color = plexpy.CONFIG.HIPCHAT_COLOR + self.incl_subject = plexpy.CONFIG.HIPCHAT_INCL_SUBJECT + self.incl_emoticon = plexpy.CONFIG.HIPCHAT_INCL_EMOTICON def notify(self, message, subject): if not message or not subject: return - text = '(plex) ' + subject.encode('utf-8') + ': ' + message.encode('utf-8') + if self.incl_subject: + text = subject.encode('utf-8') + ': ' + message.encode('utf-8') + else: + text = message.encode('utf-8') + + if self.incl_emoticon: + text = '(plex) ' + text data = {'color': self.color, 'message': text, @@ -2811,8 +2819,20 @@ class HIPCHAT(object): 'description': 'Color for the message to show up in your room. You' ' may use any valid Hipchat message color value.', 'input_type': 'text' + }, + {'label': 'Include Subject Line', + 'value': self.incl_subject, + 'name': 'hipchat_incl_subject', + 'description': 'Include the subject line with the notifications.', + 'input_type': 'checkbox' + }, + {'label': 'Include (plex) emoticon', + 'value': self.incl_emoticon, + 'name': 'hipchat_incl_emoticon', + 'description': 'Include (plex) emoticon tag at the beginning of all notifications.' + ' Create a custom emoticon here.', + 'input_type': 'checkbox' } - ] return config_option \ No newline at end of file From b3cfcf660ea62aeaab77e71b9fa688dd419e8425 Mon Sep 17 00:00:00 2001 From: aboron Date: Fri, 8 Jul 2016 11:37:49 -0400 Subject: [PATCH 3/6] Changed Hipchat emoticon checkbox to user definable string, since (plex) was a custom user emoticon. --- plexpy/config.py | 2 +- plexpy/notifiers.py | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/plexpy/config.py b/plexpy/config.py index 027965ff..1b075a99 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -215,7 +215,7 @@ _CONFIG_DEFINITIONS = { 'HIPCHAT_URL': (str, 'Hipchat', ''), 'HIPCHAT_COLOR': (str, 'Hipchat', 'green'), 'HIPCHAT_INCL_SUBJECT': (int, 'Hipchat', 1), - 'HIPCHAT_INCL_EMOTICON': (int, 'Hipchat', 0), + 'HIPCHAT_INCL_EMOTICON': (str, 'Hipchat', ''), 'HIPCHAT_ENABLED': (int, 'Hipchat', 0), 'HIPCHAT_ON_PLAY': (int, 'Hipchat', 0), 'HIPCHAT_ON_STOP': (int, 'Hipchat', 0), diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index d8064f42..0c5f4346 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -2767,7 +2767,7 @@ class HIPCHAT(object): text = message.encode('utf-8') if self.incl_emoticon: - text = '(plex) ' + text + text = self.incl_emoticon + ' ' + text data = {'color': self.color, 'message': text, @@ -2820,18 +2820,19 @@ class HIPCHAT(object): ' may use any valid Hipchat message color value.', 'input_type': 'text' }, + {'label': 'Prefix emoticon', + 'value': self.incl_emoticon, + 'name': 'hipchat_incl_emoticon', + 'description': 'Include an emoticon tag at the beginning of all notifications.' + ' Use a stock emoticon or create a custom emoticon' + ' here.', + 'input_type': 'text' + }, {'label': 'Include Subject Line', 'value': self.incl_subject, 'name': 'hipchat_incl_subject', 'description': 'Include the subject line with the notifications.', 'input_type': 'checkbox' - }, - {'label': 'Include (plex) emoticon', - 'value': self.incl_emoticon, - 'name': 'hipchat_incl_emoticon', - 'description': 'Include (plex) emoticon tag at the beginning of all notifications.' - ' Create a custom emoticon here.', - 'input_type': 'checkbox' } ] From 9b9e31f54c76581592e4c429076a64c3252137e3 Mon Sep 17 00:00:00 2001 From: aboron Date: Fri, 8 Jul 2016 11:44:14 -0400 Subject: [PATCH 4/6] Improved configuration help text wording. --- plexpy/notifiers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 0c5f4346..b44110de 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -2820,10 +2820,10 @@ class HIPCHAT(object): ' may use any valid Hipchat message color value.', 'input_type': 'text' }, - {'label': 'Prefix emoticon', + {'label': 'Hipchat emoticon', 'value': self.incl_emoticon, 'name': 'hipchat_incl_emoticon', - 'description': 'Include an emoticon tag at the beginning of all notifications.' + 'description': 'Include an emoticon tag at the beginning of all notifications. Leave blank for none.' ' Use a stock emoticon or create a custom emoticon' ' here.', 'input_type': 'text' From ed2f87f57b96d85c8d95548a30ddcfdeb365e834 Mon Sep 17 00:00:00 2001 From: aboron Date: Fri, 8 Jul 2016 15:45:43 -0400 Subject: [PATCH 5/6] Added all available color options into a selection list. --- plexpy/config.py | 2 +- plexpy/notifiers.py | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/plexpy/config.py b/plexpy/config.py index 1b075a99..ef71cd1e 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -215,7 +215,7 @@ _CONFIG_DEFINITIONS = { 'HIPCHAT_URL': (str, 'Hipchat', ''), 'HIPCHAT_COLOR': (str, 'Hipchat', 'green'), 'HIPCHAT_INCL_SUBJECT': (int, 'Hipchat', 1), - 'HIPCHAT_INCL_EMOTICON': (str, 'Hipchat', ''), + 'HIPCHAT_EMOTICON': (str, 'Hipchat', ''), 'HIPCHAT_ENABLED': (int, 'Hipchat', 0), 'HIPCHAT_ON_PLAY': (int, 'Hipchat', 0), 'HIPCHAT_ON_STOP': (int, 'Hipchat', 0), diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index b44110de..6a6c9efb 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -2755,7 +2755,7 @@ class HIPCHAT(object): self.apiurl = plexpy.CONFIG.HIPCHAT_URL self.color = plexpy.CONFIG.HIPCHAT_COLOR self.incl_subject = plexpy.CONFIG.HIPCHAT_INCL_SUBJECT - self.incl_emoticon = plexpy.CONFIG.HIPCHAT_INCL_EMOTICON + self.emoticon = plexpy.CONFIG.HIPCHAT_EMOTICON def notify(self, message, subject): if not message or not subject: @@ -2766,8 +2766,8 @@ class HIPCHAT(object): else: text = message.encode('utf-8') - if self.incl_emoticon: - text = self.incl_emoticon + ' ' + text + if self.emoticon: + text = self.emoticon + ' ' + text data = {'color': self.color, 'message': text, @@ -2797,11 +2797,13 @@ class HIPCHAT(object): logger.warn(u"PlexPy Notifiers :: Hipchat notification failed.") return False - def test(self, apiurl, color): + def test(self, apiurl, color, hipchat_emoticon, hipchat_incl_subject): self.enabled = True self.apiurl = apiurl self.color = color + self.emoticon = hipchat_emoticon + self.incl_subject = hipchat_incl_subject return self.notify('PlexPy', 'Test Message') @@ -2816,13 +2818,13 @@ class HIPCHAT(object): {'label': 'Hipchat Color', 'value': self.color, 'name': 'hipchat_color', - 'description': 'Color for the message to show up in your room. You' - ' may use any valid Hipchat message color value.', - 'input_type': 'text' + 'description': 'Background color for the message.', + 'input_type': 'select', + 'select_options': {'yellow': 'yellow', 'green': 'green', 'red': 'red', 'purple': 'purple', 'gray': 'gray', 'random': 'random'} }, {'label': 'Hipchat emoticon', - 'value': self.incl_emoticon, - 'name': 'hipchat_incl_emoticon', + 'value': self.emoticon, + 'name': 'hipchat_emoticon', 'description': 'Include an emoticon tag at the beginning of all notifications. Leave blank for none.' ' Use a stock emoticon or create a custom emoticon' ' here.', From f040d897a7fa1b13450928d425ccbc2ff0b3b7e5 Mon Sep 17 00:00:00 2001 From: aboron Date: Fri, 8 Jul 2016 16:24:26 -0400 Subject: [PATCH 6/6] Modified message color default behavior. --- plexpy/config.py | 2 +- plexpy/notifiers.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/plexpy/config.py b/plexpy/config.py index ef71cd1e..5c5ade78 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -213,7 +213,7 @@ _CONFIG_DEFINITIONS = { 'HTTP_ROOT': (str, 'General', ''), 'HTTP_USERNAME': (str, 'General', ''), 'HIPCHAT_URL': (str, 'Hipchat', ''), - 'HIPCHAT_COLOR': (str, 'Hipchat', 'green'), + 'HIPCHAT_COLOR': (str, 'Hipchat', ''), 'HIPCHAT_INCL_SUBJECT': (int, 'Hipchat', 1), 'HIPCHAT_EMOTICON': (str, 'Hipchat', ''), 'HIPCHAT_ENABLED': (int, 'Hipchat', 0), diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 6a6c9efb..97413cb2 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -2769,11 +2769,13 @@ class HIPCHAT(object): if self.emoticon: text = self.emoticon + ' ' + text - data = {'color': self.color, - 'message': text, + data = {'message': text, 'notify': 'false', 'message_format': 'text'} + if self.color: + data['color'] = self.color + hiphost = urlparse(self.apiurl).hostname hippath = urlparse(self.apiurl).path hipquery = urlparse(self.apiurl).query