Skip to content

Commit

Permalink
优化下载工具类,添加进度条
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Feb 4, 2025
1 parent a655dae commit 43e2d19
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions tools/utils/download_util.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from pathlib import Path

import httpx
from tqdm import tqdm


def download_file(url: str, file_path: Path):
with httpx.stream('GET', url, follow_redirects=True) as response:
assert response.is_success, url
tmp_file_path = file_path.with_suffix(f'{file_path.suffix}.download')
with tmp_file_path.open('wb') as file:
for chunk in response.iter_raw(1024):
file.write(chunk)
total = int(response.headers['Content-Length']) if 'Content-Length' in response.headers else 0
with tqdm(total=total) as progress:
for chunk in response.iter_bytes():
file.write(chunk)
progress.update(len(chunk))
tmp_file_path.rename(file_path)

0 comments on commit 43e2d19

Please sign in to comment.