From e94ffaa1ac25906115ad8aafc6cfb589966319a3 Mon Sep 17 00:00:00 2001 From: Cunliang Geng Date: Mon, 28 Aug 2023 12:09:48 +0200 Subject: [PATCH] fix download bugs - if http request fails, remove the target file automatically but not leave a empty target file - download decoded content but not raw content --- src/nplinker/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/nplinker/utils.py b/src/nplinker/utils.py index e402194b..7a8b3216 100644 --- a/src/nplinker/utils.py +++ b/src/nplinker/utils.py @@ -173,6 +173,7 @@ def download_url(url: str, url, follow_redirects=allow_http_redirect) as response: if not response.is_success: + fpath.unlink(missing_ok=True) raise RuntimeError( f"Failed to download url {url} with status code {response.status_code}" ) @@ -182,7 +183,7 @@ def download_url(url: str, unit_divisor=1024, unit="B") as progress: num_bytes_downloaded = response.num_bytes_downloaded - for chunk in response.iter_raw(): + for chunk in response.iter_bytes(): fh.write(chunk) progress.update(response.num_bytes_downloaded - num_bytes_downloaded)