{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "b7d2515e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully loaded in libraries\n"
]
}
],
"source": [
"import fastf1\n",
"import pandas as pd\n",
"from urllib.request import urlopen\n",
"from pprint import pprint\n",
"import json\n",
"print(\"Successfully loaded in libraries\")"
]
},
{
"cell_type": "markdown",
"id": "81c06a3f",
"metadata": {},
"source": [
"# FastF1"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "8109aed6",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"req WARNING \tDEFAULT CACHE ENABLED! (107.63 KB) /home/arre/.cache/fastf1\n",
"core INFO \tLoading data for Bahrain Grand Prix - Race [v3.5.3]\n",
"req INFO \tNo cached data found for session_info. Loading data...\n",
"_api INFO \tFetching session info data...\n",
"req INFO \tData has been written to cache!\n",
"req INFO \tNo cached data found for driver_info. Loading data...\n",
"_api INFO \tFetching driver list...\n",
"req INFO \tData has been written to cache!\n",
"req INFO \tNo cached data found for race_control_messages. Loading data...\n",
"_api INFO \tFetching race control messages...\n",
"req INFO \tData has been written to cache!\n",
"core INFO \tFinished loading data for 20 drivers: ['81', '63', '4', '16', '44', '1', '10', '31', '22', '87', '12', '23', '6', '7', '14', '30', '18', '5', '55', '27']\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Lewis: Bono my Tyres are dead, oh wait I'm in a Ferrari now\n"
]
}
],
"source": [
"session = fastf1.get_session(2025, 'Bahrain', 'Race')\n",
"session.load(telemetry=False, laps=False, weather=False)\n",
"driver = session.get_driver('HAM')\n",
"print(f\"{driver['FirstName']}: Bono my Tyres are dead, oh wait I'm in a Ferrari now\")"
]
},
{
"cell_type": "markdown",
"id": "a5047955",
"metadata": {},
"source": [
"# OpenF1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b5d31092",
"metadata": {},
"outputs": [],
"source": [
"def make_request(api_action_string: str, debug: bool = False):\n",
" try: \n",
" response = urlopen(f\"https://api.openf1.org/v1/{api_action_string}\")\n",
" data = json.loads(response.read().decode('utf-8'))\n",
" if debug: pprint(data)\n",
" return data\n",
" except Exception as e:\n",
" print(f\"Error: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "599aeec3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'brake': 0,\n",
" 'date': '2023-09-15T13:08:19.923000+00:00',\n",
" 'driver_number': 55,\n",
" 'drs': 12,\n",
" 'meeting_key': 1219,\n",
" 'n_gear': 8,\n",
" 'rpm': 11141,\n",
" 'session_key': 9159,\n",
" 'speed': 315,\n",
" 'throttle': 99},\n",
" {'brake': 100,\n",
" 'date': '2023-09-15T13:35:41.808000+00:00',\n",
" 'driver_number': 55,\n",
" 'drs': 8,\n",
" 'meeting_key': 1219,\n",
" 'n_gear': 8,\n",
" 'rpm': 11023,\n",
" 'session_key': 9159,\n",
" 'speed': 315,\n",
" 'throttle': 57}]\n"
]
}
],
"source": [
"# Respone object for OpenF1\n",
"\n",
"response = urlopen('https://api.openf1.org/v1/car_data?driver_number=55&session_key=9159&speed>=315')\n",
"data = json.loads(response.read().decode('utf-8'))\n",
"pprint(data)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "2b06a5c0",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" meeting_key | \n",
" session_key | \n",
" location | \n",
" date_start | \n",
" date_end | \n",
" session_type | \n",
" session_name | \n",
" country_key | \n",
" country_code | \n",
" country_name | \n",
" circuit_key | \n",
" circuit_short_name | \n",
" gmt_offset | \n",
" year | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1262 | \n",
" 9964 | \n",
" Barcelona | \n",
" 2025-05-30T11:30:00+00:00 | \n",
" 2025-05-30T12:30:00+00:00 | \n",
" Practice | \n",
" Practice 1 | \n",
" 1 | \n",
" ESP | \n",
" Spain | \n",
" 15 | \n",
" Catalunya | \n",
" 02:00:00 | \n",
" 2025 | \n",
"
\n",
" \n",
" 1 | \n",
" 1262 | \n",
" 9965 | \n",
" Barcelona | \n",
" 2025-05-30T15:00:00+00:00 | \n",
" 2025-05-30T16:00:00+00:00 | \n",
" Practice | \n",
" Practice 2 | \n",
" 1 | \n",
" ESP | \n",
" Spain | \n",
" 15 | \n",
" Catalunya | \n",
" 02:00:00 | \n",
" 2025 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" meeting_key session_key location date_start \\\n",
"0 1262 9964 Barcelona 2025-05-30T11:30:00+00:00 \n",
"1 1262 9965 Barcelona 2025-05-30T15:00:00+00:00 \n",
"\n",
" date_end session_type session_name country_key \\\n",
"0 2025-05-30T12:30:00+00:00 Practice Practice 1 1 \n",
"1 2025-05-30T16:00:00+00:00 Practice Practice 2 1 \n",
"\n",
" country_code country_name circuit_key circuit_short_name gmt_offset year \n",
"0 ESP Spain 15 Catalunya 02:00:00 2025 \n",
"1 ESP Spain 15 Catalunya 02:00:00 2025 "
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get session\n",
"respone = make_request(\"sessions?country_name=Spain&year=2025\", debug=False)\n",
"df = pd.DataFrame(respone)\n",
"df.head(n=2)\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "f13e4f1a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" meeting_key | \n",
" session_key | \n",
" location | \n",
" date_start | \n",
" date_end | \n",
" session_type | \n",
" session_name | \n",
" country_key | \n",
" country_code | \n",
" country_name | \n",
" circuit_key | \n",
" circuit_short_name | \n",
" gmt_offset | \n",
" year | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1262 | \n",
" 9966 | \n",
" Barcelona | \n",
" 2025-05-31T10:30:00+00:00 | \n",
" 2025-05-31T11:30:00+00:00 | \n",
" Practice | \n",
" Practice 3 | \n",
" 1 | \n",
" ESP | \n",
" Spain | \n",
" 15 | \n",
" Catalunya | \n",
" 02:00:00 | \n",
" 2025 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" meeting_key session_key location date_start \\\n",
"0 1262 9966 Barcelona 2025-05-31T10:30:00+00:00 \n",
"\n",
" date_end session_type session_name country_key \\\n",
"0 2025-05-31T11:30:00+00:00 Practice Practice 3 1 \n",
"\n",
" country_code country_name circuit_key circuit_short_name gmt_offset year \n",
"0 ESP Spain 15 Catalunya 02:00:00 2025 "
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Current session id (called during FP3 in Spain)\n",
"respone = make_request(\"sessions?session_key=latest\")\n",
"df = pd.DataFrame(respone)\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80a5699e",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" meeting_key | \n",
" session_key | \n",
" location | \n",
" date_start | \n",
" date_end | \n",
" session_type | \n",
" session_name | \n",
" country_key | \n",
" country_code | \n",
" country_name | \n",
" circuit_key | \n",
" circuit_short_name | \n",
" gmt_offset | \n",
" year | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1262 | \n",
" 9966 | \n",
" Barcelona | \n",
" 2025-05-31T10:30:00+00:00 | \n",
" 2025-05-31T11:30:00+00:00 | \n",
" Practice | \n",
" Practice 3 | \n",
" 1 | \n",
" ESP | \n",
" Spain | \n",
" 15 | \n",
" Catalunya | \n",
" 02:00:00 | \n",
" 2025 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" meeting_key session_key location date_start \\\n",
"0 1262 9966 Barcelona 2025-05-31T10:30:00+00:00 \n",
"\n",
" date_end session_type session_name country_key \\\n",
"0 2025-05-31T11:30:00+00:00 Practice Practice 3 1 \n",
"\n",
" country_code country_name circuit_key circuit_short_name gmt_offset year \n",
"0 ESP Spain 15 Catalunya 02:00:00 2025 "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Current session id V2 (called between FP3 and Quali in Spain)\n",
"respone = make_request(\"sessions?session_key=latest\")\n",
"df = pd.DataFrame(respone)\n",
"df.head()\n",
"# session_key=latest points to the current or the most recent session NOT the upcoming one"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}