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]:
"""Get travel events."""
return [
Event(
date=item["depart"],
end_date=item.get("arrive"),
name="transport",
title=f'✈️ {item["from"]} to {item["to"]} ({flight_number(item)})',
url=(item.get("url") if flask.g.user.is_authenticated else None),
)
for item in parse_yaml("flights", data_dir)
if item["depart"].date()
]
bookings = parse_yaml("flights", data_dir)
events = []
for booking in bookings:
for item in booking["flights"]:
if not item["depart"].date():
continue
e = Event(
date=item["depart"],
end_date=item.get("arrive"),
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]:

View file

@ -67,18 +67,25 @@ def depart_datetime(item: StrDict) -> datetime:
def load_flights(data_dir: str) -> list[StrDict]:
"""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")))
airports = travel.parse_yaml("airports", data_dir)
for flight in flights:
if flight["from"] in airports:
flight["from_airport"] = airports[flight["from"]]
if flight["to"] in airports:
flight["to_airport"] = airports[flight["to"]]
if "airline" in flight:
flight["airline_name"] = airlines.get(flight["airline"], "[unknown]")
flights = []
for booking in bookings:
for flight in booking["flights"]:
if flight["from"] in airports:
flight["from_airport"] = airports[flight["from"]]
if flight["to"] in airports:
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