feat: Open-Meteo parsing and icon mapping
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
839c1824a9
commit
37065a5fba
2 files changed
+209
No files matched your search
+108
@@ -0,0 +1,108 @@
|
||||
"""Open-Meteo forecast for Zlín: fetching and parsing (no drawing)."""
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
import requests
|
||||
|
||||
LAT = 49.2265
|
||||
LON = 17.6627
|
||||
URL = "https://api.open-meteo.com/v1/forecast"
|
||||
TIMEZONE = "Europe/Prague"
|
||||
WINDOW_HOURS = 12
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Hour:
|
||||
time: datetime # naive local time
|
||||
rain_pct: int | None
|
||||
mm: float | None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Weather:
|
||||
temp: float | None
|
||||
code: int | None
|
||||
today_max: float | None
|
||||
today_min: float | None
|
||||
hours: tuple[Hour, ...] # at most WINDOW_HOURS, starting with the hour containing "now"
|
||||
|
||||
|
||||
class Icon(Enum):
|
||||
SUN = "sun"
|
||||
PARTLY = "partly"
|
||||
CLOUDY = "cloudy"
|
||||
RAIN = "rain"
|
||||
SNOW = "snow"
|
||||
STORM = "storm"
|
||||
FOG = "fog"
|
||||
|
||||
|
||||
_WMO_ICONS = {
|
||||
Icon.SUN: (0,),
|
||||
Icon.PARTLY: (1, 2),
|
||||
Icon.CLOUDY: (3,),
|
||||
Icon.FOG: (45, 48),
|
||||
Icon.RAIN: (*range(51, 58), *range(61, 68), 80, 81, 82),
|
||||
Icon.SNOW: (*range(71, 78), 85, 86),
|
||||
Icon.STORM: (95, 96, 99),
|
||||
}
|
||||
_ICON_BY_CODE = {code: icon for icon, codes in _WMO_ICONS.items() for code in codes}
|
||||
|
||||
|
||||
def icon_for(code: int | None) -> Icon | None:
|
||||
return _ICON_BY_CODE.get(code)
|
||||
|
||||
|
||||
def _at(values, i: int):
|
||||
"""values[i] or None when the list is missing, short, or holds a null."""
|
||||
if not isinstance(values, list) or i >= len(values):
|
||||
return None
|
||||
return values[i]
|
||||
|
||||
|
||||
def parse_weather(payload: dict, now: datetime) -> Weather:
|
||||
current = payload.get("current") or {}
|
||||
daily = payload.get("daily") or {}
|
||||
hourly = payload.get("hourly") or {}
|
||||
|
||||
day = now.strftime("%Y-%m-%d")
|
||||
days = daily.get("time") or []
|
||||
d = days.index(day) if day in days else None
|
||||
today_max = _at(daily.get("temperature_2m_max"), d) if d is not None else None
|
||||
today_min = _at(daily.get("temperature_2m_min"), d) if d is not None else None
|
||||
|
||||
hours: list[Hour] = []
|
||||
times = hourly.get("time") or []
|
||||
start = now.strftime("%Y-%m-%dT%H:00")
|
||||
if start in times:
|
||||
first = times.index(start)
|
||||
for i in range(first, min(first + WINDOW_HOURS, len(times))):
|
||||
hours.append(Hour(
|
||||
time=datetime.fromisoformat(times[i]),
|
||||
rain_pct=_at(hourly.get("precipitation_probability"), i),
|
||||
mm=_at(hourly.get("precipitation"), i),
|
||||
))
|
||||
|
||||
return Weather(
|
||||
temp=current.get("temperature_2m"),
|
||||
code=current.get("weather_code"),
|
||||
today_max=today_max,
|
||||
today_min=today_min,
|
||||
hours=tuple(hours),
|
||||
)
|
||||
|
||||
|
||||
def fetch_weather(now: datetime, get=requests.get) -> Weather:
|
||||
params = {
|
||||
"latitude": LAT,
|
||||
"longitude": LON,
|
||||
"current": "temperature_2m,weather_code",
|
||||
"hourly": "precipitation_probability,precipitation",
|
||||
"daily": "temperature_2m_max,temperature_2m_min",
|
||||
"timezone": TIMEZONE,
|
||||
"forecast_days": 2,
|
||||
}
|
||||
response = get(URL, params=params, timeout=10)
|
||||
response.raise_for_status()
|
||||
return parse_weather(response.json(), now)
|
||||
Reference in new issue
Block a user