Avoid crash when downloading files

This commit is contained in:
2Shirt 2021-09-30 15:37:06 -06:00
parent 7391c863f9
commit 625539c786
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -27,7 +27,9 @@ CACHED_DIRS = {}
def download_file(out_path, source_url, as_new=False, overwrite=False): def download_file(out_path, source_url, as_new=False, overwrite=False):
"""Download a file using requests, returns pathlib.Path.""" """Download a file using requests, returns pathlib.Path."""
out_path = pathlib.Path(out_path).resolve() out_path = pathlib.Path(out_path).resolve()
download_msg = f'Downloading {out_path.name}...' name = out_path.name
download_failed = None
download_msg = f'Downloading {name}...'
if as_new: if as_new:
out_path = out_path.with_suffix(f'{out_path.suffix}.new') out_path = out_path.with_suffix(f'{out_path.suffix}.new')
print(download_msg, end='', flush=True) print(download_msg, end='', flush=True)
@ -40,15 +42,17 @@ def download_file(out_path, source_url, as_new=False, overwrite=False):
out_path.parent.mkdir(parents=True, exist_ok=True) out_path.parent.mkdir(parents=True, exist_ok=True)
# Request download # Request download
response = requests.get(source_url, stream=True) try:
if not response.ok: response = requests.get(source_url, stream=True)
name = out_path.name except requests.RequestException as _err:
if as_new: download_failed = _err
name = name[:-4] else:
LOG.error( if not response.ok:
'Failed to download file (status %s): %s', download_failed = response
response.status_code, name,
) # Download failed
if download_failed:
LOG.error('Failed to download file: %s', download_failed)
raise GenericError(f'Failed to download file: {name}') raise GenericError(f'Failed to download file: {name}')
# Write to file # Write to file