Task 2, redis
This commit is contained in:
49
regiojet.py
49
regiojet.py
@@ -3,11 +3,28 @@ import json
|
||||
import pprint
|
||||
import argparse
|
||||
from datetime import date, datetime, time, timedelta
|
||||
import redis
|
||||
from slugify import slugify
|
||||
from typing import Optional
|
||||
from redis import Redis
|
||||
|
||||
url = "https://brn-ybus-pubapi.sa.cz"
|
||||
tarrifs_path = "/restapi/consts/tariffs"
|
||||
location_path = "/restapi/consts/locations"
|
||||
route_path = "/restapi/routes/search/simple"
|
||||
redis_host = "redis.pythonweekend.skypicker.com"
|
||||
#redis_host = "localhost"
|
||||
|
||||
|
||||
def store_dict(redis: Redis, key: str, value: dict) -> None:
|
||||
redis.set(key, json.dumps(value, default=json_serial))
|
||||
|
||||
|
||||
def retrieve_dict(redis: Redis, key: str) -> Optional[dict]:
|
||||
maybe_value = redis.get(name=key)
|
||||
if maybe_value is None:
|
||||
return None
|
||||
return json.loads(maybe_value)
|
||||
|
||||
|
||||
def json_serial(obj):
|
||||
@@ -36,6 +53,19 @@ def search_connection(from_station, to_station, tariff_type, to_location_type, f
|
||||
"departureDate": departure})
|
||||
|
||||
routes = json.loads(r.content)
|
||||
surname = "jaro"
|
||||
source = from_station["name"]
|
||||
destination = to_station["name"]
|
||||
|
||||
key1 = F"{surname}:journey"
|
||||
key2 = slugify(source)
|
||||
key3 = slugify(destination)
|
||||
key4 = departure
|
||||
key = ':'.join((key1, key2 + '_' + key3 + '_' + key4))
|
||||
journey = retrieve_dict(redisdb, key)
|
||||
if journey is not None:
|
||||
return journey
|
||||
|
||||
routes_ret = []
|
||||
for route in routes['routes']:
|
||||
ret = {}
|
||||
@@ -48,9 +78,10 @@ def search_connection(from_station, to_station, tariff_type, to_location_type, f
|
||||
ret["free_seats"] = route["freeSeatsCount"]
|
||||
ret["carrier"] = "REGIOJET"
|
||||
ret["type"] = route["vehicleTypes"][0]
|
||||
ret["fare"] = { "amount": route["priceFrom"], "currency": "EUR"}
|
||||
ret["fare"] = {"amount": route["priceFrom"], "currency": "EUR"}
|
||||
routes_ret.append(ret)
|
||||
|
||||
store_dict(redisdb, key, routes_ret)
|
||||
return routes_ret
|
||||
|
||||
|
||||
@@ -62,11 +93,19 @@ parser.add_argument("departure")
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
r = requests.get(url + tarrifs_path)
|
||||
tariffs = json.loads(r.content)
|
||||
redisdb = Redis(host=redis_host, port=6379, db=0, decode_responses=True)
|
||||
|
||||
r = requests.get(url + location_path)
|
||||
locations = json.loads(r.content)
|
||||
tariffs = retrieve_dict(redisdb,'jaro:REGIOJET:tariffs')
|
||||
if tariffs is None:
|
||||
r = requests.get(url + tarrifs_path)
|
||||
tariffs = json.loads(r.content)
|
||||
store_dict(redisdb, 'jaro:REGIOJET:tariffs', tariffs)
|
||||
|
||||
locations = retrieve_dict(redisdb,'jaro:REGIOJET:locations')
|
||||
if locations is None:
|
||||
r = requests.get(url + location_path)
|
||||
locations = json.loads(r.content)
|
||||
store_dict(redisdb, 'jaro:REGIOJET:locations', locations)
|
||||
|
||||
city_from = search_locations('Czech Republic', args.origin)
|
||||
city_to = search_locations('Czech Republic', args.destination)
|
||||
|
||||
Reference in New Issue
Block a user