-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_next_trains.py
40 lines (34 loc) · 1.03 KB
/
get_next_trains.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
import os
import requests
from dotenv import load_dotenv
# load environment variables from .env
load_dotenv()
# prompt for origin and destination stations
station_orig = input("Please enter the origin station code.\n")
station_dest = input("\nPlease enter the destination station code.\n")
# assemble API parameters
api_key = os.environ.get("BART_API_KEY")
payload = {
"cmd": "depart",
"orig": station_orig,
"dest": station_dest,
"date": "now",
"b": 0,
"a": 3,
"key": api_key,
"json": "y",
}
# call API and parse response
r = requests.get("https://api.bart.gov/api/sched.aspx", params=payload)
trips = r.json()["root"]["schedule"]["request"]["trip"]
# print out formatted trip information
print(f"\nNext trips between {station_orig} and {station_dest}:")
i = 1
for trip in trips:
print(
f"Option {i}: departs {station_orig} at {trip['@origTimeMin']} and "
f"arrives {station_dest} at {trip['@destTimeMin']} "
f"({trip['@tripTime']} minutes, ${trip['@fare']})."
)
i += 1
print()