From 9da75a777b59092c92bc1ab174f54da6e3dc1cdf Mon Sep 17 00:00:00 2001 From: delphiactual Date: Tue, 6 May 2014 20:22:13 -0600 Subject: [PATCH] NMA fix --- headphones/__init__.py | 4 ++-- headphones/notifiers.py | 52 +++++++++++++++++++++++------------------ headphones/searcher.py | 30 ++++++++++++++++++++++-- headphones/utorrent.py | 3 +-- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/headphones/__init__.py b/headphones/__init__.py index 380a31bf..9aa42ba1 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -248,7 +248,7 @@ PLEX_UPDATE = False PLEX_NOTIFY = False NMA_ENABLED = False NMA_APIKEY = None -NMA_PRIORITY = None +NMA_PRIORITY = 0 NMA_ONSNATCH = None PUSHALOT_ENABLED = False PUSHALOT_APIKEY = None @@ -1014,7 +1014,7 @@ def config_write(): new_config['NMA'] = {} new_config['NMA']['nma_enabled'] = int(NMA_ENABLED) new_config['NMA']['nma_apikey'] = NMA_APIKEY - new_config['NMA']['nma_priority'] = NMA_PRIORITY + new_config['NMA']['nma_priority'] = int(NMA_PRIORITY) new_config['NMA']['nma_onsnatch'] = int(NMA_ONSNATCH) new_config['Pushalot'] = {} diff --git a/headphones/notifiers.py b/headphones/notifiers.py index f5e4ff12..4bd7c366 100644 --- a/headphones/notifiers.py +++ b/headphones/notifiers.py @@ -27,7 +27,7 @@ import time from xml.dom import minidom from httplib import HTTPSConnection from urllib import urlencode - +from lib.pynma import pynma import lib.oauth2 as oauth import lib.pythontwitter as twitter @@ -380,34 +380,40 @@ class Plex: logger.warn('Error sending notification request to Plex Media Server') class NMA: + def notify(self, artist=None, album=None, snatched=None): + title = 'Headphones' + api = headphones.NMA_APIKEY + nma_priority = headphones.NMA_PRIORITY - def __init__(self): - - self.apikey = headphones.NMA_APIKEY - self.priority = headphones.NMA_PRIORITY - - def _send(self, data): - return request.request_content('https://www.notifymyandroid.com/publicapi/notify', data=data) - - def notify(self, artist=None, album=None, snatched_nzb=None): - - apikey = self.apikey - priority = self.priority - - if snatched_nzb: - event = snatched_nzb + " snatched!" - description = "Headphones has snatched: " + snatched_nzb + " and has sent it to SABnzbd+" + logger.debug(u"NMA title: " + title) + logger.debug(u"NMA API: " + api) + logger.debug(u"NMA Priority: " + str(nma_priority)) + if snatched: + event = snatched + " snatched!" + message = "Headphones has snatched: " + snatched else: event = artist + ' - ' + album + ' complete!' - description = "Headphones has downloaded and postprocessed: " + artist + ' [' + album + ']' + message = "Headphones has downloaded and postprocessed: " + artist + ' [' + album + ']' - data = { 'apikey': apikey, 'application':'Headphones', 'event': event, 'description': description, 'priority': priority} - logger.info('Sending notification request to NotifyMyAndroid') - request = self._send(data) + logger.debug(u"NMA event: " + event) + logger.debug(u"NMA message: " + message) - if not request: - logger.warn('Error sending notification request to NotifyMyAndroid') + batch = False + + p = pynma.PyNMA() + keys = api.split(',') + p.addkey(keys) + + if len(keys) > 1: batch = True + + response = p.push(title, event, message, priority=nma_priority, batch_mode=batch) + + if not response[api][u'code'] == u'200': + logger.error(u'Could not send notification to NotifyMyAndroid') + return False + else: + return True class PUSHBULLET: diff --git a/headphones/searcher.py b/headphones/searcher.py index 1cc78b84..48483b0d 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -699,7 +699,7 @@ def send_to_downloader(data, bestqual, album): except Exception, e: logger.exception("Unhandled exception") - else: + elif headphones.TORRENT_DOWNLOADER == 2: logger.info("Sending torrent to uTorrent") # rutracker needs cookies to be set, pass the .torrent file instead of url @@ -725,6 +725,32 @@ def send_to_downloader(data, bestqual, album): except Exception, e: logger.exception("Unhandled exception") + else: + logger.info("Sending torrent to DownloadStation") + + # rutracker needs cookies to be set, pass the .torrent file instead of url + if bestqual[3] == 'rutracker.org': + file_or_url = rutracker.get_torrent(bestqual[2]) + else: + file_or_url = bestqual[2] + + _hash = CalculateTorrentHash(file_or_url, data) + + folder_name = download_station.addTorrent(file_or_url) + + if folder_name: + logger.info('Torrent folder name: %s' % folder_name) + else: + logger.error('Torrent folder name could not be determined') + return + + # remove temp .torrent file created above + if bestqual[3] == 'rutracker.org': + try: + shutil.rmtree(os.path.split(file_or_url)[0]) + except Exception, e: + logger.exception("Unhandled exception") + myDB = db.DBConnection() myDB.action('UPDATE albums SET status = "Snatched" WHERE AlbumID=?', [album['AlbumID']]) myDB.action('INSERT INTO snatched VALUES( ?, ?, ?, ?, DATETIME("NOW", "localtime"), ?, ?, ?)', [album['AlbumID'], bestqual[0], bestqual[1], bestqual[2], "Snatched", folder_name, kind]) @@ -762,7 +788,7 @@ def send_to_downloader(data, bestqual, album): if headphones.NMA_ENABLED and headphones.NMA_ONSNATCH: logger.info(u"Sending NMA notification") nma = notifiers.NMA() - nma.notify(snatched_nzb=name) + nma.notify(snatched=name) if headphones.PUSHALOT_ENABLED and headphones.PUSHALOT_ONSNATCH: logger.info(u"Sending Pushalot notification") pushalot = notifiers.PUSHALOT() diff --git a/headphones/utorrent.py b/headphones/utorrent.py index e60d95c5..3ce07906 100644 --- a/headphones/utorrent.py +++ b/headphones/utorrent.py @@ -144,7 +144,6 @@ class utorrentclient(object): def addTorrent(link, hash): label = headphones.UTORRENT_LABEL - uTorrentClient = utorrentclient() uTorrentClient.add_url(link) time.sleep(1) #need to ensure file is loaded uTorrent... @@ -154,4 +153,4 @@ def addTorrent(link, hash): if (torrent[0].lower()==hash): return torrent[26] - return False + return False \ No newline at end of file