forked from divulgacheur/TGV_Maximize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct_destination.py
64 lines (53 loc) · 2.24 KB
/
direct_destination.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from operator import itemgetter
from requests import get, Response as ReqResponse
from station import Station
class DirectDestination:
"""
Class for finding the direct destinations of a given station.
"""
station: Station
destination = {'station': Station, 'duration': int}
destinations: dict[str, destination]
def __init__(self, station: Station, destinations: dict[str, destination]):
self.station = station
self.destinations = destinations
@staticmethod
def parse(station: Station, request: ReqResponse):
"""
Parses the direct destinations of a given station and JSON response of the API.
"""
destinations = {
station['id']: {
'station':
Station(
name=station['name'],
coordinates=(
station['location']['latitude'], station['location']['longitude']),
identifier=station['id'],
),
'duration':
station['duration']}
for station in request.json()}
return DirectDestination(station, destinations)
def get_common_stations(self: 'DirectDestination',
arrival_direct_destinations: 'DirectDestination') -> [Station]:
"""
Returns a list of stations that are common to both the departure and arrival stations.
:param self: The departure station's direct destinations.
:param arrival_direct_destinations: The arrival station's direct destinations.
:return: A list of stations
"""
destinations_keys = set(self.destinations.keys()).intersection(
arrival_direct_destinations.destinations.keys())
destinations = list(itemgetter(*destinations_keys)(
self.destinations | arrival_direct_destinations.destinations))
print(len(destinations), 'intermediate stations available')
return destinations
@staticmethod
def get(departure):
"""
Returns the direct destinations of a given station.
"""
return DirectDestination.parse(
departure,
get('https://api.direkt.bahn.guru/' + departure.identifier))