diff --git a/ferry/api.py b/ferry/api.py index 7eaf2ca..dc57c1d 100644 --- a/ferry/api.py +++ b/ferry/api.py @@ -69,6 +69,8 @@ def get_accommodations( departure_date: str, ticket_tier: str, vehicle: Vehicle, + adults: int, + small_dogs: int, ) -> dict[str, Any]: """Grab cabin details.""" url = api_root_url + "crossing/accommodations" @@ -77,12 +79,12 @@ def get_accommodations( "departurePort": departure_port, "arrivalPort": arrival_port, "departureDate": departure_date, - "passengers": {"adults": 2, "children": 0, "infants": 0}, + "passengers": {"adults": adults, "children": 0, "infants": 0}, "disability": None, "vehicle": vehicle_dict(vehicle), "petCabinsNeeded": True, "ticketTier": ticket_tier, - "pets": {"smallDogs": 1, "largeDogs": 0, "cats": 0}, + "pets": {"smallDogs": small_dogs, "largeDogs": 0, "cats": 0}, "sponsor": None, "offerType": "NONE", } diff --git a/main.py b/main.py index f602180..ba46759 100755 --- a/main.py +++ b/main.py @@ -143,6 +143,8 @@ def cabins_url(dep: str, arr: str, crossing: dict[str, Any], ticket_tier: str) - """Generate a URL for the cabins on a given crossing.""" dt = datetime.fromisoformat(crossing["departureDateTime"]["iso"]) utc_dt = dt.astimezone(pytz.utc) + adults_str = flask.request.args.get("adults") + small_dogs_str = flask.request.args.get("small_dogs") return flask.url_for( "cabins", @@ -151,6 +153,8 @@ def cabins_url(dep: str, arr: str, crossing: dict[str, Any], ticket_tier: str) - arrival_port=ferry.ports[arr], departure_date=utc_dt.strftime("%Y-%m-%dT%H:%M:%S.000Z"), ticket_tier=ticket_tier, + adults=adults_str, + small_dogs=small_dogs_str, ) @@ -188,9 +192,9 @@ def get_prices_with_cache( start: str, end: str, selection: list[tuple[str, str]], + adults: int, + small_dogs: int, refresh: bool = False, - adults: int = 2, - small_dogs: int = 1, ) -> PriceData: """Get price data using cache.""" params = f"{direction}_{start}_{end}_{adults}_{small_dogs}" @@ -224,22 +228,38 @@ def get_prices_with_cache( return all_data +def read_pax() -> dict[str, int]: + """Get the number of adults and dogs that are travelling.""" + config_adults = int(ferry_config.get("pax", "adults")) + config_dogs = int(ferry_config.get("pax", "dogs")) + + adults_str = flask.request.args.get("adults") + adults = int(adults_str) if adults_str else config_adults + + small_dogs_str = flask.request.args.get("small_dogs") + small_dogs = int(small_dogs_str) if small_dogs_str else config_dogs + + return {"adults": adults, "small_dogs": small_dogs} + + def build_outbound(section: str) -> str: """Show all routes on one page.""" start = ferry_config.get(section, "from") end = ferry_config.get(section, "to") refresh = bool(flask.request.args.get("refresh")) - adults_str = flask.request.args.get("adults") - adults = int(adults_str) if adults_str else 2 - - small_dogs_str = flask.request.args.get("small_dogs") - small_dogs = int(small_dogs_str) if small_dogs_str else 2 + pax = read_pax() direction = section[:-1] if section[-1].isdigit() else section all_data = get_prices_with_cache( - direction, start, end, routes["outbound"], refresh, adults, small_dogs + direction, + start, + end, + routes["outbound"], + pax["adults"], + pax["small_dogs"], + refresh, ) return flask.render_template( @@ -262,17 +282,18 @@ def build_return(section: str) -> str: start = ferry_config.get(section, "from") end = ferry_config.get(section, "to") refresh = bool(flask.request.args.get("refresh")) + pax = read_pax() direction = section[:-1] if section[-1].isdigit() else section - adults_str = flask.request.args.get("adults") - adults = int(adults_str) if adults_str else 2 - - small_dogs_str = flask.request.args.get("small_dogs") - small_dogs = int(small_dogs_str) if small_dogs_str else 2 - all_data = get_prices_with_cache( - direction, start, end, routes["return"], refresh, adults, small_dogs + direction, + start, + end, + routes["return"], + pax["adults"], + pax["small_dogs"], + refresh, ) return flask.render_template( @@ -331,7 +352,9 @@ def format_pet_options(o: dict[str, bool]) -> list[str]: def get_accommodations_with_cache( dep: str, arr: str, d: str, ticket_tier: str, refresh: bool = False ) -> dict[str, list[dict[str, Any]]]: - params = f"{dep}_{arr}_{d}_{ticket_tier}" + pax = read_pax() + + params = f"{dep}_{arr}_{d}_{ticket_tier}_{pax['adults']}_{pax['small_dogs']}" existing_files = os.listdir(cache_location()) existing = [f for f in existing_files if f.endswith(params + ".json")] if not refresh and existing: @@ -348,7 +371,9 @@ def get_accommodations_with_cache( vehicle = vehicle_from_config(ferry_config) filename = cache_filename(params) - data = get_accommodations(dep, arr, d, ticket_tier, vehicle) + data = get_accommodations( + dep, arr, d, ticket_tier, vehicle, pax["adults"], pax["small_dogs"] + ) with open(filename, "w") as out: print(filename) @@ -425,7 +450,11 @@ def cabins( section, start, end = get_outbound_section(departure_date[:10]) time_delta = -60 - prices = get_prices_with_cache(direction, start, end, routes[direction]) + pax = read_pax() + + prices = get_prices_with_cache( + direction, start, end, routes[direction], pax["adults"], pax["small_dogs"] + ) crossing = lookup_sailing_id(prices, sailing_id) cabin_data = get_accommodations_with_cache( diff --git a/templates/all_routes.html b/templates/all_routes.html index 48d5821..bdee066 100644 --- a/templates/all_routes.html +++ b/templates/all_routes.html @@ -30,7 +30,7 @@ a:link {
-

{{ days_until_start }} days / {{ (days_until_start / 7) | int }} weeks / {{ "{:.1f}".format(days_until_start / 30.5) }} months until start of Dodainville week: Friday 5 May 2022

+

{{ days_until_start }} days / {{ (days_until_start / 7) | int }} weeks / {{ "{:.1f}".format(days_until_start / 30.5) }} months until start of Dodainville week: Friday 29 Sept 2023

{#

{{ other }}

@@ -52,7 +52,11 @@ a:link { {% for dep, arr, days in data %}

{{ dep.title() }} to {{ arr.title() }}

- {{ route_table(dep, arr, days) }} + {% if days %} + {{ route_table(dep, arr, days) }} + {% else %} +

not available

+ {% endif %} {% endfor %}