diff --git a/agenda/fx.py b/agenda/fx.py index 90cbc5f..f0ec161 100644 --- a/agenda/fx.py +++ b/agenda/fx.py @@ -44,17 +44,6 @@ async def get_gbpusd(config: flask.config.Config) -> Decimal: return typing.cast(Decimal, 1 / data["quotes"]["USDGBP"]) -def read_cached_rates(filename: str, currencies: list[str]) -> dict[str, Decimal]: - """Read FX rates from cache.""" - with open(filename) as file: - data = json.load(file, parse_float=Decimal) - return { - cur: Decimal(data["quotes"][f"GBP{cur}"]) - for cur in currencies - if f"GBP{cur}" in data["quotes"] - } - - def get_rates(config: flask.config.Config) -> dict[str, Decimal]: """Get current values of exchange rates for a list of currencies against GBP.""" currencies = config["CURRENCIES"] @@ -76,19 +65,22 @@ def get_rates(config: flask.config.Config) -> dict[str, Decimal]: recent = datetime.strptime(recent_filename[:16], "%Y-%m-%d_%H:%M") delta = now - recent - full_path = os.path.join(fx_dir, recent_filename) if delta < timedelta(hours=12): - return read_cached_rates(full_path, currencies) + full_path = os.path.join(fx_dir, recent_filename) + with open(full_path) as file: + data = json.load(file, parse_float=Decimal) + return { + cur: Decimal(data["quotes"][f"GBP{cur}"]) + for cur in currencies + if f"GBP{cur}" in data["quotes"] + } url = "http://api.exchangerate.host/live" params = {"currencies": currency_string, "source": "GBP", "access_key": access_key} filename = f"{now_str}_{file_suffix}" - try: - with httpx.Client() as client: - response = client.get(url, params=params) - except httpx.ConnectError: - return read_cached_rates(full_path, currencies) + with httpx.Client() as client: + response = client.get(url, params=params) with open(os.path.join(fx_dir, filename), "w") as file: file.write(response.text) diff --git a/agenda/trip.py b/agenda/trip.py index e8f542f..f2c4a47 100644 --- a/agenda/trip.py +++ b/agenda/trip.py @@ -19,9 +19,9 @@ class UnknownStation(Exception): pass -def load_travel(travel_type: str, plural: str, data_dir: str) -> list[StrDict]: +def load_travel(travel_type: str, data_dir: str) -> list[StrDict]: """Read flight and train journeys.""" - items = travel.parse_yaml(plural, data_dir) + items = travel.parse_yaml(travel_type + "s", data_dir) for item in items: item["type"] = travel_type return items @@ -31,7 +31,7 @@ def load_trains( data_dir: str, route_distances: travel.RouteDistances | None = None ) -> list[StrDict]: """Load trains.""" - trains = load_travel("train", "trains", data_dir) + trains = load_travel("train", data_dir) stations = travel.parse_yaml("stations", data_dir) by_name = {station["name"]: station for station in stations} @@ -58,25 +58,6 @@ def load_trains( return trains -def load_ferries(data_dir: str) -> list[StrDict]: - """Load ferries.""" - ferries = load_travel("ferry", "ferries", data_dir) - terminals = travel.parse_yaml("ferry_terminals", data_dir) - by_name = {terminal["name"]: terminal for terminal in terminals} - - for item in ferries: - assert item["from"] in by_name and item["to"] in by_name - from_terminal, to_terminal = by_name[item["from"]], by_name[item["to"]] - item["from_terminal"] = from_terminal - item["to_terminal"] = to_terminal - - geojson = from_terminal["routes"].get(item["to"]) - if geojson: - item["geojson_filename"] = geojson - - return ferries - - def depart_datetime(item: StrDict) -> datetime: depart = item["depart"] if isinstance(depart, datetime): @@ -86,7 +67,7 @@ def depart_datetime(item: StrDict) -> datetime: def load_flight_bookings(data_dir: str) -> list[StrDict]: """Load flight bookings.""" - bookings = load_travel("flight", "flights", 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 booking in bookings: @@ -129,9 +110,7 @@ def build_trip_list( yaml_trip_lookup = {item["trip"]: item for item in yaml_trip_list} travel_items = sorted( - load_flights(data_dir) - + load_trains(data_dir, route_distances=route_distances) - + load_ferries(data_dir), + load_flights(data_dir) + load_trains(data_dir, route_distances=route_distances), key=depart_datetime, ) @@ -190,22 +169,17 @@ def collect_trip_coordinates(trip: Trip) -> list[StrDict]: stations = {} station_list = [] airports = {} - ferry_terminals = {} for t in trip.travel: if t["type"] == "train": station_list += [t["from_station"], t["to_station"]] for leg in t["legs"]: station_list.append(leg["from_station"]) station_list.append(leg["to_station"]) - elif t["type"] == "flight": + else: + assert t["type"] == "flight" for field in "from_airport", "to_airport": if field in t: airports[t[field]["iata"]] = t[field] - else: - assert t["type"] == "ferry" - for field in "from_terminal", "to_terminal": - terminal = t[field] - ferry_terminals[terminal["name"]] = terminal for s in station_list: if s["uic"] in stations: @@ -231,12 +205,7 @@ def collect_trip_coordinates(trip: Trip) -> list[StrDict]: if "latitude" in item and "longitude" in item ] - locations = [ - ("station", stations), - ("airport", airports), - ("ferry_terminal", ferry_terminals), - ] - for coord_type, coord_dict in locations: + for coord_type, coord_dict in ("station", stations), ("airport", airports): coords += [ { "name": s["name"], @@ -257,7 +226,7 @@ def latlon_tuple(stop: StrDict) -> tuple[float, float]: def read_geojson(data_dir: str, filename: str) -> str: """Read GeoJSON from file.""" - return open(os.path.join(data_dir, filename + ".geojson")).read() + return open(os.path.join(data_dir, "train_routes", filename + ".geojson")).read() def get_trip_routes(trip: Trip) -> list[StrDict]: @@ -265,20 +234,6 @@ def get_trip_routes(trip: Trip) -> list[StrDict]: routes = [] seen_geojson = set() for t in trip.travel: - if t["type"] == "ferry": - ferry_from, ferry_to = t["from_terminal"], t["to_terminal"] - - key = "_".join(["ferry"] + sorted([ferry_from["name"], ferry_to["name"]])) - filename = os.path.join("ferry_routes", t["geojson_filename"]) - - routes.append( - { - "type": "train", - "key": key, - "geojson_filename": filename, - } - ) - continue if t["type"] == "flight": if "from_airport" not in t or "to_airport" not in t: continue @@ -317,7 +272,7 @@ def get_trip_routes(trip: Trip) -> list[StrDict]: { "type": "train", "key": key, - "geojson_filename": os.path.join("train_routes", geojson_filename), + "geojson_filename": geojson_filename, } ) diff --git a/static/js/map.js b/static/js/map.js index cd29dc7..21872c9 100644 --- a/static/js/map.js +++ b/static/js/map.js @@ -16,7 +16,6 @@ function emoji_icon(emoji) { var icons = { "station": emoji_icon("🚉"), "airport": emoji_icon("✈️"), - "ferry_terminal": emoji_icon("✈️"), "accommodation": emoji_icon("🏨"), "conference": emoji_icon("🎤"), "event": emoji_icon("🍷"), diff --git a/templates/macros.html b/templates/macros.html index fec5da9..6db2d87 100644 --- a/templates/macros.html +++ b/templates/macros.html @@ -226,36 +226,3 @@ {% endif %} {% endmacro %} - -{% macro ferry_row(item) %} -