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()