Add price extraction
This commit is contained in:
parent
0f306809d4
commit
6b4b9e5953
4 changed files with 7914 additions and 1082 deletions
7862
bookings.json
7862
bookings.json
File diff suppressed because one or more lines are too long
2
lowest.csv
Normal file
2
lowest.csv
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
couchette6,99.9,Compartment for 6 passengers
|
||||||
|
couchette6,99.9,Compartment for 6 passengers
|
||||||
|
58
main.py
58
main.py
|
|
@ -1,4 +1,6 @@
|
||||||
import json
|
import json
|
||||||
|
import csv
|
||||||
|
from dataclasses import dataclass
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
@ -8,6 +10,7 @@ BASE_URL = "https://www.nightjet.com"
|
||||||
|
|
||||||
|
|
||||||
def dprint(txt) -> None:
|
def dprint(txt) -> None:
|
||||||
|
return
|
||||||
print(txt)
|
print(txt)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -93,7 +96,7 @@ 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]:
|
||||||
with open("bookings.json") as f:
|
with open("bookings.json") as f:
|
||||||
DEBUG_BOOKINGS = json.load(f)
|
DEBUG_BOOKINGS = json.load(f)[0]
|
||||||
resp_json = DEBUG_BOOKINGS
|
resp_json = DEBUG_BOOKINGS
|
||||||
|
|
||||||
# headers = {
|
# headers = {
|
||||||
|
|
@ -133,13 +136,50 @@ def json_extract(obj, key):
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
|
||||||
def extract_prices(bookings_dict: list[dict[Any, Any]]) -> dict[Any, Any]:
|
@dataclass
|
||||||
|
class Price:
|
||||||
|
id: str
|
||||||
|
name: str
|
||||||
|
price: float
|
||||||
|
|
||||||
|
|
||||||
|
def extract_prices(bookings_dict: list[dict[Any, Any]]) -> list[Price]:
|
||||||
|
prices = []
|
||||||
# .result[].connections[].offers[].reservation.reservationSegments[].compartments[].objects
|
# .result[].connections[].offers[].reservation.reservationSegments[].compartments[].objects
|
||||||
for booking in bookings_dict:
|
for booking in bookings_dict:
|
||||||
for r in booking["result"]:
|
for reservation in booking["result"]:
|
||||||
for c in r["connections"]:
|
for connection in reservation["connections"]:
|
||||||
for o in c["offers"]:
|
for offer in connection["offers"]:
|
||||||
print(o["name"])
|
for reservation in offer["reservation"]["reservationSegments"]:
|
||||||
|
for compartment in reservation["compartments"]:
|
||||||
|
id = compartment["externalIdentifier"]
|
||||||
|
name = compartment["name"]["en"]
|
||||||
|
# filter undesired compartments
|
||||||
|
if id in ["sideCorridorCoach_2"]:
|
||||||
|
continue
|
||||||
|
# print all compartment identifiers w/ full name
|
||||||
|
# dprint(f"{id}: {name}")
|
||||||
|
|
||||||
|
# only keep those with a price (i.e. bookable?)
|
||||||
|
if "objects" not in compartment:
|
||||||
|
continue
|
||||||
|
price = compartment["objects"][0]["price"]
|
||||||
|
prices.append(Price(id, name, price))
|
||||||
|
return prices
|
||||||
|
|
||||||
|
|
||||||
|
def get_lowest_price(prices: list[Price]) -> Price:
|
||||||
|
lowest = Price("", "", 10000000.0)
|
||||||
|
for p in prices:
|
||||||
|
if p.price < lowest.price:
|
||||||
|
lowest = p
|
||||||
|
return lowest
|
||||||
|
|
||||||
|
CSV_LOWEST_FILE="lowest.csv"
|
||||||
|
def add_to_csv(price: Price) -> None:
|
||||||
|
with open(CSV_LOWEST_FILE, "a") as f:
|
||||||
|
writer = csv.writer(f)
|
||||||
|
writer.writerow([price.id, price.price, price.name])
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
@ -147,7 +187,11 @@ def main():
|
||||||
connections = request_connections(token)
|
connections = request_connections(token)
|
||||||
booking_requests = connection_data_to_booking_requests(connections)
|
booking_requests = connection_data_to_booking_requests(connections)
|
||||||
bookings = [request_bookings(token, req) for req in booking_requests]
|
bookings = [request_bookings(token, req) for req in booking_requests]
|
||||||
dprint(extract_prices(bookings))
|
prices = extract_prices(bookings)
|
||||||
|
|
||||||
|
lowest = get_lowest_price(prices)
|
||||||
|
add_to_csv(lowest)
|
||||||
|
# dprint(extract_prices(bookings))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue