http_downloader/http_downloader.py
2023-11-06 05:27:24 +00:00

61 lines
2.3 KiB
Python
Executable File

#!/usr/bin/python3
"""
A simple download helper written in python3
Copyright (C) 2023 Benjamin Burkhardt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
"""
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 surely 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)