download to tmp file and rename once download is complete

This commit is contained in:
j 2024-03-22 08:48:48 +01:00
parent 0e801f82a3
commit e850296f68

View file

@ -113,14 +113,15 @@ get_url = read_url
def save_url(url, filename, overwrite=False): def save_url(url, filename, overwrite=False):
if not os.path.exists(filename) or overwrite: if not os.path.exists(filename) or overwrite:
dirname = os.path.dirname(filename) dirname = os.path.dirname(filename)
if dirname and not os.path.exists(dirname): os.makedirs(dirname, exist_ok=True)
os.makedirs(dirname)
headers = DEFAULT_HEADERS.copy() headers = DEFAULT_HEADERS.copy()
r = requests.get(url, headers=headers, stream=True) r = requests.get(url, headers=headers, stream=True)
with open(filename, 'wb') as f: filename_tmp = filename + '~'
with open(filename_tmp, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024): for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks if chunk: # filter out keep-alive new chunks
f.write(chunk) f.write(chunk)
os.rename(filename_tmp, filename)
def _get_size(url): def _get_size(url):
req = urllib.request.Request(url, headers=DEFAULT_HEADERS.copy()) req = urllib.request.Request(url, headers=DEFAULT_HEADERS.copy())