Skip to content

Instantly share code, notes, and snippets.

@rmasters
Created February 18, 2018 19:21
Show Gist options
  • Save rmasters/d36a214833cac483250da798939c1a7c to your computer and use it in GitHub Desktop.
Save rmasters/d36a214833cac483250da798939c1a7c to your computer and use it in GitHub Desktop.
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