Start extracting prices
This commit is contained in:
parent
3cf51b35de
commit
65aab7b180
2 changed files with 51 additions and 15 deletions
1
bookings.json
Normal file
1
bookings.json
Normal file
File diff suppressed because one or more lines are too long
65
main.py
65
main.py
|
|
@ -61,7 +61,7 @@ def request_connections(
|
||||||
TRAVELLER_BIRTHDATE = "2000-07-15" # TODO: randomize a little
|
TRAVELLER_BIRTHDATE = "2000-07-15" # TODO: randomize a little
|
||||||
|
|
||||||
|
|
||||||
def connection_data_to_booking_requests(connections):
|
def connection_data_to_booking_requests(connections) -> list[dict[str, Any]]:
|
||||||
b_requests = []
|
b_requests = []
|
||||||
for c in connections:
|
for c in connections:
|
||||||
train = c["trains"][0]
|
train = c["trains"][0]
|
||||||
|
|
@ -83,35 +83,70 @@ def connection_data_to_booking_requests(connections):
|
||||||
"lang": "en",
|
"lang": "en",
|
||||||
}
|
}
|
||||||
b_requests.append(req)
|
b_requests.append(req)
|
||||||
|
dprint(
|
||||||
|
f"Crafted booking request {c['from']['name']} -> {c['to']['name']}: {train['departure']['local']}-{train['arrival']['local']}."
|
||||||
|
)
|
||||||
return b_requests
|
return b_requests
|
||||||
|
|
||||||
|
|
||||||
def request_bookings(
|
def request_bookings(
|
||||||
token: str, booking_req: dict[str, Any], endpoint: str = "/nj-booking-ocp/offer/get"
|
token: str, booking_req: dict[str, Any], endpoint: str = "/nj-booking-ocp/offer/get"
|
||||||
) -> dict[Any, Any]:
|
) -> dict[Any, Any]:
|
||||||
headers = {
|
with open("bookings.json") as f:
|
||||||
"Accept": "application/json",
|
DEBUG_BOOKINGS = json.load(f)
|
||||||
"Accept-Language": "en-US,en;q=0.5",
|
resp_json = DEBUG_BOOKINGS
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Referer": "https://www.nightjet.com/en/ticket-buchen/",
|
# headers = {
|
||||||
"Origin": "https://www.nightjet.com",
|
# "Accept": "application/json",
|
||||||
"x-token": token,
|
# "Accept-Language": "en-US,en;q=0.5",
|
||||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:139.0) Gecko/20100101 Firefox/139.0",
|
# "Content-Type": "application/json",
|
||||||
}
|
# "Referer": "https://www.nightjet.com/en/ticket-buchen/",
|
||||||
resp_json = requests.post(
|
# "Origin": "https://www.nightjet.com",
|
||||||
f"{BASE_URL}{endpoint}", headers=headers, data=json.dumps(booking_req)
|
# "x-token": token,
|
||||||
).json()
|
# "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()
|
||||||
|
# dprint(f"Requested prices ({booking_req["njFrom"]} -> {booking_req["njTo"]} at {booking_req["njDep"]}).")
|
||||||
return resp_json
|
return resp_json
|
||||||
|
|
||||||
|
|
||||||
def get_prices(bookings_dict: dict[Any, Any]) -> dict[Any, Any]: ...
|
def json_extract(obj, key):
|
||||||
|
"""Recursively fetch values from nested JSON."""
|
||||||
|
arr = []
|
||||||
|
|
||||||
|
def extract(obj, arr, key):
|
||||||
|
"""Recursively search for values of key in JSON tree."""
|
||||||
|
if isinstance(obj, dict):
|
||||||
|
for k, v in obj.items():
|
||||||
|
if isinstance(v, (dict, list)):
|
||||||
|
extract(v, arr, key)
|
||||||
|
elif k == key:
|
||||||
|
arr.append(v)
|
||||||
|
elif isinstance(obj, list):
|
||||||
|
for item in obj:
|
||||||
|
extract(item, arr, key)
|
||||||
|
return arr
|
||||||
|
|
||||||
|
values = extract(obj, arr, key)
|
||||||
|
return values
|
||||||
|
|
||||||
|
|
||||||
|
def extract_prices(bookings_dict: dict[Any, Any]) -> dict[Any, Any]:
|
||||||
|
# .result[].connections[].offers[].reservation.reservationSegments[].compartments[].objects
|
||||||
|
for r in bookings_dict[0][0][0]["result"]:
|
||||||
|
for c in r["connections"]:
|
||||||
|
for o in c["offers"]:
|
||||||
|
print(o["name"])
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
token = request_init_token()
|
token = request_init_token()
|
||||||
connections = request_connections(token)
|
connections = request_connections(token)
|
||||||
booking_requests = connection_data_to_booking_requests(connections)
|
booking_requests = connection_data_to_booking_requests(connections)
|
||||||
dprint(request_bookings(token, booking_requests[0]))
|
bookings = [request_bookings(token, req) for req in booking_requests]
|
||||||
|
dprint(extract_prices(bookings))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue