def download(url, filename, progress_data=None, session=None, silent=False):
"""
Initiate a file download and display the progress
Args:
url(str): Download URL
filename(str): Path to save the file to
progress_data(dict): Static information to display above the progress bar
session(Session): An optional download session to use
silent(bool): Download the file, but don't print any output
Returns:
"""
# Set up our requests session and make sure the filepath exists
session = session or Session()
os.makedirs(os.path.dirname(filename), 0o755, True)
# Test the connection
response = session.head(url, allow_redirects=True) # type: Response
response.raise_for_status()
# Get some information about the file we are downloading
filesize = naturalsize(response.headers.get('content-length', 0))
filetype = response.headers.get('content-type', 'Unknown')
# Format the information output
info_lines = [
click.style('Saving to: ', bold=True) + filename,
click.style('File type: ', bold=True) + filetype,
click.style('File size: ', bold=True) + filesize
]
if progress_data:
for key, value in progress_data.items():
info_lines.append('{key} {value}'.format(key=click.style(key + ':', bold=True), value=value))
# Print the static information now
click.echo()
for line in info_lines:
click.echo(line)
# Now let's make the real download request
response = session.get(url, allow_redirects=True) # type: Response
# Process the download
with open(filename, 'wb') as file:
length = int(response.headers.get('content-length', 0))
with click.progressbar(response.iter_content(1024), (length / 1024)) as progress:
for chunk in progress:
if chunk:
file.write(chunk)
file.flush()
评论列表
文章目录