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