The events.py script is responsible for fetching the Formula 1 race calendar for a specified season and creating the initial directory structure for storing race data. This is typically the first script to run when collecting data for a new season.
Main execution function that orchestrates the data collection process.
events.py
def main(): base_dir = "." start_year = 2026 end_year = 2026 for year in range(start_year, end_year + 1): print(f"Processing year {year}...") # Create year directory year_dir = os.path.join(base_dir, str(year)) create_directory(year_dir) # Fetch races for the year url = f"https://api.jolpi.ca/ergast/f1/{year}/races/" data = fetch_with_rate_limit(url) # Save year data to events.json with open(os.path.join(year_dir, f"events.json"), "w") as f: json.dump(data, f, indent=2) # Process each race if ("MRData" in data and "RaceTable" in data["MRData"] and "Races" in data["MRData"]["RaceTable"]): races = data["MRData"]["RaceTable"]["Races"] for race in races: race_name = race["raceName"] race_slug = slugify(race_name) # Create race directory race_dir = os.path.join(year_dir, f"{race_slug}") create_directory(race_dir) # Save race data to event_info.json with open(os.path.join(race_dir, "event_info.json"), "w") as f: json.dump(race, f, indent=2) print(f"Processed: {year} - {race_name}")
The script can be configured by editing these variables:
# Directory for data storagebase_dir = "."# Season range to fetchstart_year = 2026end_year = 2026# Rate limiting delay (seconds)time.sleep(0.3) # 0.3 seconds = ~3.3 req/sec