feat: weather card — precip probability %, condition text, unit-aware wind
- Fetch precipitation_probability_max from Open-Meteo (replaces precip_sum in the card display — probability is more useful at a glance than mm) - Show WMO condition description text on each forecast day - Convert wind speed to mph when temp unit is F; pass wind_unit in response - Display 💧 X% chance of rain; 💨 X mph/km/h wind per day Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -15,7 +15,7 @@ NOMINATIM_URL = "https://nominatim.openstreetmap.org/search"
|
||||
OPEN_METEO_URL = "https://api.open-meteo.com/v1/forecast"
|
||||
OPEN_METEO_DAILY = (
|
||||
"temperature_2m_max,temperature_2m_min,precipitation_sum,"
|
||||
"weathercode,windspeed_10m_max"
|
||||
"precipitation_probability_max,weathercode,windspeed_10m_max"
|
||||
)
|
||||
|
||||
# WMO weather code → description (subset; covers the most common codes)
|
||||
@@ -42,12 +42,14 @@ def parse_forecast(raw: dict) -> list[dict]:
|
||||
"""Convert Open-Meteo daily response into a clean list of day dicts."""
|
||||
daily = raw.get("daily", {})
|
||||
dates = daily.get("time", [])
|
||||
precip_prob = daily.get("precipitation_probability_max", [])
|
||||
return [
|
||||
{
|
||||
"date": dates[i],
|
||||
"temp_max": daily.get("temperature_2m_max", [])[i],
|
||||
"temp_min": daily.get("temperature_2m_min", [])[i],
|
||||
"precip_mm": daily.get("precipitation_sum", [])[i],
|
||||
"precip_probability": precip_prob[i] if i < len(precip_prob) else None,
|
||||
"weathercode": daily.get("weathercode", [])[i],
|
||||
"description": describe_weathercode(daily.get("weathercode", [])[i]),
|
||||
"windspeed_max": daily.get("windspeed_10m_max", [])[i],
|
||||
@@ -119,10 +121,13 @@ def parse_weather_card_data(
|
||||
yesterday_day = next((d for d in days if d["date"] == yesterday_str), None)
|
||||
future_days = [d for d in days if d["date"] > today_str][:5]
|
||||
|
||||
imperial = temp_unit == "F"
|
||||
|
||||
def to_temp(c: float) -> int:
|
||||
if temp_unit == "F":
|
||||
return round(c * 9 / 5 + 32)
|
||||
return round(c)
|
||||
return round(c * 9 / 5 + 32) if imperial else round(c)
|
||||
|
||||
def to_wind(kmh: float) -> int:
|
||||
return round(kmh * 0.621371) if imperial else round(kmh)
|
||||
|
||||
def day_label(date_str: str) -> str:
|
||||
from datetime import date as _date
|
||||
@@ -131,6 +136,8 @@ def parse_weather_card_data(
|
||||
except ValueError:
|
||||
return date_str
|
||||
|
||||
wind_unit = "mph" if imperial else "km/h"
|
||||
|
||||
return {
|
||||
"location": getattr(cache_row, "location_label", ""),
|
||||
"fetched_at": cache_row.fetched_at.isoformat(),
|
||||
@@ -140,14 +147,15 @@ def parse_weather_card_data(
|
||||
"today_low": to_temp(today_day["temp_min"]) if today_day else None,
|
||||
"yesterday_high": to_temp(yesterday_day["temp_max"]) if yesterday_day else None,
|
||||
"yesterday_low": to_temp(yesterday_day["temp_min"]) if yesterday_day else None,
|
||||
"wind_unit": wind_unit,
|
||||
"forecast": [
|
||||
{
|
||||
"day": day_label(d["date"]),
|
||||
"condition": d["description"],
|
||||
"high": to_temp(d["temp_max"]),
|
||||
"low": to_temp(d["temp_min"]),
|
||||
"precip_mm": round(d["precip_mm"], 1),
|
||||
"windspeed_max": round(d["windspeed_max"]),
|
||||
"precip_probability": d["precip_probability"],
|
||||
"windspeed_max": to_wind(d["windspeed_max"]),
|
||||
}
|
||||
for d in future_days
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user