1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
26 def download_file(url, destination=None, logger=None, progress_handler=None):
30 :param url: URL from which to download
32 :param destination: path where the file should be saved or ``None`` to auto-generate
33 :type destination: basestring
34 :returns: path where the file was saved
36 :raises exceptions.IOError:
37 :raises requests.exceptions.RequestException:
42 file_descriptor, destination = tempfile.mkstemp()
43 os.close(file_descriptor)
45 logger.info('Downloading {0} to {1}...'.format(url, destination))
47 response = requests.get(url, stream=True)
48 final_url = response.url
49 if final_url != url and logger:
50 logger.debug('Redirected to {0}'.format(final_url))
53 total_size = int(response.headers['Content-Length']) \
54 if 'Content-Length' in response.headers else None
56 with open(destination, 'wb') as destination_file:
57 for chunk in response.iter_content(chunk_size):
58 destination_file.write(chunk)
59 if total_size and progress_handler:
60 # Only showing progress bar if we have the total content length
61 read_bytes += chunk_size
62 progress_handler(read_bytes, total_size)