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,
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",
}

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."""
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(

View file

@ -30,7 +30,7 @@ a:link {
<body>
<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>
@ -52,7 +52,11 @@ a:link {
{% for dep, arr, days in data %}
<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 %}
<!--

View file

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

View file

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

View file

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