Add in IFTTT notifiers

This commit is contained in:
rossdargan 2015-10-12 11:32:59 +01:00
commit 99b052dd87
2 changed files with 104 additions and 16 deletions

View file

@ -1,4 +1,4 @@
import plexpy.logger import plexpy.logger
import itertools import itertools
import os import os
import re import re
@ -181,11 +181,19 @@ _CONFIG_DEFINITIONS = {
'PUSHBULLET_ON_RESUME': (int, 'PushBullet', 0), 'PUSHBULLET_ON_RESUME': (int, 'PushBullet', 0),
'PUSHBULLET_ON_BUFFER': (int, 'PushBullet', 0), 'PUSHBULLET_ON_BUFFER': (int, 'PushBullet', 0),
'PUSHBULLET_ON_WATCHED': (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_APITOKEN': (str, 'Pushover', ''),
'PUSHOVER_ENABLED': (int, 'Pushover', 0), 'PUSHOVER_ENABLED': (int, 'Pushover', 0),
'PUSHOVER_KEYS': (str, 'Pushover', ''), 'PUSHOVER_KEYS': (str, 'Pushover', ''),
'PUSHOVER_PRIORITY': (int, 'Pushover', 0), 'PUSHOVER_PRIORITY': (int, 'Pushover', 0),
'PUSHOVER_SOUND': (str, 'Pushover', ''),
'PUSHOVER_ON_PLAY': (int, 'Pushover', 0), 'PUSHOVER_ON_PLAY': (int, 'Pushover', 0),
'PUSHOVER_ON_STOP': (int, 'Pushover', 0), 'PUSHOVER_ON_STOP': (int, 'Pushover', 0),
'PUSHOVER_ON_PAUSE': (int, 'Pushover', 0), 'PUSHOVER_ON_PAUSE': (int, 'Pushover', 0),

View file

@ -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 # PlexPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -31,6 +31,7 @@ import os.path
import subprocess import subprocess
import gntp.notifier import gntp.notifier
import json import json
import ssl
import oauth2 as oauth import oauth2 as oauth
import pythontwitter as twitter import pythontwitter as twitter
@ -50,7 +51,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 +186,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 +259,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 +307,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:
@ -845,7 +865,6 @@ class PUSHOVER(object):
self.enabled = plexpy.CONFIG.PUSHOVER_ENABLED self.enabled = plexpy.CONFIG.PUSHOVER_ENABLED
self.keys = plexpy.CONFIG.PUSHOVER_KEYS self.keys = plexpy.CONFIG.PUSHOVER_KEYS
self.priority = plexpy.CONFIG.PUSHOVER_PRIORITY self.priority = plexpy.CONFIG.PUSHOVER_PRIORITY
self.sound = plexpy.CONFIG.PUSHOVER_SOUND
self.on_play = plexpy.CONFIG.PUSHOVER_ON_PLAY self.on_play = plexpy.CONFIG.PUSHOVER_ON_PLAY
self.on_stop = plexpy.CONFIG.PUSHOVER_ON_STOP self.on_stop = plexpy.CONFIG.PUSHOVER_ON_STOP
self.on_watched = plexpy.CONFIG.PUSHOVER_ON_WATCHED self.on_watched = plexpy.CONFIG.PUSHOVER_ON_WATCHED
@ -868,7 +887,6 @@ class PUSHOVER(object):
'user': plexpy.CONFIG.PUSHOVER_KEYS, 'user': plexpy.CONFIG.PUSHOVER_KEYS,
'title': event, 'title': event,
'message': message.encode("utf-8"), 'message': message.encode("utf-8"),
'sound': plexpy.CONFIG.PUSHOVER_SOUND,
'priority': plexpy.CONFIG.PUSHOVER_PRIORITY} 'priority': plexpy.CONFIG.PUSHOVER_PRIORITY}
http_handler.request("POST", http_handler.request("POST",
@ -895,11 +913,10 @@ class PUSHOVER(object):
#For uniformity reasons not removed #For uniformity reasons not removed
return return
def test(self, keys, priority, sound): def test(self, keys, priority):
self.enabled = True self.enabled = True
self.keys = keys self.keys = keys
self.priority = priority self.priority = priority
self.sound = sound
self.notify('Main Screen Activate', 'Test Message') self.notify('Main Screen Activate', 'Test Message')
@ -916,12 +933,6 @@ class PUSHOVER(object):
'description': 'Set the priority (-2,-1,0,1 or 2).', 'description': 'Set the priority (-2,-1,0,1 or 2).',
'input_type': 'number' 'input_type': 'number'
}, },
{'label': 'Sound',
'value': self.sound,
'name': 'pushover_sound',
'description': 'Set the notification sound (choose from <a href="https://pushover.net/api#sounds" target="_blank">this list</a> or leave blank for default)',
'input_type': 'text'
},
{'label': 'Pushover API Token', {'label': 'Pushover API Token',
'value': plexpy.CONFIG.PUSHOVER_APITOKEN, 'value': plexpy.CONFIG.PUSHOVER_APITOKEN,
'name': 'pushover_apitoken', 'name': 'pushover_apitoken',
@ -1261,3 +1272,72 @@ 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
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