Created
February 18, 2018 19:21
-
-
Save rmasters/d36a214833cac483250da798939c1a7c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
# Say the user's home station is Woolwich Arsenal and they travel by NR and DLR | |
search_result = requests.get('https://api.tfl.gov.uk/StopPoint/Search?query=woolwich').json()['matches'][0] | |
# Get the StopPoint's line data | |
stoppoint = requests.get('https://api.tfl.gov.uk/StopPoint/{}'.format(search_result['id'])).json() | |
# Set modes for each requested mode that's actually provided by the station | |
requested_modes = ['dlr', 'national-rail'] | |
modes = set(requested_modes).intersection(stoppoint['modes']) | |
# Get a list of lineIdentifiers for each mode | |
# DLR may be 'dlr' but national-rail could be any/multiple TOCs, in this case 'southeastern' | |
# e.g. mode_lines = {'dlr': ['dlr'], 'national-rail': ['southeastern']} | |
for mode in modes: | |
# Get lines served on this mode | |
lines = [lineId | |
for lineModeGroup in stoppoint['lineModeGroups'] | |
for lineId in lineModeGroup['lineIdentifier'] | |
if lineModeGroup['modeName'] == mode] | |
# Get station NaPTAN IDs for each line | |
station_ids = set([group['stationAtcoCode'] | |
for line in lines | |
for group in stoppoint['lineGroup'] | |
if line in group['lineIdentifier']]) | |
print(mode, station_ids) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment