37 lines
1012 B
Python
37 lines
1012 B
Python
#!/usr/bin/env python3
|
|
"""Verify all plant image URLs work."""
|
|
import json
|
|
import urllib.parse
|
|
import urllib.request
|
|
import urllib.error
|
|
import time
|
|
import sys
|
|
|
|
with open('src/data/plants.json') as f:
|
|
plants = json.load(f)
|
|
|
|
def check_url(url):
|
|
time.sleep(0.5)
|
|
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
|
try:
|
|
resp = urllib.request.urlopen(req, timeout=10)
|
|
ct = resp.headers.get('Content-Type', '')
|
|
cl = resp.headers.get('Content-Length', '?')
|
|
if 'image' in ct:
|
|
return (True, f'HTTP {resp.status} {ct} {cl}B')
|
|
return (False, f'not image: {ct}')
|
|
except urllib.error.HTTPError as e:
|
|
return (False, f'HTTP {e.code}')
|
|
except Exception as e:
|
|
return (False, str(e))
|
|
|
|
all_ok = True
|
|
for plant in plants:
|
|
ok, msg = check_url(plant['imageUrl'])
|
|
status = '✅' if ok else '❌'
|
|
if not ok:
|
|
all_ok = False
|
|
print(f' {plant["id"]:20s} {status} {msg}')
|
|
|
|
sys.exit(0 if all_ok else 1)
|