Created
January 6, 2017 13:03
-
-
Save MiloszKrajewski/b4164144b0a332b0e99131c0f8e4523f to your computer and use it in GitHub Desktop.
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
## NOTE: pip install requests | |
import requests | |
import sys | |
import argparse | |
from shutil import copyfileobj | |
from urllib.parse import urlparse | |
from os.path import basename, isfile | |
def parse(argv): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("url", help="source url") | |
parser.add_argument("file", help="target file", nargs="?", default=None) | |
parser.add_argument("--force", help="force download", action="store_true", default=False) | |
return parser.parse_args(argv) | |
def main(args): | |
url = args.url | |
filename = args.file or basename(urlparse(url).path) | |
if args.force or (not isfile(filename)): | |
print("Downloading: {}".format(url)) | |
response = requests.get(url) | |
print("Saving: {}".format(filename)) | |
with open(filename, 'wb') as target: | |
target.write(response.content) | |
else: | |
print("Already exists: {}".format(filename)) | |
if __name__=="__main__": | |
sys.exit(main(parse(sys.argv[1:]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment