diff --git a/data/interfaces/default/notification_config.html b/data/interfaces/default/notification_config.html index 41fcfa93..7d420259 100644 --- a/data/interfaces/default/notification_config.html +++ b/data/interfaces/default/notification_config.html @@ -121,6 +121,12 @@ from plexpy import helpers $('#ajaxMsg').addClass('success').fadeIn().delay(3000).fadeOut(); }); + $('#testIFTTT').click(function () { + $.get("/test_ifttt", + function (data) { $('#ajaxMsg').html(" " + data); }); + $('#ajaxMsg').addClass('success').fadeIn().delay(3000).fadeOut(); + }); + // Never send checkbox values directly, always substitute value in hidden input. $('.checkboxes').click(function() { var configToggle = $(this).data('id'); diff --git a/plexpy/config.py b/plexpy/config.py index f9e65086..b2cca63c 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -99,6 +99,15 @@ _CONFIG_DEFINITIONS = { 'HTTP_USERNAME': (str, 'General', ''), 'INTERFACE': (str, 'General', 'default'), 'IP_LOGGING_ENABLE': (int, 'General', 0), + 'IFTTT_KEY': (str, 'IFTTT', ''), + 'IFTTT_EVENT': (str, 'IFTTT', 'plexpy'), + 'IFTTT_ENABLED': (int, 'IFTTT', 0), + 'IFTTT_ON_PLAY': (int, 'IFTTT', 0), + 'IFTTT_ON_STOP': (int, 'IFTTT', 0), + 'IFTTT_ON_PAUSE': (int, 'IFTTT', 0), + 'IFTTT_ON_RESUME': (int, 'IFTTT', 0), + 'IFTTT_ON_BUFFER': (int, 'IFTTT', 0), + 'IFTTT_ON_WATCHED': (int, 'IFTTT', 0), 'JOURNAL_MODE': (str, 'Advanced', 'wal'), 'LAUNCH_BROWSER': (int, 'General', 1), 'LOG_DIR': (str, 'General', ''), diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 6f3e879b..cbbe8401 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -50,7 +50,8 @@ AGENT_IDS = {"Growl": 0, "OSX Notify": 8, "Boxcar2": 9, "Email": 10, - "Twitter": 11} + "Twitter": 11, + "IFTTT": 12} def available_notification_agents(): agents = [{'name': 'Growl', @@ -184,6 +185,18 @@ def available_notification_agents(): 'on_resume': plexpy.CONFIG.TWITTER_ON_RESUME, 'on_buffer': plexpy.CONFIG.TWITTER_ON_BUFFER, 'on_watched': plexpy.CONFIG.TWITTER_ON_WATCHED + }, + {'name': 'IFTTT', + 'id': AGENT_IDS['IFTTT'], + 'config_prefix': 'ifttt', + 'has_config': True, + 'state': checked(plexpy.CONFIG.IFTTT_ENABLED), + 'on_play': plexpy.CONFIG.IFTTT_ON_PLAY, + 'on_stop': plexpy.CONFIG.IFTTT_ON_STOP, + 'on_pause': plexpy.CONFIG.IFTTT_ON_PAUSE, + 'on_resume': plexpy.CONFIG.IFTTT_ON_RESUME, + 'on_buffer': plexpy.CONFIG.IFTTT_ON_BUFFER, + 'on_watched': plexpy.CONFIG.IFTTT_ON_WATCHED } ] @@ -245,6 +258,9 @@ def get_notification_agent_config(config_id): elif config_id == 11: tweet = TwitterNotifier() return tweet.return_config_options() + elif config_id == 12: + iftttClient = IFTTT() + return iftttClient.return_config_options() else: return [] else: @@ -290,6 +306,9 @@ def send_notification(config_id, subject, body): elif config_id == 11: tweet = TwitterNotifier() tweet.notify(subject=subject, message=body) + elif config_id == 12: + iftttClient = IFTTT() + iftttClient.notify(subject=subject, message=body) else: logger.debug(u"PlexPy Notifier :: Unknown agent id received.") else: @@ -1319,4 +1338,69 @@ class Email(object): } ] - return config_option \ No newline at end of file + return config_option + + +class IFTTT(object): + + def __init__(self): + self.apikey = plexpy.CONFIG.IFTTT_KEY + self.event = plexpy.CONFIG.IFTTT_EVENT + + def notify(self, message, subject): + if not message or not subject: + return + + http_handler = HTTPSConnection("maker.ifttt.com") + + data = {'value1': subject.encode("utf-8"), + 'value2': message.encode("utf-8")} + + # logger.debug("Ifttt SENDING: %s" % json.dumps(data)) + + http_handler.request("POST", + "/trigger/%s/with/key/%s" % (self.event, self.apikey), + headers={'Content-type': "application/json"}, + body=json.dumps(data)) + response = http_handler.getresponse() + request_status = response.status + # logger.debug(u"Ifttt response status: %r" % request_status) + # logger.debug(u"Ifttt response headers: %r" % response.getheaders()) + # logger.debug(u"Ifttt response body: %r" % response.read()) + + if request_status == 200: + logger.info(u"Ifttt notifications sent.") + return True + elif request_status >= 400 and request_status < 500: + logger.info(u"Ifttt request failed: %s" % response.reason) + return False + else: + logger.info(u"Ifttt notification failed serverside.") + return False + + def test(self): + return self.notify('PlexPy', 'Test Message') + + def return_config_options(self): + config_option = [{'label': 'Ifttt Maker Channel Key', + 'value': self.apikey, + 'name': 'ifttt_key', + 'description': 'Your Ifttt key. You can get a key from here https://ifttt.com/maker', + 'input_type': 'text' + }, + {'label': 'Ifttt event', + 'value': self.event, + 'name': 'ifttt_event', + 'description': 'The Ifttt maker event to fire. The notification subject and body will be sent' + ' as value1 and value2 respectively.', + 'input_type': 'text' + }, + {'label': 'Test Event', + 'value': 'Test Event', + 'name': 'testIFTTT', + 'description': 'Test if IFTTT notifications are working. See logs for troubleshooting.', + 'input_type': 'button' + } + ] + + return config_option diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 3549089b..e33f9207 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -653,6 +653,16 @@ class WebInterface(object): else: return "Error sending tweet" + @cherrypy.expose + def test_ifttt(self): + cherrypy.response.headers['Cache-Control'] = "max-age=0,no-cache,no-store" + event = notifiers.IFTTT() + result = event.test() + if result: + return "Notification successful." + else: + return "Error sending event." + @cherrypy.expose def osxnotifyregister(self, app): cherrypy.response.headers['Cache-Control'] = "max-age=0,no-cache,no-store"