Urlopen try,except fixes

This commit is contained in:
Ruud committed 2011-11-11 15:47:57 +01:00
1 parent 0420bdeec5
commit 668b95353f
19 files changed
+116 -133

No files matched your search

+3 -2
View File
@@ -3,6 +3,7 @@ from couchpotato.core.downloaders.base import Downloader
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
from inspect import isfunction from inspect import isfunction
import socket import socket
import traceback
import xmlrpclib import xmlrpclib
log = CPLog(__name__) log = CPLog(__name__)
@@ -48,8 +49,8 @@ class NZBGet(Downloader):
else: else:
log.info('Downloading: %s' % data.get('url')) log.info('Downloading: %s' % data.get('url'))
file = self.urlopen(data.get('url')) file = self.urlopen(data.get('url'))
except Exception, e: except:
log.error('Unable to get NZB file: %s' % e) log.error('Unable to get NZB file: %s' % traceback.format_exc())
return False return False
if rpc.append(nzb_name, self.conf('category'), False, standard_b64encode(file.strip())): if rpc.append(nzb_name, self.conf('category'), False, standard_b64encode(file.strip())):
+6 -6
View File
@@ -79,9 +79,9 @@ class NMJ(Notification):
if self.mount: if self.mount:
log.debug('Try to mount network drive via url: %s' % (mount)) log.debug('Try to mount network drive via url: %s' % (mount))
data = self.urlopen(mount) try:
if not data: data = self.urlopen(mount)
log.error('Warning: Couldn\'t contact popcorn hour on host %s' % host) except:
return False return False
params = { params = {
@@ -94,9 +94,9 @@ class NMJ(Notification):
UPDATE_URL = 'http://%(host)s:8008/metadata_database?%(params)s' UPDATE_URL = 'http://%(host)s:8008/metadata_database?%(params)s'
updateUrl = UPDATE_URL % {'host': host, 'params': params} updateUrl = UPDATE_URL % {'host': host, 'params': params}
response = self.urlopen(updateUrl) try:
if not response: response = self.urlopen(updateUrl)
log.error('Warning: Couldn\'t contact Popcorn Hour on host %s' % host) except:
return False return False
try: try:
@@ -3,6 +3,7 @@ from couchpotato.core.logger import CPLog
from couchpotato.core.notifications.base import Notification from couchpotato.core.notifications.base import Notification
from flask.helpers import json from flask.helpers import json
import base64 import base64
import traceback
log = CPLog(__name__) log = CPLog(__name__)
@@ -30,8 +31,8 @@ class Notifo(Notification):
if result['status'] != 'success' or result['response_message'] != 'OK': if result['status'] != 'success' or result['response_message'] != 'OK':
raise Exception raise Exception
except Exception, e: except:
log.error('Notification failed: %s' % e) log.error('Notification failed: %s' % traceback.format_exc())
return False return False
log.info('Notifo notification successful.') log.info('Notifo notification successful.')
+3 -1
View File
@@ -29,7 +29,9 @@ class XBMC(Notification):
'Authorization': "Basic %s" % base64.encodestring('%s:%s' % (self.conf('username'), self.conf('password')))[:-1] '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") log.error("Couldn't sent command to XBMC")
return False return False
+9 -1
View File
@@ -160,12 +160,20 @@ class Plugin(object):
log.error("Something went wrong when finishing the plugin function. Could not find the 'is_running' key") 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) cache = Env.get('cache').get(cache_key)
if cache: if cache:
log.debug('Getting cache %s' % cache_key) log.debug('Getting cache %s' % cache_key)
return cache 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): def setCache(self, cache_key, value, timeout = 300):
log.debug('Setting cache %s' % cache_key) log.debug('Setting cache %s' % cache_key)
Env.get('cache').set(cache_key, value, timeout) Env.get('cache').set(cache_key, value, timeout)
+3 -3
View File
@@ -34,9 +34,9 @@ class FileManager(Plugin):
def download(self, url = '', dest = None, overwrite = False): def download(self, url = '', dest = None, overwrite = False):
file = self.urlopen(url) try:
if not file: file = self.urlopen(url)
log.error('File is empty, don\'t download') except:
return False return False
if not dest: # to Cache if not dest: # to Cache
+5 -2
View File
@@ -261,7 +261,10 @@ class Searcher(Plugin):
nfo = self.getCache(cache_key) nfo = self.getCache(cache_key)
if not nfo: if not nfo:
nfo = self.urlopen('http://www.srrdb.com/showfile.php?release=%s' % check_name) try:
self.setCache(cache_key, nfo) 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 return getImdb(nfo) == imdb_id
@@ -20,9 +20,8 @@ class CouchPotatoApi(MovieProvider):
def releaseDate(self, imdb_id): def releaseDate(self, imdb_id):
data = self.urlopen((self.apiUrl % ('eta')) + (id + '/'))
try: try:
data = self.urlopen((self.apiUrl % ('eta')) + (id + '/'))
dates = json.loads(data) dates = json.loads(data)
log.info('Found ETA for %s: %s' % (imdb_id, dates)) log.info('Found ETA for %s: %s' % (imdb_id, dates))
except Exception, e: except Exception, e:
@@ -31,10 +30,8 @@ class CouchPotatoApi(MovieProvider):
return dates return dates
def suggest(self, movies = [], ignore = []): def suggest(self, movies = [], ignore = []):
data = self.urlopen((self.apiUrl % ('suggest')) + ','.join(movies) + '/' + ','.join(ignore) + '/')
try: try:
data = self.urlopen((self.apiUrl % ('suggest')) + ','.join(movies) + '/' + ','.join(ignore) + '/')
suggestions = json.loads(data) suggestions = json.loads(data)
log.info('Found Suggestions for %s' % (suggestions)) log.info('Found Suggestions for %s' % (suggestions))
except Exception, e: except Exception, e:
+24 -31
View File
@@ -28,39 +28,32 @@ class Moovee(NZBProvider):
url = self.urls['search'] % quote_plus(q) url = self.urls['search'] % quote_plus(q)
cache_key = 'moovee.%s' % q cache_key = 'moovee.%s' % q
data = self.getCache(cache_key) data = self.getCache(cache_key, url)
if not data: if data:
data = self.urlopen(url) match = re.compile(self.regex, re.DOTALL).finditer(data)
self.setCache(cache_key, data)
if not data: for nzb in match:
log.error('Failed to get data from %s.' % url) new = {
return results '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) new['score'] = fireEvent('score.calculate', new, movie, single = True)
is_correct_movie = fireEvent('searcher.correct_movie',
for nzb in match: nzb = new, movie = movie, quality = quality,
new = { imdb_results = False, single_category = False, single = True)
'id': nzb.group('reqid'), if is_correct_movie:
'name': nzb.group('title'), results.append(new)
'type': 'nzb', self.found(new)
'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)
return results return results
@@ -66,9 +66,11 @@ class Newzbin(NZBProvider, RSS):
headers = { headers = {
'Authorization': "Basic %s" % base64.encodestring('%s:%s' % (self.conf('username'), self.conf('password')))[:-1] 'Authorization': "Basic %s" % base64.encodestring('%s:%s' % (self.conf('username'), self.conf('password')))[:-1]
} }
try:
data = self.urlopen(url, headers = headers) data = self.urlopen(url, headers = headers)
self.setCache(cache_key, data) self.setCache(cache_key, data)
except:
return results
if data: if data:
try: try:
@@ -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): def createItems(self, url, cache_key, host, single_cat = False, movie = None, quality = None, for_feed = False):
results = [] results = []
data = self.getCache(cache_key) data = self.getCache(cache_key, url)
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
if data: if data:
try: try:
try: try:
@@ -37,15 +37,7 @@ class NzbIndex(NZBProvider, RSS):
cache_key = 'nzbindex.%s.%s' % (movie['library'].get('identifier'), quality.get('identifier')) cache_key = 'nzbindex.%s.%s' % (movie['library'].get('identifier'), quality.get('identifier'))
data = self.getCache(cache_key) data = self.getCache(cache_key, url)
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
if data: if data:
try: try:
try: try:
@@ -50,15 +50,7 @@ class NZBMatrix(NZBProvider, RSS):
cache_key = 'nzbmatrix.%s.%s' % (movie['library'].get('identifier'), cat_ids) cache_key = 'nzbmatrix.%s.%s' % (movie['library'].get('identifier'), cat_ids)
single_cat = True single_cat = True
data = self.getCache(cache_key) data = self.getCache(cache_key, url)
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
if data: if data:
try: try:
try: try:
+1 -9
View File
@@ -47,15 +47,7 @@ class Nzbs(NZBProvider, RSS):
cache_key = 'nzbs.%s.%s' % (movie['library'].get('identifier'), str(cat_id)) cache_key = 'nzbs.%s.%s' % (movie['library'].get('identifier'), str(cat_id))
data = self.getCache(cache_key) data = self.getCache(cache_key, url)
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
if data: if data:
try: try:
try: try:
+29 -36
View File
@@ -27,45 +27,38 @@ class X264(NZBProvider):
url = self.urls['search'] % quote_plus(q) url = self.urls['search'] % quote_plus(q)
cache_key = 'x264.%s' % q cache_key = 'x264.%s' % q
data = self.getCache(cache_key) data = self.getCache(cache_key, url)
if not data: if data:
data = self.urlopen(url) match = re.compile(self.regex, re.DOTALL).finditer(data)
self.setCache(cache_key, data)
if not data: for nzb in match:
log.error('Failed to get data from %s.' % url) try:
return results age_match = re.match('((?P<day>\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: new['score'] = fireEvent('score.calculate', new, movie, single = True)
try: is_correct_movie = fireEvent('searcher.correct_movie',
age_match = re.match('((?P<day>\d+)d)', nzb.group('age')) nzb = new, movie = movie, quality = quality,
age = age_match.group('day') imdb_results = False, single_category = False, single = True)
except: if is_correct_movie:
age = 1 results.append(new)
self.found(new)
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)
return results return results
@@ -21,7 +21,10 @@ class HDTrailers(TrailerProvider):
movie_name = movie['library']['titles'][0]['title'] movie_name = movie['library']['titles'][0]['title']
url = self.url['api'] % self.movieUrlName(movie_name) url = self.url['api'] % self.movieUrlName(movie_name)
data = self.urlopen(url) try:
data = self.urlopen(url)
except:
return {}
p480 = [] p480 = []
p720 = [] p720 = []
@@ -45,7 +48,10 @@ class HDTrailers(TrailerProvider):
results = {'480p':[], '720p':[], '1080p':[]} results = {'480p':[], '720p':[], '1080p':[]}
url = "%s?%s" % (self.url['backup'], urlencode({'s':movie})) url = "%s?%s" % (self.url['backup'], urlencode({'s':movie}))
data = self.urlopen(url) try:
data = self.urlopen(url)
except:
return results
try: try:
tables = SoupStrainer('div') tables = SoupStrainer('div')
@@ -10,7 +10,10 @@ class AlloCine(UserscriptBase):
if not 'fichefilm_gen_cfilm' in url: if not 'fichefilm_gen_cfilm' in url:
return 'Url isn\'t from a movie' return 'Url isn\'t from a movie'
data = self.urlopen(url) try:
data = self.urlopen(url)
except:
return
html = BeautifulSoup(data) html = BeautifulSoup(data)
title = html.find('title').contents[0].strip() title = html.find('title').contents[0].strip()
@@ -8,7 +8,10 @@ class AppleTrailers(UserscriptBase):
def getMovie(self, url): def getMovie(self, url):
data = self.urlopen(url) try:
data = self.urlopen(url)
except:
return
name = re.search("trailerTitle.*=.*\'(?P<name>.*)\';", data) name = re.search("trailerTitle.*=.*\'(?P<name>.*)\';", data)
name = name.group('name').decode('string_escape') name = name.group('name').decode('string_escape')
@@ -42,7 +42,10 @@ class UserscriptBase(Plugin):
return return
def getMovie(self, url): def getMovie(self, url):
data = self.urlopen(url) try:
data = self.urlopen(url)
except:
data = ''
return self.getInfo(getImdb(data)) return self.getInfo(getImdb(data))
def getInfo(self, identifier): def getInfo(self, identifier):