Skip to main content

results.py

The results.py script fetches race results for Formula 1 Grand Prix events. It retrieves finishing positions, race times, points awarded, and status information for each driver in a race.

Overview

This script uses an object-oriented approach with the RaceResultsFetcher class to:
  • Fetch race results from the Ergast API
  • Implement rate limiting to respect API constraints
  • Save results to structured JSON files
  • Handle errors and retries gracefully

RaceResultsFetcher Class

Initialization

results.py
Parameters:
  • base_dir (str): Base directory for data storage (default: current directory)
Attributes:
  • base_dir (Path): Root directory for race data
  • base_url (str): Ergast API base URL
  • burst_limit (int): Maximum requests per second
  • last_request_time (float): Timestamp of last API request

Methods

get_race_folder_name()

Converts a race object to a folder name.
results.py
Parameters:
  • race (dict): Race information object
Returns:
  • str: Folder name (e.g., “british-grand-prix”)

make_request()

Makes an API request with rate limiting and retry logic.
results.py
Parameters:
  • url (str): API endpoint URL
Returns:
  • dict: JSON response or None on error
The method automatically retries after 30 seconds if a 429 (rate limit) response is received.

get_race_info()

Fetches basic race information.
results.py
Parameters:
  • season (int): F1 season year
  • round_num (int): Round number in the season
Returns:
  • dict: Race information or None if not found

get_race_results()

Fetches race results for a specific round.
results.py
Parameters:
  • season (int): F1 season year
  • round_num (int): Round number
Returns:
  • dict: Complete results data

save_json()

Saves data to a JSON file.
results.py
Parameters:
  • data (dict): Data to save
  • filepath (str): Destination file path
Returns:
  • bool: True on success, False on error

fetch_round()

Main method to fetch and save race results.
results.py
Parameters:
  • season (int): F1 season year
  • round_num (int): Round number

Usage

Basic Usage

Fetch Multiple Races

Command Line Usage

API Endpoint

Example:

Output Structure

Results are saved to: {year}/{race-slug}/results.json

Logging

The script creates a log file: race_results_fetch.log

See Also