wmts-exfiltrator/pkg/thread.py

71 lines
3.0 KiB
Python

import shutil
from pathlib import Path
import requests
from tqdm import tqdm
from pkg.proxies import PROXIES
from .image import convert_to_png, is_png
def del_path(p: Path):
if p.is_file() or p.is_symlink():
p.unlink(missing_ok=True)
else:
shutil.rmtree(p)
def download_tile(task):
row, col, base_url, r_headers, output, use_proxy, do_convert_to_png = task
corrupted_image = False
try:
output_path: Path = output / f"{row}_{col}.png"
if output_path.exists():
valid_png_file, image_type = is_png(output_path)
if do_convert_to_png and image_type and image_type != 'PNG':
# The image was read sucessfully by PIL but it's in the wrong format.
coverted = convert_to_png(output_path)
if not is_png(output_path):
tqdm.write(f'PNG conversion for {output_path} failed. Deleting and retrying...')
corrupted_image = True
else:
return row, col, 'converted' if coverted else 'exist'
elif not valid_png_file:
# We will re-download the image. Don't need to delete it, just overwrite it.
# del_path(output_path)
corrupted_image = True
tqdm.write(f'Bad image file: "{output_path}" (is format: {image_type}), deleting and retrying...')
else:
return row, col, 'exist'
tile_url = f"{base_url}/{row}/{col}".replace('//', '/').replace(':/', '://')
response = requests.get(tile_url, headers=r_headers, proxies=PROXIES if use_proxy else None, timeout=60)
if response.status_code == 200:
# if not do_convert_to_png and not response.headers.get('Content-Type') == 'image/png':
# # If we will convert the image to a PNG, ignore this header.
# raise Exception(f'Response gave Content-Type: {response.headers.get("Content-Type")}')
with open(output_path, "wb") as f:
f.write(response.content)
if do_convert_to_png:
convert_to_png(output_path)
if not is_png(output_path)[0]:
tqdm.write(f'PNG conversion for {output_path} failed')
else:
return row, col, 'success' if not corrupted_image else 'fixed'
else:
# Recheck the PNG if it was corrupted.
valid_png_file, image_type = is_png(output_path)
if not valid_png_file:
tqdm.write(f'Bad image file: "{output_path}" (is format: {image_type}).')
return row, col, 'failure'
else:
return row, col, 'success' if not corrupted_image else 'fixed'
else:
print(f"Failed to download tile {row}_{col}")
return row, col, 'failure'
except Exception as e:
# import traceback
# traceback.print_exc()
tqdm.write(f'Exception on {(row, col)} - {e.__class__.__name__}: {e}')
return row, col, 'failure'