Add basic detection/recovery of corrupt partial downloads

This commit is contained in:
Chandler Swift 2025-01-02 22:34:29 -06:00
parent fb1ad615d0
commit fb1d3947b7
Signed by: chandlerswift
GPG key ID: A851D929D52FB93F

View file

@ -1,10 +1,11 @@
#!/usr/bin/env nix-shell
#!nix-shell --quiet -p python3 -i python
#!nix-shell --quiet -p python312 -p python312Packages.pillow -i python
# https://svc.pictometry.com/Image/D2B06344-7A2D-5BD0-FC89-DFDDC9888C41/wmts?SERVICE=WMTS&REQUEST=GetCapabilities
import os
import urllib.request
from PIL import Image
MAX_ZOOM=20
@ -22,7 +23,12 @@ for z in range(12, MAX_ZOOM+1):
file = os.path.normpath(os.path.join(os.path.dirname(__file__), f"../satellite/{z}/{x}/{y}.png"))
os.makedirs(os.path.dirname(file), exist_ok=True)
if os.path.isfile(file):
print(f"{progress} {file} already exists; skipping")
continue
print(f"{progress} downloading {file}")
try: # does it already exist and look good?
Image.open(file).verify() # CRC check on PNGs; nothing on other file types? https://pillow.readthedocs.io/en/latest/_modules/PIL/PngImagePlugin.html#ChunkStream.verify
print(f"{progress} {file} already exists; skipping")
continue
except OSError as e:
print(f"{progress} replacing corrupt {file}")
else:
print(f"{progress} downloading {file}")
urllib.request.urlretrieve(url, file)