File size: 13,891 Bytes
81c6a41 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
{
"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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>meeting_key</th>\n",
" <th>session_key</th>\n",
" <th>location</th>\n",
" <th>date_start</th>\n",
" <th>date_end</th>\n",
" <th>session_type</th>\n",
" <th>session_name</th>\n",
" <th>country_key</th>\n",
" <th>country_code</th>\n",
" <th>country_name</th>\n",
" <th>circuit_key</th>\n",
" <th>circuit_short_name</th>\n",
" <th>gmt_offset</th>\n",
" <th>year</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1262</td>\n",
" <td>9964</td>\n",
" <td>Barcelona</td>\n",
" <td>2025-05-30T11:30:00+00:00</td>\n",
" <td>2025-05-30T12:30:00+00:00</td>\n",
" <td>Practice</td>\n",
" <td>Practice 1</td>\n",
" <td>1</td>\n",
" <td>ESP</td>\n",
" <td>Spain</td>\n",
" <td>15</td>\n",
" <td>Catalunya</td>\n",
" <td>02:00:00</td>\n",
" <td>2025</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1262</td>\n",
" <td>9965</td>\n",
" <td>Barcelona</td>\n",
" <td>2025-05-30T15:00:00+00:00</td>\n",
" <td>2025-05-30T16:00:00+00:00</td>\n",
" <td>Practice</td>\n",
" <td>Practice 2</td>\n",
" <td>1</td>\n",
" <td>ESP</td>\n",
" <td>Spain</td>\n",
" <td>15</td>\n",
" <td>Catalunya</td>\n",
" <td>02:00:00</td>\n",
" <td>2025</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>meeting_key</th>\n",
" <th>session_key</th>\n",
" <th>location</th>\n",
" <th>date_start</th>\n",
" <th>date_end</th>\n",
" <th>session_type</th>\n",
" <th>session_name</th>\n",
" <th>country_key</th>\n",
" <th>country_code</th>\n",
" <th>country_name</th>\n",
" <th>circuit_key</th>\n",
" <th>circuit_short_name</th>\n",
" <th>gmt_offset</th>\n",
" <th>year</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1262</td>\n",
" <td>9966</td>\n",
" <td>Barcelona</td>\n",
" <td>2025-05-31T10:30:00+00:00</td>\n",
" <td>2025-05-31T11:30:00+00:00</td>\n",
" <td>Practice</td>\n",
" <td>Practice 3</td>\n",
" <td>1</td>\n",
" <td>ESP</td>\n",
" <td>Spain</td>\n",
" <td>15</td>\n",
" <td>Catalunya</td>\n",
" <td>02:00:00</td>\n",
" <td>2025</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>meeting_key</th>\n",
" <th>session_key</th>\n",
" <th>location</th>\n",
" <th>date_start</th>\n",
" <th>date_end</th>\n",
" <th>session_type</th>\n",
" <th>session_name</th>\n",
" <th>country_key</th>\n",
" <th>country_code</th>\n",
" <th>country_name</th>\n",
" <th>circuit_key</th>\n",
" <th>circuit_short_name</th>\n",
" <th>gmt_offset</th>\n",
" <th>year</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1262</td>\n",
" <td>9966</td>\n",
" <td>Barcelona</td>\n",
" <td>2025-05-31T10:30:00+00:00</td>\n",
" <td>2025-05-31T11:30:00+00:00</td>\n",
" <td>Practice</td>\n",
" <td>Practice 3</td>\n",
" <td>1</td>\n",
" <td>ESP</td>\n",
" <td>Spain</td>\n",
" <td>15</td>\n",
" <td>Catalunya</td>\n",
" <td>02:00:00</td>\n",
" <td>2025</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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
}
|