diff --git a/plexpy/config.py b/plexpy/config.py index 5716cd84..3c08a463 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -1,4 +1,4 @@ -import plexpy.logger +import plexpy.logger import itertools import os import re @@ -181,11 +181,19 @@ _CONFIG_DEFINITIONS = { 'PUSHBULLET_ON_RESUME': (int, 'PushBullet', 0), 'PUSHBULLET_ON_BUFFER': (int, 'PushBullet', 0), 'PUSHBULLET_ON_WATCHED': (int, 'PushBullet', 0), + 'IFTTT_KEY': (str, 'Ifttt', ''), + 'IFTTT_EVENT': (str, 'Ifttt', 'plextv'), + '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), 'PUSHOVER_APITOKEN': (str, 'Pushover', ''), 'PUSHOVER_ENABLED': (int, 'Pushover', 0), 'PUSHOVER_KEYS': (str, 'Pushover', ''), 'PUSHOVER_PRIORITY': (int, 'Pushover', 0), - 'PUSHOVER_SOUND': (str, 'Pushover', ''), 'PUSHOVER_ON_PLAY': (int, 'Pushover', 0), 'PUSHOVER_ON_STOP': (int, 'Pushover', 0), 'PUSHOVER_ON_PAUSE': (int, 'Pushover', 0), @@ -321,4 +329,4 @@ class Config(object): """ for name, value in kwargs.items(): key, definition_type, section, ini_key, default = self._define(name) - self._config[section][ini_key] = definition_type(value) \ No newline at end of file + self._config[section][ini_key] = definition_type(value) diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 8c17bc32..d00507b9 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -1,4 +1,4 @@ -# This file is part of PlexPy. +# This file is part of PlexPy. # # PlexPy is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -31,6 +31,7 @@ import os.path import subprocess import gntp.notifier import json +import ssl import oauth2 as oauth import pythontwitter as twitter @@ -50,7 +51,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 +186,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 +259,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 +307,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: @@ -845,7 +865,6 @@ class PUSHOVER(object): self.enabled = plexpy.CONFIG.PUSHOVER_ENABLED self.keys = plexpy.CONFIG.PUSHOVER_KEYS self.priority = plexpy.CONFIG.PUSHOVER_PRIORITY - self.sound = plexpy.CONFIG.PUSHOVER_SOUND self.on_play = plexpy.CONFIG.PUSHOVER_ON_PLAY self.on_stop = plexpy.CONFIG.PUSHOVER_ON_STOP self.on_watched = plexpy.CONFIG.PUSHOVER_ON_WATCHED @@ -868,7 +887,6 @@ class PUSHOVER(object): 'user': plexpy.CONFIG.PUSHOVER_KEYS, 'title': event, 'message': message.encode("utf-8"), - 'sound': plexpy.CONFIG.PUSHOVER_SOUND, 'priority': plexpy.CONFIG.PUSHOVER_PRIORITY} http_handler.request("POST", @@ -895,11 +913,10 @@ class PUSHOVER(object): #For uniformity reasons not removed return - def test(self, keys, priority, sound): + def test(self, keys, priority): self.enabled = True self.keys = keys self.priority = priority - self.sound = sound self.notify('Main Screen Activate', 'Test Message') @@ -916,12 +933,6 @@ class PUSHOVER(object): 'description': 'Set the priority (-2,-1,0,1 or 2).', 'input_type': 'number' }, - {'label': 'Sound', - 'value': self.sound, - 'name': 'pushover_sound', - 'description': 'Set the notification sound (choose from this list or leave blank for default)', - 'input_type': 'text' - }, {'label': 'Pushover API Token', 'value': plexpy.CONFIG.PUSHOVER_APITOKEN, 'name': 'pushover_apitoken', @@ -1260,4 +1271,73 @@ 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 + self.on_play = plexpy.CONFIG.IFTTT_ON_PLAY + self.on_stop = plexpy.CONFIG.IFTTT_ON_STOP + self.on_watched = plexpy.CONFIG.IFTTT_ON_WATCHED + + + def notify(self, message, subject): + if not message or not subject: + return + # This should be the contex we use, but it isn't working as it casues an SSL validation error + # Unfortunately this is beyond my phython ability! + #context = ssl.create_default_context() + context = ssl._create_unverified_context() + http_handler = HTTPSConnection("maker.ifttt.com", context=context) + + 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" % (plexpy.CONFIG.IFTTT_EVENT, plexpy.CONFIG.IFTTT_KEY), + 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, apikey, event): + + self.enabled = True + self.apikey = apikey + self.event = event + self.notify('Main Screen Activate', '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.', + 'input_type': 'text' + } + ] + + return config_option