Files
kiwi-budapest/regiojet.py
2022-03-05 14:22:28 +01:00

81 lines
2.8 KiB
Python

import requests
import json
import pprint
import argparse
from datetime import date, datetime, time, timedelta
url = "https://brn-ybus-pubapi.sa.cz"
tarrifs_path = "/restapi/consts/tariffs"
location_path = "/restapi/consts/locations"
route_path = "/restapi/routes/search/simple"
def json_serial(obj):
"""JSON serializer for objects not serializable by default json code"""
if isinstance(obj, timedelta):
return str(obj)
if isinstance(obj, (datetime, date)):
return obj.isoformat()
raise TypeError("Type %s not serializable" % type(obj))
def search_locations(country,city):
for location in locations:
if country == location['country']:
for _city in location['cities']:
if city == _city['name']:
return _city
def search_connection(from_station, to_station, tariff_type, to_location_type, from_location_type, departure):
r = requests.get(url + route_path, params={"tariffs_type": tariff_type,
"toLocationId": to_station["id"],
"fromLocationId": from_station["id"],
"fromLocationType": from_location_type,
"toLocationType": to_location_type,
"departureDate": departure})
routes = json.loads(r.content)
routes_ret = []
for route in routes['routes']:
ret = {}
ret["departure_datetime"] = datetime.fromisoformat(route["departureTime"])
ret["arrival_datetime"] = datetime.fromisoformat(route["arrivalTime"])
ret["source"] = from_station["name"]
ret["destination"] = to_station["name"]
ret["source_id"] = from_station["id"]
ret["destination_id"] = to_station["id"]
ret["free_seats"] = route["freeSeatsCount"]
ret["carrier"] = "REGIOJET"
ret["type"] = route["vehicleTypes"][0]
ret["fare"] = { "amount": route["priceFrom"], "currency": "EUR"}
routes_ret.append(ret)
return routes_ret
parser = argparse.ArgumentParser(
description='Search some connection') # use of ArgumentParser against of simple OptionParser
parser.add_argument("origin")
parser.add_argument("destination")
parser.add_argument("departure")
args = parser.parse_args()
r = requests.get(url + tarrifs_path)
tariffs = json.loads(r.content)
r = requests.get(url + location_path)
locations = json.loads(r.content)
city_from = search_locations('Czech Republic', args.origin)
city_to = search_locations('Czech Republic', args.destination)
#pprint.pp(city_from)
#pprint.pp(city_to)
ret = search_connection(city_from, city_to, 'REGULAR', 'CITY', 'CITY', args.departure)
print(json.dumps(ret, indent=4, default=json_serial, sort_keys=False))