From ba36c738c7d6c345e728f957ab997f1c32f89a86 Mon Sep 17 00:00:00 2001 From: clinton-hall Date: Wed, 26 Sep 2012 16:46:10 +0930 Subject: [PATCH 1/4] Added def remove Allows renamer to request deletion of failed downloads --- couchpotato/core/downloaders/base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/couchpotato/core/downloaders/base.py b/couchpotato/core/downloaders/base.py index 6fa6a915..6ca29f6d 100644 --- a/couchpotato/core/downloaders/base.py +++ b/couchpotato/core/downloaders/base.py @@ -24,6 +24,7 @@ class Downloader(Plugin): def __init__(self): addEvent('download', self.download) addEvent('download.status', self.getDownloadStatus) + addEvent('download.remove', self.remove) def download(self, data = {}, movie = {}, manual = False, filedata = None): pass @@ -31,6 +32,9 @@ class Downloader(Plugin): def getDownloadStatus(self, data = {}, movie = {}): return False + def remove(self, name = {}, nzo_id = {}): + return False + def createNzbName(self, data, movie): tag = self.cpTag(movie) return '%s%s' % (toSafeString(data.get('name')[:127 - len(tag)]), tag) From ef945597d2036217bb78a4fc166dbb55dec80e88 Mon Sep 17 00:00:00 2001 From: clinton-hall Date: Wed, 26 Sep 2012 16:51:51 +0930 Subject: [PATCH 2/4] Removed checking of status results from here getDownloadStatus is only called once from renamer and all results are passed back. Def remove is added so that renamer can request a failed downlaod to be deleted from SABnzbd if enabled. --- couchpotato/core/downloaders/sabnzbd/main.py | 96 ++++++++------------ 1 file changed, 38 insertions(+), 58 deletions(-) diff --git a/couchpotato/core/downloaders/sabnzbd/main.py b/couchpotato/core/downloaders/sabnzbd/main.py index 0151cc47..f05f0d50 100644 --- a/couchpotato/core/downloaders/sabnzbd/main.py +++ b/couchpotato/core/downloaders/sabnzbd/main.py @@ -67,8 +67,7 @@ class Sabnzbd(Downloader): if self.isDisabled(manual = True) or not self.isCorrectType(data.get('type')): return - nzbname = self.createNzbName(data, movie) - log.info('Checking download status of "%s" at SABnzbd.', nzbname) + log.info('Checking SABnzbd download status.') # Go through Queue params = { @@ -85,20 +84,12 @@ class Sabnzbd(Downloader): return False try: - history = json.loads(sab) + queue = json.loads(sab) except: log.debug("Result text from SAB: " + sab[:40]) log.error('Failed parsing json status: %s', traceback.format_exc()) return False - try: - for slot in history['queue']['slots']: - log.debug('Found %s in SabNZBd queue, which is %s, with %s left', (slot['filename'], slot['status'], slot['timeleft'])) - if slot['filename'] == nzbname: - return slot['status'].lower() - except: - log.debug('No items in queue: %s', (traceback.format_exc())) - # Go through history items params = { 'apikey': self.conf('api_key'), @@ -112,57 +103,46 @@ class Sabnzbd(Downloader): sab = self.urlopen(url, timeout = 60, show_error = False) except: log.error('Failed getting history: %s', traceback.format_exc()) - return + return False try: history = json.loads(sab) except: log.debug("Result text from SAB: " + sab[:40]) log.error('Failed parsing history json: %s', traceback.format_exc()) + return False + + return queue, history + + def remove(self, name = {}, nzo_id = {}): + # Delete failed download + if self.conf('delete_failed', default = True): + + log.info('%s failed downloading, deleting...', name) + params = { + 'apikey': self.conf('api_key'), + 'mode': 'history', + 'name': 'delete', + 'del_files': '1', + 'value': nzo_id + } + url = cleanHost(self.conf('host')) + "api?" + tryUrlencode(params) + + try: + sab = self.urlopen(url, timeout = 60, show_error = False) + except: + log.error('Failed deleting: %s', traceback.format_exc()) + return False + + result = sab.strip() + if not result: + log.error("SABnzbd didn't return anything.") + + log.debug("Result text from SAB: " + result[:40]) + if result == "ok": + log.info('SabNZBd deleted failed release %s successfully.', name) + elif result == "Missing authentication": + log.error("Incorrect username/password or API?.") + else: + log.error("Unknown error: " + result[:40]) return - - try: - for slot in history['history']['slots']: - log.debug('Found %s in SabNZBd history, which has %s', (slot['name'], slot['status'])) - if slot['name'] == nzbname: - # Note: if post process even if failed is on in SabNZBd, it will complete with a fail message - if slot['status'] == 'Failed' or (slot['status'] == 'Completed' and slot['fail_message'].strip()): - - # Delete failed download - if self.conf('delete_failed', default = True): - - log.info('%s failed downloading, deleting...', slot['name']) - params = { - 'apikey': self.conf('api_key'), - 'mode': 'history', - 'name': 'delete', - 'del_files': '1', - 'value': slot['nzo_id'] - } - url = cleanHost(self.conf('host')) + "api?" + tryUrlencode(params) - - try: - sab = self.urlopen(url, timeout = 60, show_error = False) - except: - log.error('Failed deleting: %s', traceback.format_exc()) - return False - - result = sab.strip() - if not result: - log.error("SABnzbd didn't return anything.") - - log.debug("Result text from SAB: " + result[:40]) - if result == "ok": - log.info('SabNZBd deleted failed release %s successfully.', slot['name']) - elif result == "Missing authentication": - log.error("Incorrect username/password or API?.") - else: - log.error("Unknown error: " + result[:40]) - - return 'failed' - else: - return slot['status'].lower() - except: - log.debug('No items in history: %s', (traceback.format_exc())) - - return 'not_found' From c6cba2f6e55f5f594738e9ac4b227dc99ef34979 Mon Sep 17 00:00:00 2001 From: clinton-hall Date: Wed, 26 Sep 2012 16:59:36 +0930 Subject: [PATCH 3/4] Only request SABnzbd status once Performs the checking of queue and history here. Requests delete of failed downlaods via download.remove event. --- couchpotato/core/plugins/renamer/main.py | 27 ++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/couchpotato/core/plugins/renamer/main.py b/couchpotato/core/plugins/renamer/main.py index a696265c..aedc40c2 100644 --- a/couchpotato/core/plugins/renamer/main.py +++ b/couchpotato/core/plugins/renamer/main.py @@ -506,9 +506,11 @@ class Renamer(Plugin): if rels: log.debug('Checking status snatched releases...') + # get queue and history (once) from SABnzbd + queue, history = fireEvent('download.status', data = {}, movie = {}, single = True) scan_required = False - + for rel in rels: # Get current selected title @@ -530,7 +532,28 @@ class Renamer(Plugin): movie_dict = fireEvent('movie.get', rel.movie_id, single = True) # check status - downloadstatus = fireEvent('download.status', data = item, movie = movie_dict, single = True) + try: + for slot in queue['queue']['slots']: + log.debug('Found %s in SabNZBd queue, which is %s, with %s left', (slot['filename'], slot['status'], slot['timeleft'])) + if slot['filename'] == nzbname: + downloadstatus =['status'].lower() + except: + log.debug('No items in queue: %s', (traceback.format_exc())) + try: + for slot in history['history']['slots']: + log.debug('Found %s in SabNZBd history, which has %s', (slot['name'], slot['status'])) + if slot['name'] == nzbname: + # Note: if post process even if failed is on in SabNZBd, it will complete with a fail message + if slot['status'] == 'Failed' or (slot['status'] == 'Completed' and slot['fail_message'].strip()): + + # Delete failed download + rel_remove = fireEvent('download.remove', name = slot['name'], nzo_id = slot['nzo_id'], single = True) + downloadstatus = 'failed' + else: + downloadstatus = slot['status'].lower() + except: + log.debug('No items in history: %s', (traceback.format_exc())) + if not downloadstatus: log.debug('Download status functionality is not implemented for active downloaders.') scan_required = True From dc63796e4864454204bb7f88ee6383022004e2e6 Mon Sep 17 00:00:00 2001 From: clinton-hall Date: Wed, 26 Sep 2012 17:12:19 +0930 Subject: [PATCH 4/4] Added nzbname previously defined in Downloader. I forgot to bring this across. --- couchpotato/core/plugins/renamer/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/couchpotato/core/plugins/renamer/main.py b/couchpotato/core/plugins/renamer/main.py index aedc40c2..2f579113 100644 --- a/couchpotato/core/plugins/renamer/main.py +++ b/couchpotato/core/plugins/renamer/main.py @@ -532,6 +532,7 @@ class Renamer(Plugin): movie_dict = fireEvent('movie.get', rel.movie_id, single = True) # check status + nzbname = self.createNzbName(item, movie_dict) try: for slot in queue['queue']['slots']: log.debug('Found %s in SabNZBd queue, which is %s, with %s left', (slot['filename'], slot['status'], slot['timeleft']))