Make downloads atomic

Should prevent corrupted/semi-downloaded files when interrupting the
download script while it's writing a file.
This commit is contained in:
Joscha 2023-04-27 00:08:28 +02:00
parent 060e3bd868
commit 7f12e083e8

View file

@ -17,6 +17,7 @@ def log(ids, n, id, msg):
def download(ids, n, id, path, link): def download(ids, n, id, path, link):
tmppath = path.parent / (path.name + ".tmp")
try: try:
r = requests.get(link) r = requests.get(link)
if r.status_code == 404: if r.status_code == 404:
@ -25,8 +26,9 @@ def download(ids, n, id, path, link):
elif r.status_code != 200: elif r.status_code != 200:
log(ids, n, id, f"Weird status code: {r.status_code}") log(ids, n, id, f"Weird status code: {r.status_code}")
return return
with open(path, "wb") as f: with open(tmppath, "wb") as f:
f.write(r.content) f.write(r.content)
tmppath.rename(path)
log(ids, n, id, "Downloaded") log(ids, n, id, "Downloaded")
except Exception as e: except Exception as e:
log(ids, n, id, f"Error fetching {link}: {e}") log(ids, n, id, f"Error fetching {link}: {e}")