dfdf
This commit is contained in:
@@ -2,9 +2,12 @@ from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
import sys
|
||||
|
||||
import requests
|
||||
|
||||
from models import ProgressBar
|
||||
|
||||
|
||||
def sanitize_filename(name: str, *, max_len: int = 150) -> str:
|
||||
text = str(name or "").strip()
|
||||
@@ -25,15 +28,45 @@ def download_file(url: str, output_path: Path, *, session: Optional[requests.Ses
|
||||
|
||||
s = session or requests.Session()
|
||||
|
||||
bar = ProgressBar()
|
||||
downloaded = 0
|
||||
total = None
|
||||
|
||||
try:
|
||||
with s.get(url, stream=True, timeout=timeout_s) as resp:
|
||||
resp.raise_for_status()
|
||||
try:
|
||||
total_val = int(resp.headers.get("content-length") or 0)
|
||||
total = total_val if total_val > 0 else None
|
||||
except Exception:
|
||||
total = None
|
||||
|
||||
# Render once immediately so fast downloads still show something.
|
||||
try:
|
||||
bar.update(downloaded=0, total=total, label=str(output_path.name or "download"), file=sys.stderr)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
with open(output_path, "wb") as f:
|
||||
for chunk in resp.iter_content(chunk_size=1024 * 256):
|
||||
if chunk:
|
||||
f.write(chunk)
|
||||
downloaded += len(chunk)
|
||||
try:
|
||||
bar.update(downloaded=downloaded, total=total, label=str(output_path.name or "download"), file=sys.stderr)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
bar.finish()
|
||||
except Exception:
|
||||
pass
|
||||
return output_path.exists() and output_path.stat().st_size > 0
|
||||
except Exception:
|
||||
try:
|
||||
bar.finish()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
if output_path.exists():
|
||||
output_path.unlink()
|
||||
|
||||
Reference in New Issue
Block a user