Use sessions in requests when downloading files

This commit is contained in:
2Shirt 2022-03-10 15:36:58 -07:00
parent b750432381
commit 2a70997a6c
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -17,6 +17,12 @@ from wk.std import GenericError
# STATIC VARIABLES
ARCH = '64' if platform.architecture()[0] == '64bit' else '32'
LOG = logging.getLogger(__name__)
HEADERS = {
'User-Agent': (
'Mozilla/5.0 (X11; Linux x86_64; rv:97.0) '
'Gecko/20100101 Firefox/97.0'
),
}
# "GLOBAL" VARIABLES
@ -42,13 +48,14 @@ def download_file(out_path, source_url, as_new=False, overwrite=False):
out_path.parent.mkdir(parents=True, exist_ok=True)
# Request download
try:
response = requests.get(source_url, stream=True)
except requests.RequestException as _err:
download_failed = _err
else:
if not response.ok:
download_failed = response
with requests.Session() as session:
try:
response = session.get(source_url, headers=HEADERS, stream=True)
except requests.RequestException as _err:
download_failed = _err
else:
if not response.ok:
download_failed = response
# Download failed
if download_failed: