From 668b95353fc22f26c1398b8aa55053b626ab2df1 Mon Sep 17 00:00:00 2001 From: Ruud Date: Fri, 11 Nov 2011 15:47:57 +0100 Subject: [PATCH] Urlopen try,except fixes --- couchpotato/core/downloaders/nzbget/main.py | 5 +- couchpotato/core/notifications/nmj/main.py | 12 ++-- couchpotato/core/notifications/notifo/main.py | 5 +- couchpotato/core/notifications/xbmc/main.py | 4 +- couchpotato/core/plugins/base.py | 10 ++- couchpotato/core/plugins/file/main.py | 6 +- couchpotato/core/plugins/searcher/main.py | 7 +- .../providers/movie/couchpotatoapi/main.py | 7 +- couchpotato/core/providers/nzb/moovee/main.py | 55 +++++++--------- .../core/providers/nzb/newzbin/main.py | 8 ++- .../core/providers/nzb/newznab/main.py | 10 +-- .../core/providers/nzb/nzbindex/main.py | 10 +-- .../core/providers/nzb/nzbmatrix/main.py | 10 +-- couchpotato/core/providers/nzb/nzbs/main.py | 10 +-- couchpotato/core/providers/nzb/x264/main.py | 65 +++++++++---------- .../core/providers/trailer/hdtrailers/main.py | 10 ++- .../providers/userscript/allocine/main.py | 5 +- .../userscript/appletrailers/main.py | 5 +- couchpotato/core/providers/userscript/base.py | 5 +- 19 files changed, 116 insertions(+), 133 deletions(-) diff --git a/couchpotato/core/downloaders/nzbget/main.py b/couchpotato/core/downloaders/nzbget/main.py index b7843b27..198215d7 100644 --- a/couchpotato/core/downloaders/nzbget/main.py +++ b/couchpotato/core/downloaders/nzbget/main.py @@ -3,6 +3,7 @@ from couchpotato.core.downloaders.base import Downloader from couchpotato.core.logger import CPLog from inspect import isfunction import socket +import traceback import xmlrpclib log = CPLog(__name__) @@ -48,8 +49,8 @@ class NZBGet(Downloader): else: log.info('Downloading: %s' % data.get('url')) file = self.urlopen(data.get('url')) - except Exception, e: - log.error('Unable to get NZB file: %s' % e) + except: + log.error('Unable to get NZB file: %s' % traceback.format_exc()) return False if rpc.append(nzb_name, self.conf('category'), False, standard_b64encode(file.strip())): diff --git a/couchpotato/core/notifications/nmj/main.py b/couchpotato/core/notifications/nmj/main.py index 934dc391..1ac8d70a 100644 --- a/couchpotato/core/notifications/nmj/main.py +++ b/couchpotato/core/notifications/nmj/main.py @@ -79,9 +79,9 @@ class NMJ(Notification): if self.mount: log.debug('Try to mount network drive via url: %s' % (mount)) - data = self.urlopen(mount) - if not data: - log.error('Warning: Couldn\'t contact popcorn hour on host %s' % host) + try: + data = self.urlopen(mount) + except: return False params = { @@ -94,9 +94,9 @@ class NMJ(Notification): UPDATE_URL = 'http://%(host)s:8008/metadata_database?%(params)s' updateUrl = UPDATE_URL % {'host': host, 'params': params} - response = self.urlopen(updateUrl) - if not response: - log.error('Warning: Couldn\'t contact Popcorn Hour on host %s' % host) + try: + response = self.urlopen(updateUrl) + except: return False try: diff --git a/couchpotato/core/notifications/notifo/main.py b/couchpotato/core/notifications/notifo/main.py index f5c49c52..d4baaf6b 100644 --- a/couchpotato/core/notifications/notifo/main.py +++ b/couchpotato/core/notifications/notifo/main.py @@ -3,6 +3,7 @@ from couchpotato.core.logger import CPLog from couchpotato.core.notifications.base import Notification from flask.helpers import json import base64 +import traceback log = CPLog(__name__) @@ -30,8 +31,8 @@ class Notifo(Notification): if result['status'] != 'success' or result['response_message'] != 'OK': raise Exception - except Exception, e: - log.error('Notification failed: %s' % e) + except: + log.error('Notification failed: %s' % traceback.format_exc()) return False log.info('Notifo notification successful.') diff --git a/couchpotato/core/notifications/xbmc/main.py b/couchpotato/core/notifications/xbmc/main.py index 299d1429..4fe0a45c 100644 --- a/couchpotato/core/notifications/xbmc/main.py +++ b/couchpotato/core/notifications/xbmc/main.py @@ -29,7 +29,9 @@ class XBMC(Notification): 'Authorization': "Basic %s" % base64.encodestring('%s:%s' % (self.conf('username'), self.conf('password')))[:-1] } - if not self.urlopen(url, headers = headers): + try: + self.urlopen(url, headers = headers) + except: log.error("Couldn't sent command to XBMC") return False diff --git a/couchpotato/core/plugins/base.py b/couchpotato/core/plugins/base.py index 902dd94e..10881c1e 100644 --- a/couchpotato/core/plugins/base.py +++ b/couchpotato/core/plugins/base.py @@ -160,12 +160,20 @@ class Plugin(object): log.error("Something went wrong when finishing the plugin function. Could not find the 'is_running' key") - def getCache(self, cache_key): + def getCache(self, cache_key, url = None): cache = Env.get('cache').get(cache_key) if cache: log.debug('Getting cache %s' % cache_key) return cache + if url: + try: + data = self.urlopen(url) + self.setCache(cache_key, data) + return data + except: + pass + def setCache(self, cache_key, value, timeout = 300): log.debug('Setting cache %s' % cache_key) Env.get('cache').set(cache_key, value, timeout) diff --git a/couchpotato/core/plugins/file/main.py b/couchpotato/core/plugins/file/main.py index b3b9918f..714e392a 100644 --- a/couchpotato/core/plugins/file/main.py +++ b/couchpotato/core/plugins/file/main.py @@ -34,9 +34,9 @@ class FileManager(Plugin): def download(self, url = '', dest = None, overwrite = False): - file = self.urlopen(url) - if not file: - log.error('File is empty, don\'t download') + try: + file = self.urlopen(url) + except: return False if not dest: # to Cache diff --git a/couchpotato/core/plugins/searcher/main.py b/couchpotato/core/plugins/searcher/main.py index 63029d76..63020395 100644 --- a/couchpotato/core/plugins/searcher/main.py +++ b/couchpotato/core/plugins/searcher/main.py @@ -261,7 +261,10 @@ class Searcher(Plugin): nfo = self.getCache(cache_key) if not nfo: - nfo = self.urlopen('http://www.srrdb.com/showfile.php?release=%s' % check_name) - self.setCache(cache_key, nfo) + try: + nfo = self.urlopen('http://www.srrdb.com/showfile.php?release=%s' % check_name) + self.setCache(cache_key, nfo) + except: + pass return getImdb(nfo) == imdb_id diff --git a/couchpotato/core/providers/movie/couchpotatoapi/main.py b/couchpotato/core/providers/movie/couchpotatoapi/main.py index c2e02110..425ef946 100644 --- a/couchpotato/core/providers/movie/couchpotatoapi/main.py +++ b/couchpotato/core/providers/movie/couchpotatoapi/main.py @@ -20,9 +20,8 @@ class CouchPotatoApi(MovieProvider): def releaseDate(self, imdb_id): - data = self.urlopen((self.apiUrl % ('eta')) + (id + '/')) - try: + data = self.urlopen((self.apiUrl % ('eta')) + (id + '/')) dates = json.loads(data) log.info('Found ETA for %s: %s' % (imdb_id, dates)) except Exception, e: @@ -31,10 +30,8 @@ class CouchPotatoApi(MovieProvider): return dates def suggest(self, movies = [], ignore = []): - - data = self.urlopen((self.apiUrl % ('suggest')) + ','.join(movies) + '/' + ','.join(ignore) + '/') - try: + data = self.urlopen((self.apiUrl % ('suggest')) + ','.join(movies) + '/' + ','.join(ignore) + '/') suggestions = json.loads(data) log.info('Found Suggestions for %s' % (suggestions)) except Exception, e: diff --git a/couchpotato/core/providers/nzb/moovee/main.py b/couchpotato/core/providers/nzb/moovee/main.py index 039ba328..d75e7b5c 100644 --- a/couchpotato/core/providers/nzb/moovee/main.py +++ b/couchpotato/core/providers/nzb/moovee/main.py @@ -28,39 +28,32 @@ class Moovee(NZBProvider): url = self.urls['search'] % quote_plus(q) cache_key = 'moovee.%s' % q - data = self.getCache(cache_key) - if not data: - data = self.urlopen(url) - self.setCache(cache_key, data) + data = self.getCache(cache_key, url) + if data: + match = re.compile(self.regex, re.DOTALL).finditer(data) - if not data: - log.error('Failed to get data from %s.' % url) - return results + for nzb in match: + new = { + 'id': nzb.group('reqid'), + 'name': nzb.group('title'), + 'type': 'nzb', + 'provider': self.getName(), + 'age': self.calculateAge(time.mktime(parse(nzb.group('age')).timetuple())), + 'size': None, + 'url': self.urls['download'] % (nzb.group('reqid')), + 'download': self.download, + 'detail_url': '', + 'description': '', + 'check_nzb': False, + } - match = re.compile(self.regex, re.DOTALL).finditer(data) - - for nzb in match: - new = { - 'id': nzb.group('reqid'), - 'name': nzb.group('title'), - 'type': 'nzb', - 'provider': self.getName(), - 'age': self.calculateAge(time.mktime(parse(nzb.group('age')).timetuple())), - 'size': None, - 'url': self.urls['download'] % (nzb.group('reqid')), - 'download': self.download, - 'detail_url': '', - 'description': '', - 'check_nzb': False, - } - - new['score'] = fireEvent('score.calculate', new, movie, single = True) - is_correct_movie = fireEvent('searcher.correct_movie', - nzb = new, movie = movie, quality = quality, - imdb_results = False, single_category = False, single = True) - if is_correct_movie: - results.append(new) - self.found(new) + new['score'] = fireEvent('score.calculate', new, movie, single = True) + is_correct_movie = fireEvent('searcher.correct_movie', + nzb = new, movie = movie, quality = quality, + imdb_results = False, single_category = False, single = True) + if is_correct_movie: + results.append(new) + self.found(new) return results diff --git a/couchpotato/core/providers/nzb/newzbin/main.py b/couchpotato/core/providers/nzb/newzbin/main.py index 498a8e9a..7cc7a8b3 100644 --- a/couchpotato/core/providers/nzb/newzbin/main.py +++ b/couchpotato/core/providers/nzb/newzbin/main.py @@ -66,9 +66,11 @@ class Newzbin(NZBProvider, RSS): headers = { 'Authorization': "Basic %s" % base64.encodestring('%s:%s' % (self.conf('username'), self.conf('password')))[:-1] } - - data = self.urlopen(url, headers = headers) - self.setCache(cache_key, data) + try: + data = self.urlopen(url, headers = headers) + self.setCache(cache_key, data) + except: + return results if data: try: diff --git a/couchpotato/core/providers/nzb/newznab/main.py b/couchpotato/core/providers/nzb/newznab/main.py index 3316c0c9..a2a1cf26 100644 --- a/couchpotato/core/providers/nzb/newznab/main.py +++ b/couchpotato/core/providers/nzb/newznab/main.py @@ -100,15 +100,7 @@ class Newznab(NZBProvider, RSS): def createItems(self, url, cache_key, host, single_cat = False, movie = None, quality = None, for_feed = False): results = [] - data = self.getCache(cache_key) - if not data: - data = self.urlopen(url) - self.setCache(cache_key, data) - - if not data: - log.error('Failed to get data from %s.' % url) - return results - + data = self.getCache(cache_key, url) if data: try: try: diff --git a/couchpotato/core/providers/nzb/nzbindex/main.py b/couchpotato/core/providers/nzb/nzbindex/main.py index 13d9f90f..af2043e9 100644 --- a/couchpotato/core/providers/nzb/nzbindex/main.py +++ b/couchpotato/core/providers/nzb/nzbindex/main.py @@ -37,15 +37,7 @@ class NzbIndex(NZBProvider, RSS): cache_key = 'nzbindex.%s.%s' % (movie['library'].get('identifier'), quality.get('identifier')) - data = self.getCache(cache_key) - if not data: - data = self.urlopen(url) - self.setCache(cache_key, data) - - if not data: - log.error('Failed to get data from %s.' % url) - return results - + data = self.getCache(cache_key, url) if data: try: try: diff --git a/couchpotato/core/providers/nzb/nzbmatrix/main.py b/couchpotato/core/providers/nzb/nzbmatrix/main.py index 9578b5ea..e069b922 100644 --- a/couchpotato/core/providers/nzb/nzbmatrix/main.py +++ b/couchpotato/core/providers/nzb/nzbmatrix/main.py @@ -50,15 +50,7 @@ class NZBMatrix(NZBProvider, RSS): cache_key = 'nzbmatrix.%s.%s' % (movie['library'].get('identifier'), cat_ids) single_cat = True - data = self.getCache(cache_key) - if not data: - data = self.urlopen(url) - self.setCache(cache_key, data) - - if not data: - log.error('Failed to get data from %s.' % url) - return results - + data = self.getCache(cache_key, url) if data: try: try: diff --git a/couchpotato/core/providers/nzb/nzbs/main.py b/couchpotato/core/providers/nzb/nzbs/main.py index da031963..04d8cf14 100644 --- a/couchpotato/core/providers/nzb/nzbs/main.py +++ b/couchpotato/core/providers/nzb/nzbs/main.py @@ -47,15 +47,7 @@ class Nzbs(NZBProvider, RSS): cache_key = 'nzbs.%s.%s' % (movie['library'].get('identifier'), str(cat_id)) - data = self.getCache(cache_key) - if not data: - data = self.urlopen(url) - self.setCache(cache_key, data) - - if not data: - log.error('Failed to get data from %s.' % url) - return results - + data = self.getCache(cache_key, url) if data: try: try: diff --git a/couchpotato/core/providers/nzb/x264/main.py b/couchpotato/core/providers/nzb/x264/main.py index 2795dd68..2d728d3d 100644 --- a/couchpotato/core/providers/nzb/x264/main.py +++ b/couchpotato/core/providers/nzb/x264/main.py @@ -27,45 +27,38 @@ class X264(NZBProvider): url = self.urls['search'] % quote_plus(q) cache_key = 'x264.%s' % q - data = self.getCache(cache_key) - if not data: - data = self.urlopen(url) - self.setCache(cache_key, data) + data = self.getCache(cache_key, url) + if data: + match = re.compile(self.regex, re.DOTALL).finditer(data) - if not data: - log.error('Failed to get data from %s.' % url) - return results + for nzb in match: + try: + age_match = re.match('((?P\d+)d)', nzb.group('age')) + age = age_match.group('day') + except: + age = 1 - match = re.compile(self.regex, re.DOTALL).finditer(data) + new = { + 'id': nzb.group('id'), + 'name': nzb.group('title'), + 'type': 'nzb', + 'provider': self.getName(), + 'age': tryInt(age), + 'size': None, + 'url': self.urls['download'] % (nzb.group('id')), + 'download': self.download, + 'detail_url': '', + 'description': '', + 'check_nzb': False, + } - for nzb in match: - try: - age_match = re.match('((?P\d+)d)', nzb.group('age')) - age = age_match.group('day') - except: - age = 1 - - new = { - 'id': nzb.group('id'), - 'name': nzb.group('title'), - 'type': 'nzb', - 'provider': self.getName(), - 'age': tryInt(age), - 'size': None, - 'url': self.urls['download'] % (nzb.group('id')), - 'download': self.download, - 'detail_url': '', - 'description': '', - 'check_nzb': False, - } - - new['score'] = fireEvent('score.calculate', new, movie, single = True) - is_correct_movie = fireEvent('searcher.correct_movie', - nzb = new, movie = movie, quality = quality, - imdb_results = False, single_category = False, single = True) - if is_correct_movie: - results.append(new) - self.found(new) + new['score'] = fireEvent('score.calculate', new, movie, single = True) + is_correct_movie = fireEvent('searcher.correct_movie', + nzb = new, movie = movie, quality = quality, + imdb_results = False, single_category = False, single = True) + if is_correct_movie: + results.append(new) + self.found(new) return results diff --git a/couchpotato/core/providers/trailer/hdtrailers/main.py b/couchpotato/core/providers/trailer/hdtrailers/main.py index 56a4eedc..556d9db0 100644 --- a/couchpotato/core/providers/trailer/hdtrailers/main.py +++ b/couchpotato/core/providers/trailer/hdtrailers/main.py @@ -21,7 +21,10 @@ class HDTrailers(TrailerProvider): movie_name = movie['library']['titles'][0]['title'] url = self.url['api'] % self.movieUrlName(movie_name) - data = self.urlopen(url) + try: + data = self.urlopen(url) + except: + return {} p480 = [] p720 = [] @@ -45,7 +48,10 @@ class HDTrailers(TrailerProvider): results = {'480p':[], '720p':[], '1080p':[]} url = "%s?%s" % (self.url['backup'], urlencode({'s':movie})) - data = self.urlopen(url) + try: + data = self.urlopen(url) + except: + return results try: tables = SoupStrainer('div') diff --git a/couchpotato/core/providers/userscript/allocine/main.py b/couchpotato/core/providers/userscript/allocine/main.py index e34de97b..e92c0ce9 100644 --- a/couchpotato/core/providers/userscript/allocine/main.py +++ b/couchpotato/core/providers/userscript/allocine/main.py @@ -10,7 +10,10 @@ class AlloCine(UserscriptBase): if not 'fichefilm_gen_cfilm' in url: return 'Url isn\'t from a movie' - data = self.urlopen(url) + try: + data = self.urlopen(url) + except: + return html = BeautifulSoup(data) title = html.find('title').contents[0].strip() diff --git a/couchpotato/core/providers/userscript/appletrailers/main.py b/couchpotato/core/providers/userscript/appletrailers/main.py index 8e5eae90..d7ce8ab3 100644 --- a/couchpotato/core/providers/userscript/appletrailers/main.py +++ b/couchpotato/core/providers/userscript/appletrailers/main.py @@ -8,7 +8,10 @@ class AppleTrailers(UserscriptBase): def getMovie(self, url): - data = self.urlopen(url) + try: + data = self.urlopen(url) + except: + return name = re.search("trailerTitle.*=.*\'(?P.*)\';", data) name = name.group('name').decode('string_escape') diff --git a/couchpotato/core/providers/userscript/base.py b/couchpotato/core/providers/userscript/base.py index e23a540a..1bf576ff 100644 --- a/couchpotato/core/providers/userscript/base.py +++ b/couchpotato/core/providers/userscript/base.py @@ -42,7 +42,10 @@ class UserscriptBase(Plugin): return def getMovie(self, url): - data = self.urlopen(url) + try: + data = self.urlopen(url) + except: + data = '' return self.getInfo(getImdb(data)) def getInfo(self, identifier):