Created
July 12, 2016 00:32
-
-
Save DrDougPhD/b234a734b123754385af1a6e49e87e53 to your computer and use it in GitHub Desktop.
Example code for a textual download progress bar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
DESCRIPTION | |
Sample script to show progress of a large file download. | |
EXAMPLE | |
$ python download_progress.py | |
71% |################# | Elapsed Time: 0:00:45 | ETA: 0:00:15 683.9 KiB/s | |
AUTHOR | |
Doug McGeehan <doug.mcgeehan@mst.edu> | |
HELPFUL SOURCES | |
http://pythonhosted.org/progressbar2/examples.html | |
http://stackoverflow.com/a/16696317 | |
LICENSE | |
Public Domain | |
""" | |
# Sorry NASA. I need a big file for demonstration. | |
URL="http://imgsrc.hubblesite.org/hu/db/images/hs-2005-35-a-full_tif.tif" | |
SAVE_TO="outputfile.tif" | |
import requests | |
from progressbar import ProgressBar | |
from progressbar import Percentage | |
from progressbar import Bar | |
from progressbar import Timer | |
from progressbar import ETA | |
from progressbar import AdaptiveTransferSpeed | |
import zipfile | |
import re | |
if __name__ == "__main__": | |
downloader = requests.get(URL, stream=True) | |
filename = | |
download_size = int(downloader.headers.get('content-length')) | |
amount_downloaded = 0 | |
widgets = [ | |
Percentage(), | |
' ', Bar(), | |
' ', Timer(), | |
' | ', ETA(), | |
' ', AdaptiveTransferSpeed(), | |
] | |
download_progress = ProgressBar(widgets=widgets, max_value=download_size) | |
download_progress.start() | |
with open(SAVE_TO, "wb") as f: | |
for chunk in downloader.iter_content(chunk_size=4098): | |
if chunk: | |
f.write(chunk) | |
f.flush() | |
amount_downloaded += len(chunk) | |
download_progress.update(amount_downloaded) | |
download_progress.finish() | |
unzipper = zipfile.ZipFile(SAVE_TO, 'r') | |
first_bad_file = unzipper.testzip() | |
unzipper.close() | |
if first_bad_file is not None: | |
os.remove(SAVE_TO) | |
sys.exit("The downloaded ZIP has a corrupt file: {0}".format( | |
first_bad_file | |
)) | |
else: | |
print("Download successful!") | |
# Delete file. | |
os.remove(SAVE_TO) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment