The weekday strip now shows today and the next six days (icon, max, min), the current temperature and feels-like sit next to the clock, and the rain graph spans the full width with the hour under every bar. From sunset to sunrise the whole frame, seconds patch included, is drawn as a reverse LCD. The rain label is now spelled DÉŠŤ. Co-Authored-By: Claude Opus 5.5 <[email protected]>
129 lines
3.5 KiB
Python
129 lines
3.5 KiB
Python
"""Open-Meteo forecast for Zlín: fetching and parsing (no drawing)."""
|
|
from dataclasses import dataclass
|
|
from datetime import date, 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 Day:
|
|
date: date
|
|
code: int | None
|
|
high: float | None
|
|
low: float | None
|
|
sunrise: datetime | None # naive local time
|
|
sunset: datetime | None
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Weather:
|
|
temp: float | None
|
|
feels: float | None
|
|
code: int | None
|
|
days: tuple[Day, ...] # as the forecast gives them, today first when fresh
|
|
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 _time(value) -> datetime | None:
|
|
return datetime.fromisoformat(value) if value else None
|
|
|
|
|
|
def parse_weather(payload: dict, now: datetime) -> Weather:
|
|
current = payload.get("current") or {}
|
|
daily = payload.get("daily") or {}
|
|
hourly = payload.get("hourly") or {}
|
|
|
|
days = tuple(
|
|
Day(
|
|
date=date.fromisoformat(day),
|
|
code=_at(daily.get("weather_code"), i),
|
|
high=_at(daily.get("temperature_2m_max"), i),
|
|
low=_at(daily.get("temperature_2m_min"), i),
|
|
sunrise=_time(_at(daily.get("sunrise"), i)),
|
|
sunset=_time(_at(daily.get("sunset"), i)),
|
|
)
|
|
for i, day in enumerate(daily.get("time") or [])
|
|
)
|
|
|
|
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"),
|
|
feels=current.get("apparent_temperature"),
|
|
code=current.get("weather_code"),
|
|
days=days,
|
|
hours=tuple(hours),
|
|
)
|
|
|
|
|
|
def fetch_weather(now: datetime, get=requests.get) -> Weather:
|
|
params = {
|
|
"latitude": LAT,
|
|
"longitude": LON,
|
|
"current": "temperature_2m,weather_code,apparent_temperature",
|
|
"hourly": "precipitation_probability,precipitation",
|
|
"daily": "weather_code,temperature_2m_max,temperature_2m_min,sunrise,sunset",
|
|
"timezone": TIMEZONE,
|
|
"forecast_days": 7,
|
|
}
|
|
response = get(URL, params=params, timeout=10)
|
|
response.raise_for_status()
|
|
return parse_weather(response.json(), now)
|