Extract request to start connection from getting init token
This commit is contained in:
parent
d583b7190f
commit
553365c108
1 changed files with 12 additions and 4 deletions
|
|
@ -26,7 +26,7 @@ def dprint(txt) -> None:
|
||||||
print(f"{datetime.now()}: {txt}")
|
print(f"{datetime.now()}: {txt}")
|
||||||
|
|
||||||
|
|
||||||
def request_init_token(endpoint: str = "/nj-booking-ocp/init/start") -> str:
|
def request_start(endpoint: str = "/nj-booking-ocp/init/start") -> dict:
|
||||||
headers = {
|
headers = {
|
||||||
"Referer": "https://www.nightjet.com",
|
"Referer": "https://www.nightjet.com",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
|
@ -35,8 +35,11 @@ def request_init_token(endpoint: str = "/nj-booking-ocp/init/start") -> str:
|
||||||
resp_json = requests.post(
|
resp_json = requests.post(
|
||||||
f"{BASE_URL}{endpoint}", data=json.dumps(body), headers=headers
|
f"{BASE_URL}{endpoint}", data=json.dumps(body), headers=headers
|
||||||
).json()
|
).json()
|
||||||
token = resp_json["token"]
|
return resp_json
|
||||||
|
|
||||||
|
|
||||||
|
def get_init_token(endpoint: str = "/nj-booking-ocp/init/start") -> str:
|
||||||
|
token = request_start(endpoint)["token"]
|
||||||
dprint(f"Received init token: {token}")
|
dprint(f"Received init token: {token}")
|
||||||
return token
|
return token
|
||||||
|
|
||||||
|
|
@ -266,8 +269,13 @@ def notify_user(previous: Price, new: Price, channel: str) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def query(start_station: int, end_station: int, travel_date: datetime, traveller_birthdate: datetime) -> list[Price]:
|
def query(
|
||||||
token = request_init_token()
|
start_station: int,
|
||||||
|
end_station: int,
|
||||||
|
travel_date: datetime,
|
||||||
|
traveller_birthdate: datetime,
|
||||||
|
) -> list[Price]:
|
||||||
|
token = get_init_token()
|
||||||
connections = request_connections(token, start_station, end_station, travel_date)
|
connections = request_connections(token, start_station, end_station, travel_date)
|
||||||
booking_requests = connection_data_to_booking_requests(connections, traveller_birthdate=traveller_birthdate)
|
booking_requests = connection_data_to_booking_requests(connections, traveller_birthdate=traveller_birthdate)
|
||||||
bookings = [request_bookings(token, req) for req in booking_requests]
|
bookings = [request_bookings(token, req) for req in booking_requests]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue