Skip to content

Instantly share code, notes, and snippets.

@megat69
Last active April 15, 2021 15:18
Show Gist options
  • Save megat69/f182f49f08981478cd2195444d5ab3c3 to your computer and use it in GitHub Desktop.
Save megat69/f182f49f08981478cd2195444d5ab3c3 to your computer and use it in GitHub Desktop.
Multiple files downloader from URL

Multiple files downloader from URL

Requirements

Please install the libraries requests and tqdm first.

A version without TQDM (the progress bars library) is also available. This version will use prints instead, and is less nice for the user.

Instructions

Run this file from the command prompt

Available parameters :

  • -f : Download all files listed in a file.
    • Alias : --filename.
    • Syntax : python files_downloader.py -f <filename>
  • -h : Display this help.
    • Alias : --help.

Regular use :

  • Indicate the links to download from. If multiple, they need to be separated by spaces.
  • Syntax : python files_downloader.py <link1> [link2]...

How should be composed the file with the URL list ?

Just a regular plain text file, each line contains a link.

import sys
import requests
import os
from tqdm import tqdm, trange
def download_file(url):
r = requests.get(url)
assert r.status_code == 200, "Something happened.\nStatus code : " + str(r.status_code)
with open(url.split("/")[-1], "wb") as f:
f.write(r.content)
print("Downloaded", url)
# Displaying help
if len(sys.argv) == 1 or sys.argv[1].lower() == "-h" \
or sys.argv[1].lower() == "--help":
print("Available parameters :\n"
f"-f : Download all files listed in a file.\n\tAlias : --filename.\n\tSyntax : python {sys.argv[0]} -f <filename>\n"
"-h : Display this help.\n\tAlias : --help.\n"
"\n"
f"Regular use :\n\tIndicate the links to download from. If multiple, they need to be separated by spaces.\n\tSyntax : python {sys.argv[0]} <link1> [link2]...")
# Download from file
elif sys.argv[1].lower() == "-f" or sys.argv[1].lower() == "--filename":
assert len(sys.argv) >= 3, "You didn't specify a filename"
assert os.path.exists(sys.argv[2]), f"The file you specified ('{sys.argv[2]}') doesn't exist"
assert os.path.isfile(sys.argv[2]), f"'{sys.argv[2]}' is a folder, not a file."
# Getting the file contents
with open(sys.argv[2], "r") as f:
links = f.readlines()
bar = trange(len(links))
for i in bar:
# Formatting the strings
links[i] = links[i].strip().replace("\n", "")
bar.set_description(f"\nDownloading {links[i].split('/')[-1]}...")
# Downloading each file
download_file(links[i])
else:
for link in sys.argv[1:]:
download_file(link)
import sys
import requests
import os
def download_file(url):
r = requests.get(url)
assert r.status_code == 200, "Something happened.\nStatus code : " + str(r.status_code)
with open(url.split("/")[-1], "wb") as f:
f.write(r.content)
print("Downloaded", url)
# Displaying help
if len(sys.argv) == 1 or sys.argv[1].lower() == "-h" \
or sys.argv[1].lower() == "--help":
print("Available parameters :\n"
f"-f : Download all files listed in a file.\n\tAlias : --filename.\n\tSyntax : python {sys.argv[0]} -f <filename>\n"
"-h : Display this help.\n\tAlias : --help.\n"
"\n"
f"Regular use :\n\tIndicate the links to download from. If multiple, they need to be separated by spaces.\n\tSyntax : python {sys.argv[0]} <link1> [link2]...")
# Download from file
elif sys.argv[1].lower() == "-f" or sys.argv[1].lower() == "--filename":
assert len(sys.argv) >= 3, "You didn't specify a filename"
assert os.path.exists(sys.argv[2]), f"The file you specified ('{sys.argv[2]}') doesn't exist"
assert os.path.isfile(sys.argv[2]), f"'{sys.argv[2]}' is a folder, not a file."
# Getting the file contents
with open(sys.argv[2], "r") as f:
links = f.readlines()
for i in range(len(links)):
# Formatting the strings
links[i] = links[i].strip().replace("\n", "")
print(f"\nDownloading {links[i].split('/')[-1]}...")
# Downloading each file
download_file(links[i])
else:
for link in sys.argv[1:]:
download_file(link)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment