Move the downloader return code to a class member

This makes it possible to initialize it with value zero and later let the
trouble() overwrite the value. It simplifies error treatment and paves the way
for the InfoExtracto objects to call process_info() themselves, which should
solve the issues with tor and some other problems.
This commit is contained in:
Ricardo Garcia 2009-04-23 21:43:04 +02:00
parent 2f4d18a9f7
commit 9bf386d74b

View File

@ -95,11 +95,13 @@ class FileDownloader(object):
params = None params = None
_ies = [] _ies = []
_pps = [] _pps = []
_download_retcode = None
def __init__(self, params): def __init__(self, params):
"""Create a FileDownloader object with the given options.""" """Create a FileDownloader object with the given options."""
self._ies = [] self._ies = []
self._pps = [] self._pps = []
self._download_retcode = 0
self.params = params self.params = params
@staticmethod @staticmethod
@ -203,15 +205,13 @@ class FileDownloader(object):
Depending on if the downloader has been configured to ignore Depending on if the downloader has been configured to ignore
download errors or not, this method may throw an exception or download errors or not, this method may throw an exception or
not when errors are found, after printing the message. If it not when errors are found, after printing the message.
doesn't raise, it returns an error code suitable to be returned
later as a program exit code to indicate error.
""" """
if message is not None: if message is not None:
self.to_stderr(message) self.to_stderr(message)
if not self.params.get('ignoreerrors', False): if not self.params.get('ignoreerrors', False):
raise DownloadError(message) raise DownloadError(message)
return 1 self._download_retcode = 1
def slow_down(self, start_time, byte_counter): def slow_down(self, start_time, byte_counter):
"""Sleep if the download speed is over the rate limit.""" """Sleep if the download speed is over the rate limit."""
@ -249,41 +249,45 @@ class FileDownloader(object):
# Do nothing else if in simulate mode # Do nothing else if in simulate mode
if self.params.get('simulate', False): if self.params.get('simulate', False):
return 0 return
try: try:
filename = self.params['outtmpl'] % info_dict filename = self.params['outtmpl'] % info_dict
self.report_destination(filename) self.report_destination(filename)
except (ValueError, KeyError), err: except (ValueError, KeyError), err:
return self.trouble('ERROR: invalid output template or system charset: %s' % str(err)) self.trouble('ERROR: invalid output template or system charset: %s' % str(err))
if self.params['nooverwrites'] and os.path.exists(filename): if self.params['nooverwrites'] and os.path.exists(filename):
self.to_stderr('WARNING: file exists: %s; skipping' % filename) self.to_stderr('WARNING: file exists: %s; skipping' % filename)
return 0 return
try: try:
self.pmkdir(filename) self.pmkdir(filename)
except (OSError, IOError), err: except (OSError, IOError), err:
return self.trouble('ERROR: unable to create directories: %s' % str(err)) self.trouble('ERROR: unable to create directories: %s' % str(err))
return
try: try:
outstream = open(filename, 'wb') outstream = open(filename, 'wb')
except (OSError, IOError), err: except (OSError, IOError), err:
return self.trouble('ERROR: unable to open for writing: %s' % str(err)) self.trouble('ERROR: unable to open for writing: %s' % str(err))
return
try: try:
self._do_download(outstream, info_dict['url']) self._do_download(outstream, info_dict['url'])
outstream.close() outstream.close()
except (OSError, IOError), err: except (OSError, IOError), err:
return self.trouble('ERROR: unable to write video data: %s' % str(err)) self.trouble('ERROR: unable to write video data: %s' % str(err))
return
except (urllib2.URLError, httplib.HTTPException, socket.error), err: except (urllib2.URLError, httplib.HTTPException, socket.error), err:
return self.trouble('ERROR: unable to download video data: %s' % str(err)) self.trouble('ERROR: unable to download video data: %s' % str(err))
return
try: try:
self.post_process(filename, info_dict) self.post_process(filename, info_dict)
except (PostProcessingError), err: except (PostProcessingError), err:
return self.trouble('ERROR: postprocessing: %s' % str(err)) self.trouble('ERROR: postprocessing: %s' % str(err))
return
return 0 return
def download(self, url_list): def download(self, url_list):
"""Download a given list of URLs.""" """Download a given list of URLs."""
retcode = 0
if len(url_list) > 1 and self.fixed_template(): if len(url_list) > 1 and self.fixed_template():
raise SameFileError(self.params['outtmpl']) raise SameFileError(self.params['outtmpl'])
@ -303,7 +307,7 @@ class FileDownloader(object):
# See if there were problems extracting any information # See if there were problems extracting any information
if len(results) != len(all_results): if len(results) != len(all_results):
retcode = self.trouble() self.trouble()
# Two results could go to the same file # Two results could go to the same file
if len(results) > 1 and self.fixed_template(): if len(results) > 1 and self.fixed_template():
@ -311,19 +315,15 @@ class FileDownloader(object):
# Process each result # Process each result
for result in results: for result in results:
result = self.process_info(result) self.process_info(result)
# Do not overwrite an error code with a success code
if result != 0:
retcode = result
# Suitable InfoExtractor had been found; go to next URL # Suitable InfoExtractor had been found; go to next URL
break break
if not suitable_found: if not suitable_found:
retcode = self.trouble('ERROR: no suitable InfoExtractor: %s' % url) self.trouble('ERROR: no suitable InfoExtractor: %s' % url)
return retcode return self._download_retcode
def post_process(self, filename, ie_info): def post_process(self, filename, ie_info):
"""Run the postprocessing chain on the given file.""" """Run the postprocessing chain on the given file."""