From 7ec7d45c48ac9a510a7958323a52c72896f8dd90 Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Wed, 27 Oct 2021 11:11:37 -0700 Subject: [PATCH] Use the requests library to fetch files --- src/web/file_cmds.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/web/file_cmds.py b/src/web/file_cmds.py index f5a6c4f2..918af037 100644 --- a/src/web/file_cmds.py +++ b/src/web/file_cmds.py @@ -219,15 +219,23 @@ def download_to_dir(url, save_dir): Takes str url, str save_dir Returns dict with boolean status and str msg """ - from subprocess import run + import requests + from pathlib import PurePath + file_name = PurePath(url).name + logging.info(f"Making a request to download {url}") - wget_proc = run( - ["wget", url, "-P", save_dir], capture_output=True - ) - if wget_proc.returncode != 0: - stderr = wget_proc.stderr.decode("utf-8") - logging.warning(f"Downloading failed: {stderr}") - return {"status": False, "msg": stderr} + try: + req = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}) + except requests.exceptions.RequestException as e: + logging.warning(f"Request failed: {str(e)}") + return {"status": False, "msg": str(e)} + + with open(f"{save_dir}/{file_name}", "wb") as download: + download.write(req.content) + + logging.info(f"Response encoding: {req.encoding}") + logging.info(f"Response content-type: {req.headers['content-type']}") + logging.info(f"Response status code: {req.status_code}") return {"status": True, "msg": f"{url} downloaded to {save_dir}"}