Update to read flights grouped by booking

Closes: #151
This commit is contained in:
Edward Betts 2024-04-20 10:23:56 +01:00
parent 5ab9d93484
commit 19732a3ef1
2 changed files with 31 additions and 20 deletions

View file

@ -58,17 +58,21 @@ def parse_yaml(travel_type: str, data_dir: str) -> TravelList:
def get_flights(data_dir: str) -> list[Event]: def get_flights(data_dir: str) -> list[Event]:
"""Get travel events.""" """Get travel events."""
return [ bookings = parse_yaml("flights", data_dir)
Event( events = []
date=item["depart"], for booking in bookings:
end_date=item.get("arrive"), for item in booking["flights"]:
name="transport", if not item["depart"].date():
title=f'✈️ {item["from"]} to {item["to"]} ({flight_number(item)})', continue
url=(item.get("url") if flask.g.user.is_authenticated else None), e = Event(
) date=item["depart"],
for item in parse_yaml("flights", data_dir) end_date=item.get("arrive"),
if item["depart"].date() name="transport",
] title=f'✈️ {item["from"]} to {item["to"]} ({flight_number(item)})',
url=(item.get("url") if flask.g.user.is_authenticated else None),
)
events.append(e)
return events
def get_trains(data_dir: str) -> list[Event]: def get_trains(data_dir: str) -> list[Event]:

View file

@ -67,18 +67,25 @@ def depart_datetime(item: StrDict) -> datetime:
def load_flights(data_dir: str) -> list[StrDict]: def load_flights(data_dir: str) -> list[StrDict]:
"""Load flights.""" """Load flights."""
flights = load_travel("flight", data_dir) bookings = load_travel("flight", data_dir)
airlines = yaml.safe_load(open(os.path.join(data_dir, "airlines.yaml"))) airlines = yaml.safe_load(open(os.path.join(data_dir, "airlines.yaml")))
airports = travel.parse_yaml("airports", data_dir) airports = travel.parse_yaml("airports", data_dir)
for flight in flights: flights = []
if flight["from"] in airports: for booking in bookings:
flight["from_airport"] = airports[flight["from"]] for flight in booking["flights"]:
if flight["to"] in airports: if flight["from"] in airports:
flight["to_airport"] = airports[flight["to"]] flight["from_airport"] = airports[flight["from"]]
if "airline" in flight: if flight["to"] in airports:
flight["airline_name"] = airlines.get(flight["airline"], "[unknown]") flight["to_airport"] = airports[flight["to"]]
if "airline" in flight:
flight["airline_name"] = airlines.get(flight["airline"], "[unknown]")
flight["distance"] = travel.flight_distance(flight) flight["distance"] = travel.flight_distance(flight)
flight["type"] = booking["type"]
flight["trip"] = booking["trip"]
if "booking_reference" in booking:
flight["booking_reference"] = booking["booking_reference"]
flights.append(flight)
return flights return flights