Add booking requests

This commit is contained in:
Marty Oehme 2025-08-11 21:45:50 +02:00
parent c1b34eb2c1
commit 3cf51b35de
Signed by: Marty
GPG key ID: 4E535BC19C61886E
3 changed files with 75 additions and 2 deletions

View file

@ -199,6 +199,31 @@ curl 'https://www.nightjet.com/nj-booking-ocp/offer/get' \
--data-raw '{"njFrom":8011160,"njDep":1760461680000,"njTo":8700011,"maxChanges":0,"connections":1,"filter":{"njTrain":"NJ 40424","njDeparture":1760461680000},"objects":[{"type":"person","birthDate":"1995-08-11","cards":[]}],"relations":[],"lang":"en"}'
```
request data
```json
{
"njFrom": 8011160,
"njDep": 1760461680000,
"njTo": 8700011,
"maxChanges": 0,
"connections": 1,
"filter": {
"njTrain": "NJ 40424",
"njDeparture": 1760461680000
},
"objects": [
{
"type": "person",
"birthDate": "1995-08-11",
"cards": []
}
],
"relations": [],
"lang": "en"
}
```
### booking response
```json

2
br.json Normal file

File diff suppressed because one or more lines are too long

50
main.py
View file

@ -1,4 +1,5 @@
import json
from pprint import pprint
from typing import Any
import requests
@ -57,7 +58,50 @@ def request_connections(
return resp_json["connections"]
def request_bookings(endpoint: str = "") -> dict[Any, Any]: ...
TRAVELLER_BIRTHDATE = "2000-07-15" # TODO: randomize a little
def connection_data_to_booking_requests(connections):
b_requests = []
for c in connections:
train = c["trains"][0]
dep = train["departure"]["utc"]
req = {
"njFrom": c["from"]["number"], # from station,
"njTo": c["to"]["number"], # to station
"njDep": dep, # departure time,
"maxChanges": 0,
"connections": 1,
"filter": {
"njTrain": train["train"], # train number
"njDeparture": dep, # departure time again
},
"objects": [ # traveller
{"type": "person", "birthDate": TRAVELLER_BIRTHDATE, "cards": []}
],
"relations": [],
"lang": "en",
}
b_requests.append(req)
return b_requests
def request_bookings(
token: str, booking_req: dict[str, Any], endpoint: str = "/nj-booking-ocp/offer/get"
) -> dict[Any, Any]:
headers = {
"Accept": "application/json",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/json",
"Referer": "https://www.nightjet.com/en/ticket-buchen/",
"Origin": "https://www.nightjet.com",
"x-token": token,
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:139.0) Gecko/20100101 Firefox/139.0",
}
resp_json = requests.post(
f"{BASE_URL}{endpoint}", headers=headers, data=json.dumps(booking_req)
).json()
return resp_json
def get_prices(bookings_dict: dict[Any, Any]) -> dict[Any, Any]: ...
@ -65,7 +109,9 @@ def get_prices(bookings_dict: dict[Any, Any]) -> dict[Any, Any]: ...
def main():
token = request_init_token()
print(request_connections(token))
connections = request_connections(token)
booking_requests = connection_data_to_booking_requests(connections)
dprint(request_bookings(token, booking_requests[0]))
if __name__ == "__main__":