requests download error handling

This commit is contained in:
Georg Perhofer 2022-12-12 12:15:06 +01:00
parent 690c47a59c
commit 83ddf50e6f

View File

@ -308,7 +308,7 @@ class Download:
def tqdm_download(url, fname, desc): def tqdm_download(url, fname, desc):
r = requests.get(url, allow_redirects=True, stream=True) r = requests.get(url, allow_redirects=True, stream=True)
total = int(r.headers.get("content-length", 0)) total = int(r.headers.get("content-length", 0))
size = 0 download_size = 0
with open(fname, "wb") as file, tqdm( with open(fname, "wb") as file, tqdm(
total=total, total=total,
unit="iB", unit="iB",
@ -320,10 +320,11 @@ def tqdm_download(url, fname, desc):
for data in r.iter_content(chunk_size=1024): for data in r.iter_content(chunk_size=1024):
size = file.write(data) size = file.write(data)
bar.update(size) bar.update(size)
download_size += size
if total != size: if total != download_size:
# https://stackoverflow.com/questions/69919912/requests-iter-content-thinks-file-is-complete-but-its-not # https://stackoverflow.com/questions/69919912/requests-iter-content-thinks-file-is-complete-but-its-not
raise ConnectionError("File download was interrupted.") raise ConnectionError("File download was interrupted for " + fname)
def _get_description(item: dict, track_title, multiple=None): def _get_description(item: dict, track_title, multiple=None):