-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrivals.py
64 lines (48 loc) · 1.76 KB
/
arrivals.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
import requests
import constants
def get_station_id(station_name):
api_key = constants.TFL_API_KEY
endpoint = 'https://api.tfl.gov.uk/Place/Search'
params = {
'app_key': api_key,
'query': station_name,
'categories': 'Station',
}
response = requests.get(endpoint, params=params)
if response.status_code == 200:
results = response.json()
if results and 'id' in results[0]:
return results[0]['id']
else:
print(f"Error {response.status_code}: {response.text}")
return None
def get_next_departures(station_id, num_departures=10):
api_key = constants.TFL_API_KEY
endpoint = f'https://api.tfl.gov.uk/StopPoint/{station_id}/arrivals'
params = {
'app_key': api_key,
}
response = requests.get(endpoint, params=params)
if response.status_code == 200:
departures = response.json()
return departures[:num_departures]
else:
print(f"Error {response.status_code}: {response.text}")
return None
def main():
station_name = 'Denmark Hill'
station_id = '910GDENMRKH'
if station_id:
print(f"Station ID for {station_name}: {station_id}")
departures = get_next_departures(station_id)
if departures:
print("Next 10 Departures:")
for departure in departures:
line_name = departure.get('lineName', 'N/A')
towards = departure.get('destinationName', 'N/A')
expected_arrival = departure.get('expectedArrival', 'N/A')
print(f"Line: {line_name} - Towards: {towards} - Expected Arrival: {expected_arrival}")
else:
print(f"Unable to retrieve Station ID for {station_name}")
if __name__ == "__main__":
main()