Add missing while loop to Curl-Impersonate downloader

This commit is contained in:
rlaphoenix 2024-01-23 09:45:31 +00:00
parent 172ab64017
commit ba93c78b99
1 changed files with 28 additions and 26 deletions

View File

@ -70,34 +70,36 @@ def curl_impersonate(
for url, out_path in uri: for url, out_path in uri:
out_path.parent.mkdir(parents=True, exist_ok=True) out_path.parent.mkdir(parents=True, exist_ok=True)
attempts = 1 attempts = 1
try:
stream = session.get(url, stream=True)
stream.raise_for_status()
with open(out_path, "wb") as f:
written = 0
for chunk in stream.iter_content(chunk_size=1024):
download_size = len(chunk)
f.write(chunk)
written += download_size
if progress:
progress(advance=1)
now = time.time() while True:
time_since = now - last_speed_refresh try:
stream = session.get(url, stream=True)
stream.raise_for_status()
with open(out_path, "wb") as f:
written = 0
for chunk in stream.iter_content(chunk_size=1024):
download_size = len(chunk)
f.write(chunk)
written += download_size
if progress:
progress(advance=1)
download_sizes.append(download_size) now = time.time()
if time_since > 5 or download_size < 1024: time_since = now - last_speed_refresh
data_size = sum(download_sizes)
download_speed = data_size / (time_since or 1) download_sizes.append(download_size)
progress(downloaded=f"{filesize.decimal(download_speed)}/s") if time_since > 5 or download_size < 1024:
last_speed_refresh = now data_size = sum(download_sizes)
download_sizes.clear() download_speed = data_size / (time_since or 1)
break progress(downloaded=f"{filesize.decimal(download_speed)}/s")
except Exception as e: last_speed_refresh = now
if DOWNLOAD_CANCELLED.is_set() or attempts == MAX_ATTEMPTS: download_sizes.clear()
raise e break
time.sleep(RETRY_WAIT) except Exception as e:
attempts += 1 if DOWNLOAD_CANCELLED.is_set() or attempts == MAX_ATTEMPTS:
raise e
time.sleep(RETRY_WAIT)
attempts += 1
return 0 return 0