Checking frequency of buses in a certain region

Hey so im currently doing a level coursework for geography and i want to find like the frequency of buses that go through Stratford station. I used ChatGPT for the code can you help me fix it? Also would it be possible to find all the bus stops for example in the stratford area and then find out the amount of buses that pass the area

import requests
from datetime import datetime, timedelta

API_KEY = ‘API KEY IK ITS HERE’
STRATFORD_BUS_STOP_ID = ‘490007291B’
BASE_URL = ‘https://api.tfl.gov.uk

def get_bus_arrivals(bus_stop_id):
“”“Fetch bus arrivals for a given bus stop ID.”“”
url = f’{BASE_URL}/StopPoint/{bus_stop_id}/Arrivals’
response = requests.get(url, headers={‘app_key’: API_KEY})
return response.json()

def count_buses_in_next_hour(arrivals):
“”“Count unique bus routes arriving within the next hour.”“”
now = datetime.now()
one_hour_later = now + timedelta(hours=1)

unique_buses = set()

for arrival in arrivals:
    arrival_time = datetime.fromisoformat(arrival['expectedArrivalTime'][:-1]) 
    if now <= arrival_time <= one_hour_later:
        unique_buses.add(arrival['lineName'])

return unique_buses

def main():
arrivals = get_bus_arrivals(STRATFORD_BUS_STOP_ID)

if isinstance(arrivals, list):
    unique_buses = count_buses_in_next_hour(arrivals)

    print(f'There are {len(unique_buses)} different buses arriving at Stratford Bus Station in the next hour:')
    for bus in unique_buses:
        print(bus)
else:
    print("Error fetching data. Please check the API key or bus stop ID.")

if name == ‘main’:
main()

Sorry the script kind of bugged out :sweat_smile:

I think i might have used the wrong bus stop code

@kai

I suggest you use the timetables! https://tfl.gov.uk/tfl/syndication/feeds/journey-planner-timetables.zip - they are in TransXchange format.

An example of the list for Stratford Bus Station

image

and Stratford City Bus station

Thanks, i actually figured it out myself which i found cool but i guess this website will help a lot, thanks!

1 Like

I have another question. I want to use TransportAPI to find all the train routes that travel through stratford. the only problem i have right now is that i dont know stratford’s name ID because i keep on getting errors when i use the api saying that the ID is incorrect.

https://transportapi.com/v3/uk/train/station_timetables/Stratford.json

@kai The master list of Naptan codes is

https://beta-naptan.dft.gov.uk/Download/National/csv

TransportAPI has a very tiny daily limit of uses, which can be annoying. .

Is this helping?

Unfortunately not

@kai

The CRS codes are the ones the national rail network uses, so Stratford is SRA

So you’ll be using (with your key!)

https://transportapi.com/v3/uk/train/station_timetables/crs:SRA.json

or

https://transportapi.com/v3/uk/train/station_timetables/tiploc:STFD.json

@kai

These people maintain an quite accurate list…

Thanks, you know a LOT about this :sweat_smile:

1 Like

More than necessary, probably…

BTW, I forgot to say that the error messages were regex