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 dfc8c75
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions tools/utils/download_util.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
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)
if 'Content-Length' in response.headers:
with tqdm(total=int(response.headers['Content-Length'])) as progress:
for chunk in response.iter_bytes():
file.write(chunk)
progress.update(len(chunk))
else:
for chunk in response.iter_bytes():
file.write(chunk)
tmp_file_path.rename(file_path)

0 comments on commit dfc8c75

Please sign in to comment.