[F] Fix error during download if content-length is not present
This commit is contained in:
@@ -23,13 +23,18 @@ def download_file(url: str, file: str | Path):
|
|||||||
term_len = os.get_terminal_size().columns
|
term_len = os.get_terminal_size().columns
|
||||||
bar_len = int(term_len * 0.4)
|
bar_len = int(term_len * 0.4)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
term_len = 60
|
||||||
bar_len = 20
|
bar_len = 20
|
||||||
|
|
||||||
|
tqdm_args = dict()
|
||||||
r = requests.get(url, stream=True)
|
r = requests.get(url, stream=True)
|
||||||
|
if 'content-length' in r.headers:
|
||||||
|
tqdm_args['total'] = int(r.headers['content-length']) / 1024 / 1024
|
||||||
|
|
||||||
with open(file, 'wb') as f:
|
with open(file, 'wb') as f:
|
||||||
pbar = tqdm.tqdm(unit=" MB", total=int(r.headers['Content-Length']) / 1024 / 1024,
|
pbar = tqdm.tqdm(unit=" MB", ncols=term_len,
|
||||||
bar_format='{desc} {rate_fmt} {remaining} [{bar}] {percentage:.0f}%', ascii=' #',
|
bar_format='{desc} {rate_fmt} {remaining} [{bar}] {percentage:.0f}%', ascii=' #',
|
||||||
desc=file.name[:bar_len].ljust(bar_len))
|
desc=file.name[:bar_len].ljust(bar_len), **tqdm_args)
|
||||||
for chunk in r.iter_content(chunk_size=chunk_size):
|
for chunk in r.iter_content(chunk_size=chunk_size):
|
||||||
if chunk:
|
if chunk:
|
||||||
pbar.update(len(chunk) / 1024 / 1024)
|
pbar.update(len(chunk) / 1024 / 1024)
|
||||||
|
|||||||
Reference in New Issue
Block a user