-
Notifications
You must be signed in to change notification settings - Fork 0
/
bahnconnection.py
44 lines (33 loc) · 1.45 KB
/
bahnconnection.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
import requests
base_url = 'https://v5.db.transport.rest/'
def station(name):
query_params = {'query': name, 'results': 1}
response = requests.get(base_url + 'locations', params=query_params).json()
return response[0]['id']
def journeys(start_id, destination_id, max_journeys=3):
query_params = {'from': start_id, 'to': destination_id}
response = requests.get(base_url + 'journeys', params=query_params).json()['journeys']
return response[:min(max_journeys, len(response))]
def board_information(all_journeys):
board = dict()
board['trains'] = list()
for leg in [journey['legs'][0] for journey in all_journeys]:
board['start'] = leg['origin']['name']
if leg['departure']:
train = dict()
train['planned_departure'] = leg['plannedDeparture']
if leg['departureDelay']:
train['delay'] = str(int(leg['departureDelay'])//60)
else:
train['delay'] = '0'
train['platform'] = leg['departurePlatform']
train['train'] = leg['line']['name']
board['trains'].append(train)
# Get correct destination
legs = all_journeys[0]['legs']
board['destination'] = legs[len(legs)-1]['destination']['name']
return board
def connections(start_name, destination_name):
start_id = station(start_name)
destination_id = station(destination_name)
return board_information(journeys(start_id, destination_id))