Improve display of cabins list
This commit is contained in:
parent
70db886a81
commit
944fe24662
5 changed files with 150 additions and 22 deletions
23
ferry/__init__.py
Normal file
23
ferry/__init__.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Vehicle:
|
||||
"""What type of vehicle is going on the ferry."""
|
||||
|
||||
type: str
|
||||
registration: str
|
||||
height: int
|
||||
length: int
|
||||
|
||||
|
||||
ports = {
|
||||
"PORTSMOUTH": "GBPME",
|
||||
"PLYMOUTH": "GBPLY",
|
||||
"POOLE": "GBPOO",
|
||||
"CAEN": "FROUI",
|
||||
"CHERBOURG": "FRCER",
|
||||
"ST MALO": "FRSML",
|
||||
}
|
||||
|
||||
port_lookup = {code: name for name, code in ports.items()}
|
||||
86
ferry/api.py
Normal file
86
ferry/api.py
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
"""Interface with the Brittany Ferries API."""
|
||||
|
||||
from typing import Any, TypedDict
|
||||
|
||||
import requests
|
||||
|
||||
from . import Vehicle
|
||||
|
||||
api_root_url = "https://www.brittany-ferries.co.uk/api/ferry/v1/"
|
||||
|
||||
|
||||
class VehicleDict(TypedDict):
|
||||
"""Description of vechicle in the format expected by the API."""
|
||||
|
||||
type: str
|
||||
registrations: list[str]
|
||||
height: int
|
||||
length: int
|
||||
extras: dict[str, None]
|
||||
|
||||
|
||||
def vehicle_dict(v: Vehicle) -> VehicleDict:
|
||||
"""Return vehicle detail in the format for the Brittany Ferries API."""
|
||||
return {
|
||||
"type": v.type,
|
||||
"registrations": [v.registration],
|
||||
"height": v.height,
|
||||
"length": v.length,
|
||||
"extras": {"rearMountedBikeCarrier": None},
|
||||
}
|
||||
|
||||
|
||||
def get_prices(
|
||||
departure_port: str,
|
||||
arrival_port: str,
|
||||
from_date: str,
|
||||
to_date: str,
|
||||
vehicle: Vehicle,
|
||||
) -> dict[str, Any]:
|
||||
"""Call Brittany Ferries API to get details of crossings."""
|
||||
url = api_root_url + "crossing/prices"
|
||||
|
||||
post_data = {
|
||||
"bookingReference": None,
|
||||
"pets": {"smallDogs": 1, "largeDogs": 0, "cats": 0},
|
||||
"passengers": {"adults": 2, "children": 0, "infants": 0},
|
||||
"vehicle": vehicle_dict(vehicle),
|
||||
"departurePort": departure_port,
|
||||
"arrivalPort": arrival_port,
|
||||
"disability": None,
|
||||
"sponsor": None,
|
||||
"fromDate": f"{from_date}T00:00:00",
|
||||
"toDate": f"{to_date}T23:59:59",
|
||||
}
|
||||
|
||||
r = requests.post(url, json=post_data)
|
||||
data: dict[str, Any] = r.json()
|
||||
return data
|
||||
|
||||
|
||||
def get_accommodations(
|
||||
departure_port: str,
|
||||
arrival_port: str,
|
||||
departure_date: str,
|
||||
ticket_tier: str,
|
||||
vehicle: Vehicle,
|
||||
) -> dict[str, Any]:
|
||||
"""Grab cabin details."""
|
||||
url = api_root_url + "crossing/accommodations"
|
||||
post_data = {
|
||||
"bookingReference": None,
|
||||
"departurePort": departure_port,
|
||||
"arrivalPort": arrival_port,
|
||||
"departureDate": departure_date,
|
||||
"passengers": {"adults": 2, "children": 0, "infants": 0},
|
||||
"disability": None,
|
||||
"vehicle": vehicle_dict(vehicle),
|
||||
"petCabinsNeeded": True,
|
||||
"ticketTier": ticket_tier,
|
||||
"pets": {"smallDogs": 1, "largeDogs": 0, "cats": 0},
|
||||
"sponsor": None,
|
||||
"offerType": "NONE",
|
||||
}
|
||||
|
||||
json_data: dict[str, Any] = requests.post(url, json=post_data).json()
|
||||
return json_data
|
||||
17
ferry/read_config.py
Normal file
17
ferry/read_config.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import configparser
|
||||
import os.path
|
||||
|
||||
from . import Vehicle
|
||||
|
||||
ferry_config = configparser.ConfigParser()
|
||||
ferry_config.read(os.path.expanduser("~edward/config/brittany-ferries/config"))
|
||||
|
||||
|
||||
def vehicle_from_config(config: configparser.ConfigParser) -> Vehicle:
|
||||
"""Generate a vehicle object from config."""
|
||||
return Vehicle(
|
||||
type=config.get("vehicle", "type"),
|
||||
registration=config.get("vehicle", "registration"),
|
||||
height=config.getint("vehicle", "height"),
|
||||
length=config.getint("vehicle", "length"),
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue