Use urlopener

This commit is contained in:
Ruud
2011-08-30 16:44:25 +02:00
parent c87f638b18
commit 1f827ef20a
15 changed files with 140 additions and 166 deletions
+14 -16
View File
@@ -34,26 +34,24 @@ class FileManager(Plugin):
def download(self, url = '', dest = None, overwrite = False):
try:
file = urllib2.urlopen(url)
file = self.urlopen(url)
if not file:
log.error('File is empty, don\'t download')
return False
if not dest: # to Cache
dest = os.path.join(Env.get('cache_dir'), '%s.%s' % (md5(url), getExt(url)))
if not dest: # to Cache
dest = os.path.join(Env.get('cache_dir'), '%s.%s' % (md5(url), getExt(url)))
if overwrite or not os.path.exists(dest):
log.debug('Writing file to: %s' % dest)
output = open(dest, 'wb')
output.write(file.read())
output.close()
else:
log.debug('File already exists: %s' % dest)
if overwrite or not os.path.exists(dest):
log.debug('Writing file to: %s' % dest)
output = open(dest, 'wb')
output.write(file)
output.close()
else:
log.debug('File already exists: %s' % dest)
return dest
return dest
except Exception:
log.error('Unable to download file "%s": %s' % (url, traceback.format_exc()))
return False
def add(self, path = '', part = 1, type = (), available = 1, properties = {}):
db = get_session()