Add OpenWeatherMap weather forecasts. Closes #48

Show 8-day Bristol home weather on the index and weekends pages.
Show destination weather per day on the trip list and trip page.
Cache forecasts in ~/lib/data/weather/ and refresh via update.py.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Edward Betts 2026-02-21 20:27:25 +00:00
parent 61e17d9c96
commit b4f0a5bf5d
7 changed files with 217 additions and 0 deletions

View file

@ -31,6 +31,7 @@ import agenda.thespacedevs
import agenda.trip
import agenda.trip_schengen
import agenda.utils
import agenda.weather
from agenda import ical, calendar, format_list_with_ampersand, travel, uk_tz
from agenda.event import Event
from agenda.types import StrDict, Trip
@ -120,6 +121,7 @@ async def index() -> str:
start_event_list=date.today() - timedelta(days=1),
end_event_list=date.today() + timedelta(days=365 * 2),
render_time=(time.time() - t0),
home_weather=get_home_weather(),
**data,
)
@ -296,6 +298,15 @@ async def weekends() -> str:
weekends = agenda.busy.weekends(
start, busy_events, trip_list, app.config["PERSONAL_DATA"]
)
home_weather = get_home_weather()
home_weather_by_date = {f["date"]: f for f in home_weather}
for weekend in weekends:
saturday = weekend["date"]
sunday = saturday + timedelta(days=1)
weekend["saturday_weather"] = home_weather_by_date.get(saturday.isoformat())
weekend["sunday_weather"] = home_weather_by_date.get(sunday.isoformat())
return flask.render_template(
"weekends.html",
items=weekends,
@ -583,6 +594,21 @@ def sum_distances_by_transport_type(trips: list[Trip]) -> list[tuple[str, float]
return list(distances_by_transport_type.items())
def get_home_weather() -> list[StrDict]:
"""Get Bristol home weather forecast, with date objects added for templates."""
from datetime import date as date_type
forecasts = agenda.weather.get_forecast(
app.config["DATA_DIR"],
app.config.get("OPENWEATHERMAP_API_KEY", ""),
app.config["HOME_LATITUDE"],
app.config["HOME_LONGITUDE"],
)
for f in forecasts:
f["date_obj"] = date_type.fromisoformat(f["date"])
return forecasts
def get_trip_list() -> list[Trip]:
"""Get trip list with route distances."""
route_distances = agenda.travel.load_route_distances(app.config["DATA_DIR"])
@ -674,11 +700,21 @@ def trip_future_list() -> str:
shown = current + future
trip_weather_map = {
trip.start.isoformat(): agenda.weather.get_trip_weather(
app.config["DATA_DIR"],
app.config.get("OPENWEATHERMAP_API_KEY", ""),
trip,
)
for trip in shown
}
return flask.render_template(
"trip/list.html",
heading="Future trips",
trips=shown,
trip_school_holiday_map=trip_school_holiday_map(shown),
trip_weather_map=trip_weather_map,
coordinates=coordinates,
routes=routes,
today=today,
@ -747,6 +783,12 @@ def trip_page(start: str) -> str:
app.config["PERSONAL_DATA"], route.pop("geojson_filename")
)
trip_weather = agenda.weather.get_trip_weather(
app.config["DATA_DIR"],
app.config.get("OPENWEATHERMAP_API_KEY", ""),
trip,
)
return flask.render_template(
"trip_page.html",
trip=trip,
@ -760,6 +802,7 @@ def trip_page(start: str) -> str:
holidays=agenda.holidays.get_trip_holidays(trip),
school_holidays=agenda.holidays.get_trip_school_holidays(trip),
human_readable_delta=agenda.utils.human_readable_delta,
trip_weather=trip_weather,
)