Show past travel

This commit is contained in:
Edward Betts 2023-11-08 12:26:50 +00:00
parent 0321d9d283
commit 2fc200c7b2
2 changed files with 10 additions and 29 deletions

View file

@ -272,7 +272,7 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]:
my_data = config["data"]["personal-data"]
events += birthday.get_birthdays(last_year, os.path.join(my_data, "entities.yaml"))
events += accommodation.get_events(os.path.join(my_data, "accommodation.yaml"))
events += travel.all_events(today, config["data"]["personal-data"])
events += travel.all_events(config["data"]["personal-data"])
events += conference.get_list(os.path.join(my_data, "conferences.yaml"))
events += backwell_bins + bristol_bins

View file

@ -1,8 +1,6 @@
"""Travel."""
import os
import typing
from datetime import date
import yaml
@ -11,39 +9,27 @@ from .types import Event
Leg = dict[str, str]
def get(
from_date: date,
method: str,
filepath: str,
extra: typing.Callable[[Leg], str] | None = None,
) -> list[Event]:
def get_flights(data_dir: str) -> list[Event]:
"""Get travel events."""
def title(item: Leg) -> str:
ret = f'{method} from {item["from"]} to {item["to"]}'
if extra:
ret += f" ({extra(item)})"
return ret
filepath = os.path.join(data_dir, "flights.yaml")
return [
Event(
date=item["depart"],
end_date=item["arrive"],
end_date=item.get("arrive"),
name="transport",
title=title(item),
title=f'flight from {item["from"]} to {item["to"]} ({flight_number(item)})',
url=item.get("url"),
)
for item in yaml.safe_load(open(filepath))
if item["depart"].date() >= from_date
if item["depart"].date()
]
def get_trains(from_date: date, filepath: str) -> list[Event]:
def get_trains(data_dir: str) -> list[Event]:
"""Get train events."""
events: list[Event] = []
filepath = os.path.join(data_dir, "trains.yaml")
for item in yaml.safe_load(open(filepath)):
if item["depart"].date() < from_date:
continue
events += [
Event(
date=leg["depart"],
@ -66,11 +52,6 @@ def flight_number(flight: Leg) -> str:
return airline_code + flight["flight_number"]
def all_events(from_date: date, data_dir: str) -> list[Event]:
def all_events(data_dir: str) -> list[Event]:
"""Get all flights and rail journeys."""
trains = get_trains(from_date, os.path.join(data_dir, "trains.yaml"))
flights = get(
from_date, "flight", os.path.join(data_dir, "flights.yaml"), extra=flight_number
)
return trains + flights
return get_trains(data_dir) + get_flights(data_dir)