From ca10feff954bd41221e3411a11d5156f3323b9f7 Mon Sep 17 00:00:00 2001 From: Bryce Chidester Date: Sun, 11 Nov 2012 12:15:31 -0700 Subject: [PATCH] Implement Pushover notification support. As mentioned in https://github.com/rembo10/headphones/issues/888 Only added the configuration to the default user interface. This was mostly copy/pasted from the Prowl notifications. --- data/interfaces/default/config.html | 40 +++++++++++++++++++ headphones/__init__.py | 19 +++++++++- headphones/notifiers.py | 59 +++++++++++++++++++++++++++++ headphones/postprocessor.py | 6 +++ headphones/sab.py | 4 ++ headphones/webserve.py | 11 +++++- 6 files changed, 137 insertions(+), 2 deletions(-) diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index a3f994cb..58baf946 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -715,6 +715,26 @@ +
+

Pushover

+
+ +
+
+
+ +
+
+ +
+
+ + +
+
+
+ +
Musicbrainz
@@ -928,6 +948,26 @@ $("#nmaoptions").slideUp(); } }); + if ($("#pushover").is(":checked")) + { + $("#pushoveroptions").show(); + } + else + { + $("#pushoveroptions").hide(); + } + + $("#pushover").click(function(){ + if ($("#pushover").is(":checked")) + { + $("#pushoveroptions").slideDown(); + } + else + { + $("#pushoveroptions").slideUp(); + } + }); + if ($("#preferred_bitrate").is(":checked")) { $("#preferred_bitrate_options").show(); diff --git a/headphones/__init__.py b/headphones/__init__.py index 3f27c709..2da69469 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -194,6 +194,10 @@ NMA_APIKEY = None NMA_PRIORITY = None NMA_ONSNATCH = None SYNOINDEX_ENABLED = False +PUSHOVER_ENABLED = True +PUSHOVER_PRIORITY = 1 +PUSHOVER_KEYS = None +PUSHOVER_ONSNATCH = True MIRRORLIST = ["musicbrainz.org","headphones","custom"] MIRROR = None CUSTOMHOST = None @@ -265,7 +269,8 @@ def initialize(): NZBMATRIX, NZBMATRIX_USERNAME, NZBMATRIX_APIKEY, NEWZNAB, NEWZNAB_HOST, NEWZNAB_APIKEY, NEWZNAB_ENABLED, EXTRA_NEWZNABS,\ NZBSORG, NZBSORG_UID, NZBSORG_HASH, NEWZBIN, NEWZBIN_UID, NEWZBIN_PASSWORD, LASTFM_USERNAME, INTERFACE, FOLDER_PERMISSIONS, \ ENCODERFOLDER, ENCODER, XLDPROFILE, BITRATE, SAMPLINGFREQUENCY, MUSIC_ENCODER, ADVANCEDENCODER, ENCODEROUTPUTFORMAT, ENCODERQUALITY, \ - ENCODERVBRCBR, ENCODERLOSSLESS, DELETE_LOSSLESS_FILES, PROWL_ENABLED, PROWL_PRIORITY, PROWL_KEYS, PROWL_ONSNATCH, MIRRORLIST, \ + ENCODERVBRCBR, ENCODERLOSSLESS, DELETE_LOSSLESS_FILES, PROWL_ENABLED, PROWL_PRIORITY, PROWL_KEYS, PROWL_ONSNATCH, \ + PUSHOVER_ENABLED, PUSHOVER_PRIORITY, PUSHOVER_KEYS, PUSHOVER_ONSNATCH, MIRRORLIST, \ 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,CACHE_SIZEMB @@ -284,6 +289,7 @@ def initialize(): CheckSection('Rutracker') CheckSection('What.cd') CheckSection('Prowl') + CheckSection('Pushover') CheckSection('XBMC') CheckSection('NMA') CheckSection('Synoindex') @@ -431,6 +437,11 @@ def initialize(): SYNOINDEX_ENABLED = bool(check_setting_int(CFG, 'Synoindex', 'synoindex_enabled', 0)) + PUSHOVER_ENABLED = bool(check_setting_int(CFG, 'Pushover', 'pushover_enabled', 0)) + PUSHOVER_KEYS = check_setting_str(CFG, 'Pushover', 'pushover_keys', '') + PUSHOVER_ONSNATCH = bool(check_setting_int(CFG, 'Pushover', 'pushover_onsnatch', 0)) + PUSHOVER_PRIORITY = check_setting_int(CFG, 'Pushover', 'pushover_priority', 0) + MIRROR = check_setting_str(CFG, 'General', 'mirror', 'musicbrainz.org') CUSTOMHOST = check_setting_str(CFG, 'General', 'customhost', 'localhost') CUSTOMPORT = check_setting_int(CFG, 'General', 'customport', 5000) @@ -720,6 +731,12 @@ def config_write(): new_config['NMA']['nma_priority'] = NMA_PRIORITY new_config['NMA']['nma_onsnatch'] = int(PROWL_ONSNATCH) + new_config['Pushover'] = {} + new_config['Pushover']['pushover_enabled'] = int(PUSHOVER_ENABLED) + new_config['Pushover']['pushover_keys'] = PUSHOVER_KEYS + new_config['Pushover']['pushover_onsnatch'] = int(PUSHOVER_ONSNATCH) + new_config['Pushover']['pushover_priority'] = int(PUSHOVER_PRIORITY) + new_config['Synoindex'] = {} new_config['Synoindex']['synoindex_enabled'] = int(SYNOINDEX_ENABLED) diff --git a/headphones/notifiers.py b/headphones/notifiers.py index 3df88485..1652399d 100644 --- a/headphones/notifiers.py +++ b/headphones/notifiers.py @@ -227,3 +227,62 @@ class Synoindex: if isinstance(path_list, list): for path in path_list: self.notify(path) +class PUSHOVER: + + application_token = "LdPCoy0dqC21ktsbEyAVCcwvQiVlsz" + keys = [] + priority = [] + + def __init__(self): + self.enabled = headphones.PUSHOVER_ENABLED + self.keys = headphones.PUSHOVER_KEYS + self.priority = headphones.PUSHOVER_PRIORITY + pass + + def conf(self, options): + return cherrypy.config['config'].get('Pushover', options) + + def notify(self, message, event): + if not headphones.PUSHOVER_ENABLED: + return + + http_handler = HTTPSConnection("api.pushover.net") + + data = {'token': self.application_token, + 'user': headphones.PUSHOVER_KEYS, + 'title': event, + 'message': message.encode("utf-8"), + 'priority': headphones.PUSHOVER_PRIORITY } + + http_handler.request("POST", + "/1/messages.json", + headers = {'Content-type': "application/x-www-form-urlencoded"}, + body = urlencode(data)) + response = http_handler.getresponse() + request_status = response.status + logger.debug(u"Pushover response status: %r" % request_status) + logger.debug(u"Pushover response headers: %r" % response.getheaders()) + logger.debug(u"Pushover response body: %r" % response.read()) + + if request_status == 200: + logger.info(u"Pushover notifications sent.") + return True + elif request_status >= 400 and request_status < 500: + logger.info(u"Pushover request failed: %s" % response.reason) + return False + else: + logger.info(u"Pushover notification failed.") + return False + + def updateLibrary(self): + #For uniformity reasons not removed + return + + def test(self, keys, priority): + + self.enabled = True + self.keys = keys + self.priority = priority + + self.notify('Main Screen Activate', 'Test Message') + diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index 1cc07a7f..dfacb9ff 100644 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -395,6 +395,12 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list) for albumpath in albumpaths: syno.notify(albumpath) + if headphones.PUSHOVER_ENABLED: + pushmessage = release['ArtistName'] + ' - ' + release['AlbumTitle'] + logger.info(u"Pushover request") + pushover = notifiers.PUSHOVER() + pushover.notify(pushmessage,"Download and Postprocessing completed") + def embedAlbumArt(artwork, downloaded_track_list): logger.info('Embedding album art') diff --git a/headphones/sab.py b/headphones/sab.py index 9b0e2bd1..f3c7fcd3 100644 --- a/headphones/sab.py +++ b/headphones/sab.py @@ -128,6 +128,10 @@ def sendNZB(nzb): logger.info(u"Sending Prowl notification") prowl = notifiers.PROWL() prowl.notify(nzb.name,"Download started") + if headphones.PUSHOVER_ENABLED and headphones.PUSHOVER_ONSNATCH: + logger.info(u"Sending Pushover notification") + prowl = notifiers.PUSHOVER() + prowl.notify(nzb.name,"Download started") if headphones.NMA_ENABLED and headphones.NMA_ONSNATCH: logger.debug(u"Sending NMA notification") nma = notifiers.NMA() diff --git a/headphones/webserve.py b/headphones/webserve.py index 08d1863f..4e04246f 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -649,6 +649,10 @@ class WebInterface(object): "nma_priority": int(headphones.NMA_PRIORITY), "nma_onsnatch": checked(headphones.NMA_ONSNATCH), "synoindex_enabled": checked(headphones.SYNOINDEX_ENABLED), + "pushover_enabled": checked(headphones.PUSHOVER_ENABLED), + "pushover_onsnatch": checked(headphones.PUSHOVER_ONSNATCH), + "pushover_keys": headphones.PUSHOVER_KEYS, + "pushover_priority": headphones.PUSHOVER_PRIORITY, "mirror_list": headphones.MIRRORLIST, "mirror": headphones.MIRROR, "customhost": headphones.CUSTOMHOST, @@ -688,7 +692,8 @@ class WebInterface(object): remix=0, spokenword=0, audiobook=0, autowant_upcoming=False, autowant_all=False, interface=None, log_dir=None, cache_dir=None, music_encoder=0, encoder=None, xldprofile=None, 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, synoindex_enabled=False, mirror=None, customhost=None, customport=None, + xbmc_update=0, xbmc_notify=0, nma_enabled=False, nma_apikey=None, nma_priority=0, nma_onsnatch=0, synoindex_enabled=False, + pushover_enabled=0, pushover_onsnatch=0, pushover_keys=None, pushover_priority=0, mirror=None, customhost=None, customport=None, customsleep=None, hpuser=None, hppass=None, preferred_bitrate_high_buffer=None, preferred_bitrate_low_buffer=None, cache_sizemb=None, **kwargs): headphones.HTTP_HOST = http_host @@ -787,6 +792,10 @@ class WebInterface(object): headphones.NMA_PRIORITY = nma_priority headphones.NMA_ONSNATCH = nma_onsnatch headphones.SYNOINDEX_ENABLED = synoindex_enabled + headphones.PUSHOVER_ENABLED = pushover_enabled + headphones.PUSHOVER_ONSNATCH = pushover_onsnatch + headphones.PUSHOVER_KEYS = pushover_keys + headphones.PUSHOVER_PRIORITY = pushover_priority headphones.MIRROR = mirror headphones.CUSTOMHOST = customhost headphones.CUSTOMPORT = customport