37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from tqdm import tqdm
|
|
import httpx
|
|
from app.utils.uploader import ChunkUploadBackend
|
|
|
|
|
|
class HttpStreamingDownloader:
|
|
def __init__(self, backend: ChunkUploadBackend):
|
|
self.backend = backend
|
|
|
|
def download(
|
|
self,
|
|
url: str,
|
|
filename: str,
|
|
headers: dict = {},
|
|
chunk_size: int = 1024**2,
|
|
):
|
|
self.backend.start(filename)
|
|
http = httpx.Client(http2=True)
|
|
try:
|
|
print("Try to download")
|
|
with http.stream("GET", url, timeout=20, headers=headers) as response:
|
|
response.raise_for_status()
|
|
total = int(response.headers.get("Content-Length", 0))
|
|
with tqdm(
|
|
total=total, unit="B", unit_scale=True, desc=filename
|
|
) as pbar:
|
|
for chunk in response.iter_bytes(chunk_size):
|
|
if chunk:
|
|
self.backend.upload_chunk(chunk)
|
|
# обновляем прогресс по размеру чанка
|
|
pbar.update(len(chunk))
|
|
|
|
self.backend.finish()
|
|
except:
|
|
self.backend.abort()
|
|
raise
|