This commit is contained in:
Edward Betts 2023-08-10 14:05:07 +01:00
parent a5195cba1a
commit 7599f655ad
6 changed files with 72 additions and 26 deletions

View file

@ -69,6 +69,8 @@ def get_accommodations(
departure_date: str, departure_date: str,
ticket_tier: str, ticket_tier: str,
vehicle: Vehicle, vehicle: Vehicle,
adults: int,
small_dogs: int,
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Grab cabin details.""" """Grab cabin details."""
url = api_root_url + "crossing/accommodations" url = api_root_url + "crossing/accommodations"
@ -77,12 +79,12 @@ def get_accommodations(
"departurePort": departure_port, "departurePort": departure_port,
"arrivalPort": arrival_port, "arrivalPort": arrival_port,
"departureDate": departure_date, "departureDate": departure_date,
"passengers": {"adults": 2, "children": 0, "infants": 0}, "passengers": {"adults": adults, "children": 0, "infants": 0},
"disability": None, "disability": None,
"vehicle": vehicle_dict(vehicle), "vehicle": vehicle_dict(vehicle),
"petCabinsNeeded": True, "petCabinsNeeded": True,
"ticketTier": ticket_tier, "ticketTier": ticket_tier,
"pets": {"smallDogs": 1, "largeDogs": 0, "cats": 0}, "pets": {"smallDogs": small_dogs, "largeDogs": 0, "cats": 0},
"sponsor": None, "sponsor": None,
"offerType": "NONE", "offerType": "NONE",
} }

65
main.py
View file

@ -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.""" """Generate a URL for the cabins on a given crossing."""
dt = datetime.fromisoformat(crossing["departureDateTime"]["iso"]) dt = datetime.fromisoformat(crossing["departureDateTime"]["iso"])
utc_dt = dt.astimezone(pytz.utc) 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( return flask.url_for(
"cabins", "cabins",
@ -151,6 +153,8 @@ def cabins_url(dep: str, arr: str, crossing: dict[str, Any], ticket_tier: str) -
arrival_port=ferry.ports[arr], arrival_port=ferry.ports[arr],
departure_date=utc_dt.strftime("%Y-%m-%dT%H:%M:%S.000Z"), departure_date=utc_dt.strftime("%Y-%m-%dT%H:%M:%S.000Z"),
ticket_tier=ticket_tier, ticket_tier=ticket_tier,
adults=adults_str,
small_dogs=small_dogs_str,
) )
@ -188,9 +192,9 @@ def get_prices_with_cache(
start: str, start: str,
end: str, end: str,
selection: list[tuple[str, str]], selection: list[tuple[str, str]],
adults: int,
small_dogs: int,
refresh: bool = False, refresh: bool = False,
adults: int = 2,
small_dogs: int = 1,
) -> PriceData: ) -> PriceData:
"""Get price data using cache.""" """Get price data using cache."""
params = f"{direction}_{start}_{end}_{adults}_{small_dogs}" params = f"{direction}_{start}_{end}_{adults}_{small_dogs}"
@ -224,22 +228,38 @@ def get_prices_with_cache(
return all_data 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: def build_outbound(section: str) -> str:
"""Show all routes on one page.""" """Show all routes on one page."""
start = ferry_config.get(section, "from") start = ferry_config.get(section, "from")
end = ferry_config.get(section, "to") end = ferry_config.get(section, "to")
refresh = bool(flask.request.args.get("refresh")) refresh = bool(flask.request.args.get("refresh"))
adults_str = flask.request.args.get("adults") pax = read_pax()
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
direction = section[:-1] if section[-1].isdigit() else section direction = section[:-1] if section[-1].isdigit() else section
all_data = get_prices_with_cache( 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( return flask.render_template(
@ -262,17 +282,18 @@ def build_return(section: str) -> str:
start = ferry_config.get(section, "from") start = ferry_config.get(section, "from")
end = ferry_config.get(section, "to") end = ferry_config.get(section, "to")
refresh = bool(flask.request.args.get("refresh")) refresh = bool(flask.request.args.get("refresh"))
pax = read_pax()
direction = section[:-1] if section[-1].isdigit() else section 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( 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( return flask.render_template(
@ -331,7 +352,9 @@ def format_pet_options(o: dict[str, bool]) -> list[str]:
def get_accommodations_with_cache( def get_accommodations_with_cache(
dep: str, arr: str, d: str, ticket_tier: str, refresh: bool = False dep: str, arr: str, d: str, ticket_tier: str, refresh: bool = False
) -> dict[str, list[dict[str, Any]]]: ) -> 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_files = os.listdir(cache_location())
existing = [f for f in existing_files if f.endswith(params + ".json")] existing = [f for f in existing_files if f.endswith(params + ".json")]
if not refresh and existing: if not refresh and existing:
@ -348,7 +371,9 @@ def get_accommodations_with_cache(
vehicle = vehicle_from_config(ferry_config) vehicle = vehicle_from_config(ferry_config)
filename = cache_filename(params) 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: with open(filename, "w") as out:
print(filename) print(filename)
@ -425,7 +450,11 @@ def cabins(
section, start, end = get_outbound_section(departure_date[:10]) section, start, end = get_outbound_section(departure_date[:10])
time_delta = -60 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) crossing = lookup_sailing_id(prices, sailing_id)
cabin_data = get_accommodations_with_cache( cabin_data = get_accommodations_with_cache(

View file

@ -30,7 +30,7 @@ a:link {
<body> <body>
<div class="m-3"> <div class="m-3">
<p>{{ 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</p> <p>{{ 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</p>
{# {#
<p><a href="{{ url_for(other + "_page") }}">{{ other }}</a></p> <p><a href="{{ url_for(other + "_page") }}">{{ other }}</a></p>
@ -52,7 +52,11 @@ a:link {
{% for dep, arr, days in data %} {% for dep, arr, days in data %}
<h4>{{ dep.title() }} to {{ arr.title() }}</h4> <h4>{{ dep.title() }} to {{ arr.title() }}</h4>
{{ route_table(dep, arr, days) }} {% if days %}
{{ route_table(dep, arr, days) }}
{% else %}
<p>not available</p>
{% endif %}
{% endfor %} {% endfor %}
<!-- <!--

View file

@ -42,7 +42,8 @@ a:link {
<th>description</th> <th>description</th>
<th>births</th> <th>births</th>
<th>quantity<br/>available</th> <th>quantity<br/>available</th>
<th class="text-end">price</th> <th class="text-end">cabin<br/>price</th>
<th class="text-end">total<br/>price</th>
</tr> </tr>
{% for a in accommodations if a.quantityAvailable > 0 %} {% for a in accommodations if a.quantityAvailable > 0 %}
<tr> <tr>
@ -57,10 +58,12 @@ a:link {
{% endif %} {% endif %}
</td> </td>
<td class="text-end">£{{ a.unitCost.amount }}</td> <td class="text-end">£{{ a.unitCost.amount }}</td>
<td class="text-end">£{{ a.unitCost.amount + crossing.flexiPrice.amount }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
{% if pet_accommodations %}
<h4>Pet accommodations</h4> <h4>Pet accommodations</h4>
{% set pets = {"G": "stay in car", "B": "large kennel", "K": "small kennel" } %} {% set pets = {"G": "stay in car", "B": "large kennel", "K": "small kennel" } %}
@ -71,6 +74,8 @@ a:link {
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %}
<h4>Sailing</h4> <h4>Sailing</h4>
<ul> <ul>

View file

@ -11,10 +11,12 @@
<div class="m-3"> <div class="m-3">
<ul> <ul>
<li><a href="{{ url_for("outbound1_page") }}">Outbound: 5 May</a> <li><a href="{{ url_for("outbound1_page") }}">Outbound: 29 September</a>
<li><a href="{{ url_for("return1_page") }}">Return: 12 May</a> <li><a href="{{ url_for("return1_page") }}">Return: 6 October</a>
{#
<li><a href="{{ url_for("outbound3_page") }}">Outbound: 29 September</a> <li><a href="{{ url_for("outbound3_page") }}">Outbound: 29 September</a>
<li><a href="{{ url_for("return2_page") }}">Return: 6 October</a> <li><a href="{{ url_for("return2_page") }}">Return: </a>
#}
</ul> </ul>
</div> </div>

View file

@ -6,6 +6,7 @@
<th>arrive</th> <th>arrive</th>
<th>duration</th> <th>duration</th>
<th>ship</th> <th>ship</th>
<th>type</th>
<th>economy</th> <th>economy</th>
<th>standard</th> <th>standard</th>
<th>flexi</th> <th>flexi</th>
@ -38,6 +39,9 @@
<td class="text-nowrap"> <td class="text-nowrap">
{{ crossing.shipName }} {{ crossing.shipName }}
</td> </td>
<td class="text-nowrap">
{{ crossing.shipType }}
</td>
<td class="text-nowrap"> <td class="text-nowrap">
{% if crossing.economyPrice %} {% if crossing.economyPrice %}
<a href="{{ cabins_url(dep, arr, crossing, "ECONOMY") }}"> <a href="{{ cabins_url(dep, arr, crossing, "ECONOMY") }}">