Implement IFTTT notification option. PR #241.

This commit is contained in:
Tim van der Westhuizen 2015-10-22 16:04:31 +02:00
parent dc8996c4d2
commit 37bc68573c
4 changed files with 111 additions and 2 deletions

View file

@ -121,6 +121,12 @@ from plexpy import helpers
$('#ajaxMsg').addClass('success').fadeIn().delay(3000).fadeOut(); $('#ajaxMsg').addClass('success').fadeIn().delay(3000).fadeOut();
}); });
$('#testIFTTT').click(function () {
$.get("/test_ifttt",
function (data) { $('#ajaxMsg').html("<i class='fa fa-check'></i> " + data); });
$('#ajaxMsg').addClass('success').fadeIn().delay(3000).fadeOut();
});
// Never send checkbox values directly, always substitute value in hidden input. // Never send checkbox values directly, always substitute value in hidden input.
$('.checkboxes').click(function() { $('.checkboxes').click(function() {
var configToggle = $(this).data('id'); var configToggle = $(this).data('id');

View file

@ -99,6 +99,15 @@ _CONFIG_DEFINITIONS = {
'HTTP_USERNAME': (str, 'General', ''), 'HTTP_USERNAME': (str, 'General', ''),
'INTERFACE': (str, 'General', 'default'), 'INTERFACE': (str, 'General', 'default'),
'IP_LOGGING_ENABLE': (int, 'General', 0), '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'), 'JOURNAL_MODE': (str, 'Advanced', 'wal'),
'LAUNCH_BROWSER': (int, 'General', 1), 'LAUNCH_BROWSER': (int, 'General', 1),
'LOG_DIR': (str, 'General', ''), 'LOG_DIR': (str, 'General', ''),

View file

@ -50,7 +50,8 @@ AGENT_IDS = {"Growl": 0,
"OSX Notify": 8, "OSX Notify": 8,
"Boxcar2": 9, "Boxcar2": 9,
"Email": 10, "Email": 10,
"Twitter": 11} "Twitter": 11,
"IFTTT": 12}
def available_notification_agents(): def available_notification_agents():
agents = [{'name': 'Growl', agents = [{'name': 'Growl',
@ -184,6 +185,18 @@ def available_notification_agents():
'on_resume': plexpy.CONFIG.TWITTER_ON_RESUME, 'on_resume': plexpy.CONFIG.TWITTER_ON_RESUME,
'on_buffer': plexpy.CONFIG.TWITTER_ON_BUFFER, 'on_buffer': plexpy.CONFIG.TWITTER_ON_BUFFER,
'on_watched': plexpy.CONFIG.TWITTER_ON_WATCHED '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: elif config_id == 11:
tweet = TwitterNotifier() tweet = TwitterNotifier()
return tweet.return_config_options() return tweet.return_config_options()
elif config_id == 12:
iftttClient = IFTTT()
return iftttClient.return_config_options()
else: else:
return [] return []
else: else:
@ -290,6 +306,9 @@ def send_notification(config_id, subject, body):
elif config_id == 11: elif config_id == 11:
tweet = TwitterNotifier() tweet = TwitterNotifier()
tweet.notify(subject=subject, message=body) tweet.notify(subject=subject, message=body)
elif config_id == 12:
iftttClient = IFTTT()
iftttClient.notify(subject=subject, message=body)
else: else:
logger.debug(u"PlexPy Notifier :: Unknown agent id received.") logger.debug(u"PlexPy Notifier :: Unknown agent id received.")
else: else:
@ -1319,4 +1338,69 @@ class Email(object):
} }
] ]
return config_option 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

View file

@ -653,6 +653,16 @@ class WebInterface(object):
else: else:
return "Error sending tweet" 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 @cherrypy.expose
def osxnotifyregister(self, app): def osxnotifyregister(self, app):
cherrypy.response.headers['Cache-Control'] = "max-age=0,no-cache,no-store" cherrypy.response.headers['Cache-Control'] = "max-age=0,no-cache,no-store"