http_downloader/http_downloader.py
2022-09-10 13:19:26 +02:00

44 lines
1.6 KiB
Python
Executable File

#!/usr/bin/python3
"""
Usage:
./http_downloader.py <list_of_urls_filepath>
Description:
The program was intended to work as a simple downloader of multiple http urls using wget. The URLs are
given in the first parameter of the program. Then it reads every line of the file and interprets it as
one URL. Each line is passed to wget to download.
"""
import sys
import os
import subprocess
if __name__ == "__main__":
# some checks
if len(sys.argv) < 1: # something malicious, no start from shell! (when sys.argv is an empty list)
exit(1)
if len(sys.argv) == 1:
print(f"Usage: {sys.argv[0]} <list_of_urls_filepath>")
exit(1)
if len(sys.argv) > 2:
print(f"[WARNING] Only the first argument given will be used ({sys.argv[1]})")
# now sys.argv is shurely 2 elements in length; let the real process begin
try:
with open(sys.argv[1]) as f:
print(f"Opened {sys.argv[1]}, reading...")
urls = f.readlines()
for url in urls:
url = url[:len(url)-1] # remove the newline at the end
print(f"Getting {url}: ", end="") # print text, without newline at the end
proc = subprocess.run([f"wget \"{url}\""], shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
if proc.returncode != 0:
print(f"not a valid URL/not existent, skipping!")
else:
print("100%.")
except FileNotFoundError:
print(f"File \"{sys.argv[1]}\" could not be found, maybe not a file or path badly specified?")
exit(1)