Task 4, API
This commit is contained in:
52
regiojet.py
52
regiojet.py
@@ -48,7 +48,18 @@ def search_locations(country,city):
|
||||
return _city
|
||||
|
||||
|
||||
def search_connection_regiojet(from_station, to_station, tariff_type, to_location_type, from_location_type, departure):
|
||||
def filter_results(data, arrival):
|
||||
|
||||
#data = [r for r in data if datetime.fromisoformat(r["arrival_datetime"]) <= datetime.fromisoformat(arrival)]
|
||||
return data
|
||||
|
||||
|
||||
def search_connection_regiojet(from_station, to_station, tariff_type, to_location_type, from_location_type, departure, arrival):
|
||||
from_station = search_locations('Czech Republic', from_station)
|
||||
to_station = search_locations('Czech Republic', to_station)
|
||||
departure = departure.isoformat()
|
||||
arrival = arrival.isoformat()
|
||||
|
||||
surname = "jaro"
|
||||
source = from_station["name"]
|
||||
destination = to_station["name"]
|
||||
@@ -60,13 +71,16 @@ def search_connection_regiojet(from_station, to_station, tariff_type, to_locatio
|
||||
key = ':'.join((key1, key2 + '_' + key3 + '_' + key4))
|
||||
|
||||
journey = retrieve_dict(redisdb, key)
|
||||
|
||||
if journey is not None:
|
||||
journey_db = get_from_db(source, destination, departure)
|
||||
journey = filter_results(journey, arrival)
|
||||
|
||||
journey_db = get_from_db(source, destination, departure, arrival)
|
||||
if journey_db is None:
|
||||
store_in_db(journey)
|
||||
return journey
|
||||
|
||||
journey = get_from_db(source, destination, departure)
|
||||
journey = get_from_db(source, destination, departure, arrival)
|
||||
if journey is not None:
|
||||
return journey
|
||||
|
||||
@@ -80,10 +94,16 @@ def search_connection_regiojet(from_station, to_station, tariff_type, to_locatio
|
||||
routes = json.loads(r.content)
|
||||
|
||||
routes_ret = []
|
||||
if 'routes' not in routes:
|
||||
return routes_ret
|
||||
|
||||
for route in routes['routes']:
|
||||
ret = {}
|
||||
ret["departure_datetime"] = datetime.fromisoformat(route["departureTime"])
|
||||
ret["arrival_datetime"] = datetime.fromisoformat(route["arrivalTime"])
|
||||
#ret["departure_datetime"] = datetime.fromisoformat(route["departureTime"])
|
||||
#ret["arrival_datetime"] = datetime.fromisoformat(route["arrivalTime"])
|
||||
ret["departure_datetime"] = route["departureTime"]
|
||||
ret["arrival_datetime"] = route["arrivalTime"]
|
||||
|
||||
ret["source"] = from_station["name"]
|
||||
ret["destination"] = to_station["name"]
|
||||
ret["source_id"] = from_station["id"]
|
||||
@@ -122,7 +142,6 @@ class Journey(Base):
|
||||
|
||||
def transform_data(data):
|
||||
ret = {}
|
||||
pprint.pp(data)
|
||||
ret["source"] = data["source"]
|
||||
ret["destination"] = data["destination"]
|
||||
ret["departure_datetime"] = data["departure_datetime"]
|
||||
@@ -137,7 +156,7 @@ def transform_data(data):
|
||||
return ret
|
||||
|
||||
|
||||
def get_from_db(source, destination, departure):
|
||||
def get_from_db(source, destination, departure, arrival):
|
||||
Session = sessionmaker(engine)
|
||||
|
||||
with Session() as session:
|
||||
@@ -145,7 +164,8 @@ def get_from_db(source, destination, departure):
|
||||
result = session.query(Journey).filter(
|
||||
Journey.source == source,
|
||||
Journey.destination == destination,
|
||||
Journey.departure_datetime == departure
|
||||
Journey.departure_datetime == departure,
|
||||
Journey.arrival_datetime == arrival
|
||||
).all()
|
||||
|
||||
cached_data = []
|
||||
@@ -218,14 +238,6 @@ engine = create_engine(
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
|
||||
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()
|
||||
|
||||
|
||||
redisdb = Redis(host=redis_host, port=6379, db=0, decode_responses=True)
|
||||
|
||||
tariffs = retrieve_dict(redisdb,'jaro:REGIOJET:tariffs')
|
||||
@@ -240,12 +252,4 @@ if locations is None:
|
||||
locations = json.loads(r.content)
|
||||
store_dict_in_redis(redisdb, 'jaro:REGIOJET:locations', locations)
|
||||
|
||||
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_regiojet(city_from, city_to, 'REGULAR', 'CITY', 'CITY', args.departure)
|
||||
|
||||
|
||||
print(json.dumps(ret, indent=4, default=json_serial, sort_keys=False))
|
||||
Reference in New Issue
Block a user