Introducing Station class

This commit is contained in:
nemunaire 2016-07-10 18:14:46 +02:00
parent 87a6ee458c
commit 14934ce7da
2 changed files with 29 additions and 6 deletions

View File

@ -15,6 +15,29 @@ CT_HEADERS = {
} }
# Structures
class Station:
def __init__(self, id, is_sellable, name, slug, score, info, latitude, longitude):
self._id = id
self._is_sellable = is_sellable
self._name = name
self._slug = slug
self._score = score
self._info = info
self._latitude = latitude
self._longitude = longitude
@property
def id(self):
return self._id
@property
def name(self):
return self._name
# API calls # API calls
def stations(name): def stations(name):
@ -27,8 +50,8 @@ def stations(name):
with urllib.request.urlopen(req) as res: with urllib.request.urlopen(req) as res:
stations = json.loads(res.read().decode())["stations"] stations = json.loads(res.read().decode())["stations"]
sorted(stations, key=lambda station: station["score"], reverse=True) sorted(stations, key=lambda station: station["score"], reverse=True)
return stations for station in stations:
return [] yield Station(**station)
def station(name): def station(name):
@ -64,9 +87,9 @@ def search(departure, arrival, departure_date, return_date=None):
], ],
"systems": ["sncf","db","busbud","idtgv","ouigo","trenitalia","ntv","hkx","renfe","timetable"], "systems": ["sncf","db","busbud","idtgv","ouigo","trenitalia","ntv","hkx","renfe","timetable"],
"exchangeable_part": None, "exchangeable_part": None,
"departure_station_id": departure["id"], "departure_station_id": departure.id,
"via_station_id": None, "via_station_id": None,
"arrival_station_id": arrival["id"], "arrival_station_id": arrival.id,
"exchangeable_pnr_id": None "exchangeable_pnr_id": None
} }
}).encode()) }).encode())

View File

@ -20,8 +20,8 @@ if __name__ == "__main__":
else: else:
departure_time = ct.parse_datetime(departure_time) departure_time = ct.parse_datetime(departure_time)
print("From:", departure["name"], departure["id"]) print("From:", departure.name, departure.id)
print("To:", arrival["name"], arrival["id"]) print("To:", arrival.name, arrival.id)
print("Departure:", departure_time) print("Departure:", departure_time)
#with open("res.json") as f: #with open("res.json") as f: