Files
troupe-standup/wizard-work/ledger/weather_fetch.py
T

32 lines
1.0 KiB
Python

import json
import urllib.request
CITIES = [
("Berlin", 52.52, 13.41),
("Paris", 48.85, 2.35),
("Nairobi", -1.29, 36.82),
("Sao Paulo", -23.55, -46.63),
("Perth", -31.95, 115.86),
("Tokyo", 35.68, 139.76),
]
URL = ("https://api.open-meteo.com/v1/forecast?"
"current=temperature_2m,relative_humidity_2m,wind_speed_10m&timezone=auto")
out = "/root/.hermes/wizard-outputs/ledger/openmeteo-weather.csv"
with open(out, "w") as f:
f.write("city,latitude,longitude,temp_c,humidity_pct,wind_kmh\n")
for name, lat, lon in CITIES:
url = URL + "&latitude=" + str(lat) + "&longitude=" + str(lon)
with urllib.request.urlopen(url, timeout=25) as resp:
d = json.load(resp)
cur = d["current"]
f.write(
f"{name},{lat},{lon},"
f"{cur['temperature_2m']},{cur['relative_humidity_2m']},{cur['wind_speed_10m']}\n"
)
print("fetched", name, "->", cur["temperature_2m"], "C")
print("current time:", cur["time"])
print(open(out).read())