Download file with progress bar
(url, save_path)
| 398 | |
| 399 | |
| 400 | def download_file(url, save_path): |
| 401 | """Download file with progress bar""" |
| 402 | try: |
| 403 | response = requests.get(url, stream=True) |
| 404 | response.raise_for_status() |
| 405 | |
| 406 | total_size = int(response.headers.get("content-length", 0)) |
| 407 | progress_bar = tqdm( |
| 408 | total=total_size, |
| 409 | unit="iB", |
| 410 | unit_scale=True, |
| 411 | desc=f"Downloading {os.path.basename(url)}", |
| 412 | ) |
| 413 | |
| 414 | with open(save_path, "wb") as f: |
| 415 | for chunk in response.iter_content(chunk_size=1024): |
| 416 | if chunk: # filter out keep-alive chunks |
| 417 | f.write(chunk) |
| 418 | progress_bar.update(len(chunk)) |
| 419 | |
| 420 | progress_bar.close() |
| 421 | return True |
| 422 | except Exception as e: |
| 423 | if os.path.exists(save_path): |
| 424 | os.remove(save_path) |
| 425 | raise RuntimeError(f"Download failed: {e!s}") |
| 426 | |
| 427 | |
| 428 | def extract_tar(tar_path, output_dir): |