Created
August 21, 2018 23:58
-
-
Save mouseroot/b0e98b1b55b091f7c7aa1801e23d1b2e to your computer and use it in GitHub Desktop.
Python downloader w/ Requests
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
import requests | |
import sys | |
def download_file(url, filename): | |
with open(filename, "wb") as file: | |
print(f"Downloading {filename}") | |
response = requests.get(url, stream=True) | |
length = response.headers.get("Content-Length") | |
if length is None: | |
file.write(response.content) | |
else: | |
fsize = (int(length) / 1024 / 1024) | |
print("Length: {0:0.2f}MB".format(fsize)) | |
progress = 0 | |
length = int(length) | |
for data in response.iter_content(chunk_size=int(length/100)): | |
progress += len(data) | |
file.write(data) | |
done = int(100 * progress / length) | |
sys.stdout.write("\r[%s%s]" % ('#' * done, ' ' * (100 - done))) | |
sys.stdout.flush() | |
print("Complete") | |
if __name__ == "__main__": | |
if len(sys.argv) > 2: | |
url = sys.argv[1] | |
filename = sys.argv[2] | |
download_file(url, filename) | |
else: | |
print("Downloader") | |
print(f"{sys.argv[0]} <URL> <Filename>") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment