def download_external_url(url, download_to_file, content_type=None,<EOL>retry_count=<NUM_LIT:10>, timeout=<NUM_LIT>, verbose=False):
error_str = "<STR_LIT>"<EOL>error_code = None<EOL>retry_attempt = <NUM_LIT:0><EOL>while retry_attempt < retry_count:<EOL><INDENT>try:<EOL><INDENT>request = open_url(url)<EOL>if request.code == <NUM_LIT:200> and "<STR_LIT>" in request.headers:<EOL><INDENT>try:<EOL><INDENT>retry_after = int(request.headers["<STR_LIT>"])<EOL>retry_after = max(retry_after, timeout)<EOL><DEDENT>except ValueError:<EOL><INDENT>retry_after = timeout<EOL><DEDENT>if verbose:<EOL><INDENT>msg = "<STR_LIT>" % (retry_after,)<EOL>print >> sys.stderr, msg<EOL><DEDENT>time.sleep(retry_after)<EOL>retry_attempt += <NUM_LIT:1><EOL>continue<EOL><DEDENT><DEDENT>except urllib2.HTTPError as e:<EOL><INDENT>error_code = e.code<EOL>error_str = str(e)<EOL>retry_after = timeout<EOL>if e.code == <NUM_LIT> and "<STR_LIT>" in e.headers:<EOL><INDENT>try:<EOL><INDENT>retry_after = int(e.headers["<STR_LIT>"])<EOL>retry_after = max(retry_after, timeout)<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if verbose:<EOL><INDENT>msg = "<STR_LIT>" % (retry_after,)<EOL>print >> sys.stderr, msg<EOL><DEDENT>time.sleep(retry_after)<EOL>retry_attempt += <NUM_LIT:1><EOL><DEDENT>except (urllib2.URLError,<EOL>socket.timeout,<EOL>socket.gaierror,<EOL>socket.error) as e:<EOL><INDENT>if verbose:<EOL><INDENT>error_str = str(e)<EOL>msg = "<STR_LIT>" % (timeout,)<EOL>print >> sys.stderr, msg<EOL><DEDENT>time.sleep(timeout)<EOL>retry_attempt += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>finalize_download(url, download_to_file, content_type, request)<EOL><DEDENT>finally:<EOL><INDENT>request.close()<EOL><DEDENT>return download_to_file<EOL><DEDENT><DEDENT>msg = '<STR_LIT>' % (error_str,)<EOL>raise InvenioFileDownloadError(msg, code=error_code)<EOL>
Download a url (if it corresponds to a remote file) and return a
local url to it. If format is specified, a check will be performed
in order to make sure that the format of the downloaded file is equal
to the expected format.

@param url: the URL to download
@type url: string

@param download_to_file: the path to download the file to
@type download_to_file: string

@param content_type: the content_type of the file (optional)
@type content_type: string

@param retry_count: max number of retries for downloading the file
@type retry_count: int

@param timeout: time to sleep in between attemps
@type timeout: int

@return: the path to the download local file
@rtype: string
@raise StandardError: if the download failed