-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgetbuses.py
43 lines (31 loc) · 1.22 KB
/
getbuses.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
#!/usr/bin/env python
import urllib.request
import json
import datetime
import sys
# By default, function searches for all MARTA routes.
def getBuses(route=''):
#Base URL for MARTA API
base = 'http://developer.itsmarta.com/BRDRestService/BRDRestService.svc/'
# If user does not input a value for route number, use 'GetAllBus' API call
if route == '':
query = 'GetAllBus'
# Else, use 'GetBusByRoute' API call with user-defined route number
else:
query = 'GetBusByRoute/' + route
# Formulate URL request and format response as json object
response = urllib.request.urlopen(base + query, timeout=30)
str_response = response.readall().decode('utf-8')
buses = json.loads(str_response)
# Prints entirety of json response
#print(buses)
# For each bus in response, print a few pieces of data.
for bus in buses:
print(bus['ROUTE'] + ' LAT:' + bus['LATITUDE'] + ' LON:' + bus['LONGITUDE'] + ' ADHER:' + bus['ADHERENCE'] + ' VEHICLE:' + bus['VEHICLE'] + '\n')
def main():
# Input function to obtain route number from user
route = input('\n\nPlease enter a route number (leave blank for all routes):\n\n')
# Call getBuses function with user-defined route number
getBuses(route)
if __name__ == '__main__':
main()