deploy: secure weather caching
This commit is contained in:
+39
-1
@@ -803,7 +803,6 @@ def launch_chrome():
|
||||
"--disable-extensions",
|
||||
"--disable-gpu",
|
||||
"--disable-dev-shm-usage",
|
||||
"--disable-web-security",
|
||||
"--allow-file-access-from-files",
|
||||
f"--user-data-dir={profile_dir}",
|
||||
f"--remote-debugging-port={CDP_PORT}",
|
||||
@@ -827,9 +826,48 @@ if __name__ == "__main__":
|
||||
overlay = KioskOverlay(cdp)
|
||||
chrome_proc = launch_chrome()
|
||||
|
||||
def update_weather_cache():
|
||||
"""Fetch weather data in background via Python and cache as local JSON to bypass CORS securely."""
|
||||
cache_file = _HOME / "kiosk-home" / "weather-data.json"
|
||||
while True:
|
||||
try:
|
||||
# 1. Geolocation lookup
|
||||
req = urllib.request.Request(
|
||||
"https://freeipapi.com/api/json",
|
||||
headers={"User-Agent": "Mozilla/5.0"}
|
||||
)
|
||||
geo_raw = urllib.request.urlopen(req, timeout=8).read()
|
||||
geo = json.loads(geo_raw)
|
||||
|
||||
loc_name = "Your Area"
|
||||
if geo.get("cityName") and geo.get("regionName"):
|
||||
loc_name = f"{geo['cityName']}, {geo['regionName']}"
|
||||
elif geo.get("cityName"):
|
||||
loc_name = geo["cityName"]
|
||||
|
||||
lat, lon = geo.get("latitude"), geo.get("longitude")
|
||||
if lat and lon:
|
||||
# 2. Weather forecast lookup
|
||||
url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t=temperature_2m,relative_humidity_2m,apparent_temperature,weather_code,wind_speed_10m&daily=weather_code,temperature_2m_max,temperature_2m_min&temperature_unit=fahrenheit&wind_speed_unit=mph&precipitation_unit=inch&timezone=auto"
|
||||
fc_raw = urllib.request.urlopen(url, timeout=8).read()
|
||||
fc = json.loads(fc_raw)
|
||||
|
||||
# 3. Save combined cache
|
||||
payload = {"location": loc_name, "forecast": fc}
|
||||
cache_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
cache_file.write_text(json.dumps(payload, indent=2))
|
||||
print("Updated local weather-data.json cache successfully")
|
||||
except Exception as e:
|
||||
print(f"Weather cache background update failed: {e}")
|
||||
|
||||
# Update cache every 15 minutes
|
||||
time.sleep(900)
|
||||
|
||||
def init_cdp():
|
||||
if cdp.wait_ready(timeout=30):
|
||||
print("CDP ready — persistent WebSocket connected successfully")
|
||||
|
||||
threading.Thread(target=update_weather_cache, daemon=True).start()
|
||||
threading.Thread(target=init_cdp, daemon=True).start()
|
||||
|
||||
sys.exit(app.exec_())
|
||||
|
||||
Reference in New Issue
Block a user