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

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(