Skip to main content

events.py

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.

Overview

This script:
  • Fetches the complete race calendar from the Ergast API
  • Creates year and race directories
  • Saves season-level event data to events.json
  • Saves individual race information to event_info.json in each race folder
  • Uses rate limiting to respect API constraints

Functions

slugify()

Converts race names to URL-friendly slugs for directory naming.
events.py
Parameters:
  • race_name (str): The official race name (e.g., “Belgian Grand Prix”)
Returns:
  • str: URL-friendly slug (e.g., “belgian-grand-prix”)
Example:

create_directory()

Creates a directory if it doesn’t already exist.
events.py
Parameters:
  • path (str): Directory path to create

fetch_with_rate_limit()

Fetches data from a URL with built-in rate limiting and retry logic.
events.py
Parameters:
  • url (str): API endpoint URL
Returns:
  • dict: Parsed JSON response
The function implements a 0.3 second delay between requests to stay within the Ergast API’s 4 requests per second limit.

main()

Main execution function that orchestrates the data collection process.
events.py

Usage

Fetch a Single Season

Fetch Multiple Seasons

API Endpoint

The script calls the following Ergast API endpoint:
Response Structure:

Output Files

events.json (Season Level)

Stored in: {year}/events.json Contains the complete race calendar for the season.

event_info.json (Race Level)

Stored in: {year}/{race-slug}/event_info.json Contains detailed information for a specific race including:
  • Circuit details
  • Race date and time
  • Session schedule (practice, qualifying, race)
  • Wikipedia reference URL

Configuration

The script can be configured by editing these variables:

Best Practices

Run First

Always run events.py before other data collection scripts to create the directory structure

Respect Rate Limits

Don’t reduce the sleep delay below 0.25 seconds to avoid hitting rate limits

Check API Status

Verify the Ergast API is accessible before fetching large date ranges

Incremental Updates

For current seasons, run periodically to fetch newly added races

See Also