From 6f7f472478a55b8e2066d59712e05b12863fdffb Mon Sep 17 00:00:00 2001 From: Patrick Yates Date: Sat, 8 Mar 2014 10:50:52 +1100 Subject: [PATCH 1/2] Added PushBullet notification support --- data/interfaces/default/config.html | 39 +++++++++++++++++++++ headphones/__init__.py | 17 +++++++++ headphones/notifiers.py | 54 +++++++++++++++++++++++++++++ headphones/postprocessor.py | 6 ++++ headphones/sab.py | 4 +++ headphones/webserve.py | 10 +++++- 6 files changed, 129 insertions(+), 1 deletion(-) diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index f1b4795b..5d139e40 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -692,6 +692,25 @@ + +
+

Pushbullet

+
+ +
+
+
+ +
+
+ +
+
+ +
+
+
+

Twitter

@@ -1269,6 +1288,26 @@ } }); + if ($("#pushbullet").is(":checked")) + { + $("#pushbulletoptions").show(); + } + else + { + $("#pushbulletoptions").hide(); + } + + $("#pushbullet").click(function(){ + if ($("#pushbullet").is(":checked")) + { + $("#pushbulletoptions").slideDown(); + } + else + { + $("#pushbulletoptions").slideUp(); + } + }); + if ($("#twitter").is(":checked")) { $("#twitteroptions").show(); diff --git a/headphones/__init__.py b/headphones/__init__.py index 1a594bd6..45301577 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -249,6 +249,10 @@ PUSHOVER_ENABLED = True PUSHOVER_PRIORITY = 1 PUSHOVER_KEYS = None PUSHOVER_ONSNATCH = True +PUSHBULLET_ENABLED = True +PUSHBULLET_APIKEY = None +PUSHBULLET_DEVICEID = None +PUSHBULLET_ONSNATCH = True TWITTER_ENABLED = False TWITTER_ONSNATCH = False TWITTER_USERNAME = None @@ -334,6 +338,7 @@ def initialize(): MUSIC_ENCODER, ADVANCEDENCODER, ENCODEROUTPUTFORMAT, ENCODERQUALITY, ENCODERVBRCBR, ENCODERLOSSLESS, DELETE_LOSSLESS_FILES, \ PROWL_ENABLED, PROWL_PRIORITY, PROWL_KEYS, PROWL_ONSNATCH, PUSHOVER_ENABLED, PUSHOVER_PRIORITY, PUSHOVER_KEYS, PUSHOVER_ONSNATCH, MIRRORLIST, \ TWITTER_ENABLED, TWITTER_ONSNATCH, TWITTER_USERNAME, TWITTER_PASSWORD, TWITTER_PREFIX, \ + PUSHBULLET_ENABLED, PUSHBULLET_APIKEY, PUSHBULLET_DEVICEID, PUSHBULLET_ONSNATCH, \ MIRROR, CUSTOMHOST, CUSTOMPORT, CUSTOMSLEEP, HPUSER, HPPASS, XBMC_ENABLED, XBMC_HOST, XBMC_USERNAME, XBMC_PASSWORD, XBMC_UPDATE, \ XBMC_NOTIFY, NMA_ENABLED, NMA_APIKEY, NMA_PRIORITY, NMA_ONSNATCH, SYNOINDEX_ENABLED, ALBUM_COMPLETION_PCT, PREFERRED_BITRATE_HIGH_BUFFER, \ PREFERRED_BITRATE_LOW_BUFFER, PREFERRED_BITRATE_ALLOW_LOSSLESS, CACHE_SIZEMB, JOURNAL_MODE, UMASK, ENABLE_HTTPS, HTTPS_CERT, HTTPS_KEY, \ @@ -360,6 +365,7 @@ def initialize(): CheckSection('What.cd') CheckSection('Prowl') CheckSection('Pushover') + CheckSection('PushBullet') CheckSection('XBMC') CheckSection('Plex') CheckSection('NMA') @@ -564,6 +570,11 @@ def initialize(): PUSHOVER_ONSNATCH = bool(check_setting_int(CFG, 'Pushover', 'pushover_onsnatch', 0)) PUSHOVER_PRIORITY = check_setting_int(CFG, 'Pushover', 'pushover_priority', 0) + PUSHBULLET_ENABLED = bool(check_setting_int(CFG, 'PushBullet', 'pushbullet_enabled', 0)) + PUSHBULLET_APIKEY = check_setting_str(CFG, 'PushBullet', 'pushbullet_apikey', '') + PUSHBULLET_DEVICEID = check_setting_str(CFG, 'PushBullet', 'pushbullet_deviceid', '') + PUSHBULLET_ONSNATCH = bool(check_setting_int(CFG, 'PushBullet', 'pushbullet_onsnatch', 0)) + TWITTER_ENABLED = bool(check_setting_int(CFG, 'Twitter', 'twitter_enabled', 0)) TWITTER_ONSNATCH = bool(check_setting_int(CFG, 'Twitter', 'twitter_onsnatch', 0)) TWITTER_USERNAME = check_setting_str(CFG, 'Twitter', 'twitter_username', '') @@ -951,6 +962,12 @@ def config_write(): new_config['Pushover']['pushover_onsnatch'] = int(PUSHOVER_ONSNATCH) new_config['Pushover']['pushover_priority'] = int(PUSHOVER_PRIORITY) + new_config['PushBullet'] = {} + new_config['PushBullet']['pushbullet_enabled'] = int(PUSHBULLET_ENABLED) + new_config['PushBullet']['pushbullet_apikey'] = PUSHBULLET_APIKEY + new_config['PushBullet']['pushbullet_deviceid'] = PUSHBULLET_DEVICEID + new_config['PushBullet']['pushbullet_onsnatch'] = int(PUSHBULLET_ONSNATCH) + new_config['Twitter'] = {} new_config['Twitter']['twitter_enabled'] = int(TWITTER_ENABLED) new_config['Twitter']['twitter_onsnatch'] = int(TWITTER_ONSNATCH) diff --git a/headphones/notifiers.py b/headphones/notifiers.py index a3a344a3..3da78a6e 100644 --- a/headphones/notifiers.py +++ b/headphones/notifiers.py @@ -323,6 +323,60 @@ class NMA: if not request: logger.warn('Error sending notification request to NotifyMyAndroid') + +class PUSHBULLET: + + def __init__(self): + self.apikey = headphones.PUSHBULLET_APIKEY + self.deviceid = headphones.PUSHBULLET_DEVICEID + + def conf(self, options): + return cherrypy.config['config'].get('PUSHBULLET', options) + + def notify(self, message, event): + if not headphones.PUSHBULLET_ENABLED: + return + + http_handler = HTTPSConnection("api.pushbullet.com") + + data = {'device_iden': headphones.PUSHBULLET_DEVICEID, + 'type': "note", + 'title': "Headphones", + 'body': message.encode("utf-8") } + + http_handler.request("POST", + "/api/pushes", + headers = {'Content-type': "application/x-www-form-urlencoded", + 'Authorization' : 'Basic %s' % base64.b64encode(headphones.PUSHBULLET_APIKEY + ":") }, + body = urlencode(data)) + response = http_handler.getresponse() + request_status = response.status + logger.debug(u"PushBullet response status: %r" % request_status) + logger.debug(u"PushBullet response headers: %r" % response.getheaders()) + logger.debug(u"PushBullet response body: %r" % response.read()) + + if request_status == 200: + logger.info(u"PushBullet notifications sent.") + return True + elif request_status >= 400 and request_status < 500: + logger.info(u"PushBullet request failed: %s" % response.reason) + return False + else: + logger.info(u"PushBullet notification failed serverside.") + return False + + def updateLibrary(self): + #For uniformity reasons not removed + return + + def test(self, apikey, deviceid): + + self.enabled = True + self.apikey = apikey + self.deviceid = deviceid + + self.notify('Main Screen Activate', 'Test Message') + class PUSHALOT: diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index 6261b0db..d2fcc8cc 100644 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -448,6 +448,12 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list, pushover = notifiers.PUSHOVER() pushover.notify(pushmessage,"Download and Postprocessing completed") + if headphones.PUSHBULLET_ENABLED: + pushmessage = release['ArtistName'] + ' - ' + release['AlbumTitle'] + logger.info(u"PushBullet request") + pushbullet = notifiers.PUSHBULLET() + pushbullet.notify(pushmessage, "Download and Postprocessing completed") + if headphones.TWITTER_ENABLED: pushmessage = release['ArtistName'] + ' - ' + release['AlbumTitle'] logger.info(u"Sending Twitter notification") diff --git a/headphones/sab.py b/headphones/sab.py index f7587fcd..45b494a6 100644 --- a/headphones/sab.py +++ b/headphones/sab.py @@ -127,6 +127,10 @@ def sendNZB(nzb): logger.info(u"Sending Pushover notification") prowl = notifiers.PUSHOVER() prowl.notify(nzb.name,"Download started") + if headphones.PUSHBULLET_ENABLED and headphones.PUSHBULLET_ONSNATCH: + logger.info(u"Sending PushBullet notification") + pushbullet = notifiers.PUSHBULLET() + pushbullet.notify(nzb.name + " has been snatched!", "Download started") if headphones.TWITTER_ENABLED and headphones.TWITTER_ONSNATCH: logger.info(u"Sending Twitter notification") twitter = notifiers.TwitterNotifier() diff --git a/headphones/webserve.py b/headphones/webserve.py index 65c4310f..a6bab744 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -952,6 +952,10 @@ class WebInterface(object): "pushover_onsnatch": checked(headphones.PUSHOVER_ONSNATCH), "pushover_keys": headphones.PUSHOVER_KEYS, "pushover_priority": headphones.PUSHOVER_PRIORITY, + "pushbullet_enabled": checked(headphones.PUSHBULLET_ENABLED), + "pushbullet_onsnatch": checked(headphones.PUSHBULLET_ONSNATCH), + "pushbullet_apikey": headphones.PUSHBULLET_APIKEY, + "pushbullet_deviceid": headphones.PUSHBULLET_DEVICEID, "twitter_enabled": checked(headphones.TWITTER_ENABLED), "twitter_onsnatch": checked(headphones.TWITTER_ONSNATCH), "mirror_list": headphones.MIRRORLIST, @@ -998,7 +1002,7 @@ class WebInterface(object): bitrate=None, samplingfrequency=None, encoderfolder=None, advancedencoder=None, encoderoutputformat=None, encodervbrcbr=None, encoderquality=None, encoderlossless=0, delete_lossless_files=0, prowl_enabled=0, prowl_onsnatch=0, prowl_keys=None, prowl_priority=0, xbmc_enabled=0, xbmc_host=None, xbmc_username=None, xbmc_password=None, xbmc_update=0, xbmc_notify=0, nma_enabled=False, nma_apikey=None, nma_priority=0, nma_onsnatch=0, pushalot_enabled=False, pushalot_apikey=None, pushalot_onsnatch=0, synoindex_enabled=False, - pushover_enabled=0, pushover_onsnatch=0, pushover_keys=None, pushover_priority=0, twitter_enabled=0, twitter_onsnatch=0, mirror=None, customhost=None, customport=None, + pushover_enabled=0, pushover_onsnatch=0, pushover_keys=None, pushover_priority=0, pushbullet_enabled=0, pushbullet_onsnatch=0, pushbullet_apikey=None, pushbullet_deviceid=None, twitter_enabled=0, twitter_onsnatch=0, mirror=None, customhost=None, customport=None, customsleep=None, hpuser=None, hppass=None, preferred_bitrate_high_buffer=None, preferred_bitrate_low_buffer=None, preferred_bitrate_allow_lossless=0, cache_sizemb=None, enable_https=0, https_cert=None, https_key=None, file_permissions=None, folder_permissions=None, plex_enabled=0, plex_server_host=None, plex_client_host=None, plex_username=None, plex_password=None, plex_update=0, plex_notify=0, post_processing_dir=None, songkick_apikey=None, **kwargs): @@ -1139,6 +1143,10 @@ class WebInterface(object): headphones.PUSHOVER_ONSNATCH = pushover_onsnatch headphones.PUSHOVER_KEYS = pushover_keys headphones.PUSHOVER_PRIORITY = pushover_priority + headphones.PUSHBULLET_ENABLED = pushbullet_enabled + headphones.PUSHBULLET_ONSNATCH = pushbullet_onsnatch + headphones.PUSHBULLET_APIKEY = pushbullet_apikey + headphones.PUSHBULLET_DEVICEID = pushbullet_deviceid headphones.TWITTER_ENABLED = twitter_enabled headphones.TWITTER_ONSNATCH = twitter_onsnatch headphones.MIRROR = mirror From 8ed040a24ee7eddb0e796a6b58d5e52536aa33ff Mon Sep 17 00:00:00 2001 From: Patrick Yates Date: Sat, 8 Mar 2014 18:55:58 +1100 Subject: [PATCH 2/2] More descriptive api key label --- data/interfaces/default/config.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index 5d139e40..7417aab3 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -700,7 +700,7 @@
- +