results.py
Theresults.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 theRaceResultsFetcher 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
base_dir(str): Base directory for data storage (default: current directory)
base_dir(Path): Root directory for race database_url(str): Ergast API base URLburst_limit(int): Maximum requests per secondlast_request_time(float): Timestamp of last API request
Methods
get_race_folder_name()
Converts a race object to a folder name.results.py
race(dict): Race information object
str: Folder name (e.g., “british-grand-prix”)
make_request()
Makes an API request with rate limiting and retry logic.results.py
url(str): API endpoint URL
dict: JSON response orNoneon 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
season(int): F1 season yearround_num(int): Round number in the season
dict: Race information orNoneif not found
get_race_results()
Fetches race results for a specific round.results.py
season(int): F1 season yearround_num(int): Round number
dict: Complete results data
save_json()
Saves data to a JSON file.results.py
data(dict): Data to savefilepath(str): Destination file path
bool:Trueon success,Falseon error
fetch_round()
Main method to fetch and save race results.results.py
season(int): F1 season yearround_num(int): Round number
Usage
Basic Usage
Fetch Multiple Races
Command Line Usage
API Endpoint
Output Structure
Results are saved to:{year}/{race-slug}/results.json
Logging
The script creates a log file:race_results_fetch.log