-
Notifications
You must be signed in to change notification settings - Fork 3
/
tfl_dump_bus_routes.py
30 lines (24 loc) · 1.07 KB
/
tfl_dump_bus_routes.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
#!/usr/python/python3
import os.path
import json
import urllib.request
import time
DUMPDIR = 'tfl_bus_routes'
# store here the TfL API key info - 2 vars named app_id and app_key
with open(os.path.expanduser('~/.tfl.json')) as f:
tflauth = json.load(f)
# this is what needs to be appended to every URL request
urlauth = 'app_id=%s&app_key=%s' % (tflauth['app_id'], tflauth['app_key'])
# get bus routes list
routesreq = urllib.request.urlopen('https://api.tfl.gov.uk/Line/Mode/bus?%s' % urlauth)
routes = json.loads(routesreq.read().decode())
# TODO: skip the outboundOnly: true lines
# TODO: skip lines with now stops (77 outbound, 150 inbound f.e.)
for route in routes:
for direction in ['inbound', 'outbound']:
print(route['id'])
routeinforeq = urllib.request.urlopen('https://api.tfl.gov.uk/line/%s/route/sequence/%s?%s' % (route['id'], direction, urlauth))
routeinfo = json.loads(routeinforeq.read().decode())
with open('%s/%s_%s.json' % (DUMPDIR, route['id'], direction), 'w') as f:
json.dump(routeinfo, f)
time.sleep(2)