Improve naming

This commit is contained in:
Ivan Habunek 2024-08-28 11:02:12 +02:00
parent 1658aba124
commit 5679e66270
No known key found for this signature in database
GPG Key ID: 01DB3DD0D824504C

View File

@ -11,29 +11,29 @@ CONNECT_TIMEOUT = 5
RETRY_COUNT = 5 RETRY_COUNT = 5
def _download(url: str, path: Path): def _download(url: str, target: Path):
tmp_path = Path(str(path) + ".tmp") tmp_path = Path(str(target) + ".tmp")
size = 0 size = 0
with httpx.stream("GET", url, timeout=CONNECT_TIMEOUT, follow_redirects=True) as response: with httpx.stream("GET", url, timeout=CONNECT_TIMEOUT, follow_redirects=True) as response:
response.raise_for_status() response.raise_for_status()
with open(tmp_path, "wb") as target: with open(tmp_path, "wb") as f:
for chunk in response.iter_bytes(chunk_size=CHUNK_SIZE): for chunk in response.iter_bytes(chunk_size=CHUNK_SIZE):
target.write(chunk) f.write(chunk)
size += len(chunk) size += len(chunk)
os.rename(tmp_path, path) os.rename(tmp_path, target)
return size return size
def download_file(url: str, path: Path, retries: int = RETRY_COUNT) -> Tuple[int, bool]: def download_file(url: str, target: Path, retries: int = RETRY_COUNT) -> Tuple[int, bool]:
if path.exists(): if target.exists():
from_disk = True from_disk = True
return os.path.getsize(path), from_disk return os.path.getsize(target), from_disk
from_disk = False from_disk = False
for _ in range(retries): for _ in range(retries):
try: try:
return _download(url, path), from_disk return _download(url, target), from_disk
except httpx.RequestError: except httpx.RequestError:
pass pass