-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf1_driver_analyzer.c
More file actions
220 lines (171 loc) · 5.67 KB
/
f1_driver_analyzer.c
File metadata and controls
220 lines (171 loc) · 5.67 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "cJSON.h"
typedef struct {
char *data;
size_t size;
} Memory;
typedef struct {
int session_key;
char country_name[64];
char date_start[64];
} SessionInfo;
typedef struct {
char race_name[64];
int position;
} RaceResult;
size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t real_size = size * nmemb;
Memory *mem = (Memory *)userp;
char *ptr = realloc(mem->data, mem->size + real_size + 1);
if (!ptr) {
return 0;
}
mem->data = ptr;
memcpy(mem->data + mem->size, contents, real_size);
mem->size += real_size;
mem->data[mem->size] = '\0';
return real_size;
}
int fetch_api(const char *url, Memory *chunk) {
CURL *curl = curl_easy_init();
if (!curl) {
printf("Curl init failed.\n");
return 0;
}
chunk->data = malloc(1);
chunk->size = 0;
if (!chunk->data) {
curl_easy_cleanup(curl);
return 0;
}
chunk->data[0] = '\0';
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
printf("Request failed: %s\n", curl_easy_strerror(res));
free(chunk->data);
return 0;
}
return 1;
}
void print_bar(int position) {
int bar_length = 21 - position; // P1 longest, P20 shortestt
if (bar_length < 1) bar_length = 1;
for (int i = 0; i < bar_length; i++) {
printf("#");
}
}
void run_driver_analyzer(void) {
int driver_number, year;
char url[256];
Memory chunk;
printf("Enter driver number: ");
scanf("%d", &driver_number);
printf("Enter year (2023+): ");
scanf("%d", &year);
if (year < 2023) {
printf("OpenF1 historical data is available from 2023 onwards only.\n");
return;
}
// gets all race sessions for the selected year
snprintf(url, sizeof(url),
"https://api.openf1.org/v1/sessions?year=%d&session_name=Race",
year);
if (!fetch_api(url, &chunk)) {
return;
}
cJSON *root = cJSON_Parse(chunk.data);
if (!root || !cJSON_IsArray(root)) {
printf("Invalid sessions JSON.\n");
printf("Raw response:\n%s\n", chunk.data);
free(chunk.data);
cJSON_Delete(root);
return;
}
SessionInfo sessions[64];
int session_count = 0;
int count = cJSON_GetArraySize(root);
for (int i = 0; i < count && session_count < 64; i++) {
cJSON *item = cJSON_GetArrayItem(root, i);
if (!cJSON_IsObject(item)) continue;
cJSON *session_key = cJSON_GetObjectItemCaseSensitive(item, "session_key");
cJSON *country_name = cJSON_GetObjectItemCaseSensitive(item, "country_name");
cJSON *date_start = cJSON_GetObjectItemCaseSensitive(item, "date_start");
if (cJSON_IsNumber(session_key) &&
cJSON_IsString(country_name) &&
cJSON_IsString(date_start)) {
sessions[session_count].session_key = session_key->valueint;
strncpy(sessions[session_count].country_name,
country_name->valuestring, 63);
sessions[session_count].country_name[63] = '\0';
strncpy(sessions[session_count].date_start,
date_start->valuestring, 63);
sessions[session_count].date_start[63] = '\0';
session_count++;
}
}
cJSON_Delete(root);
free(chunk.data);
if (session_count == 0) {
printf("No race sessions found for %d.\n", year);
return;
}
// for each race session, get final classification
RaceResult results[64];
int result_count = 0;
for (int i = 0; i < session_count && result_count < 64; i++) {
snprintf(url, sizeof(url),
"https://api.openf1.org/v1/session_result?session_key=%d",
sessions[i].session_key);
if (!fetch_api(url, &chunk)) {
continue;
}
root = cJSON_Parse(chunk.data);
if (!root || !cJSON_IsArray(root)) {
free(chunk.data);
cJSON_Delete(root);
continue;
}
int item_count = cJSON_GetArraySize(root);
for (int j = 0; j < item_count; j++) {
cJSON *item = cJSON_GetArrayItem(root, j);
if (!cJSON_IsObject(item)) continue;
cJSON *num = cJSON_GetObjectItemCaseSensitive(item, "driver_number");
cJSON *pos = cJSON_GetObjectItemCaseSensitive(item, "position");
if (cJSON_IsNumber(num) && cJSON_IsNumber(pos) &&
num->valueint == driver_number) {
strncpy(results[result_count].race_name,
sessions[i].country_name, 63);
results[result_count].race_name[63] = '\0';
results[result_count].position = pos->valueint;
result_count++;
break;
}
}
cJSON_Delete(root);
free(chunk.data);
}
if (result_count == 0) {
printf("No race result data found for driver #%d in %d.\n",
driver_number, year);
return;
}
// print ascii analysis
printf("\nDriver Analyzer - Final Race Results\n");
printf("------------------------------------\n");
for (int i = 0; i < result_count; i++) {
printf("%-15s | P%-2d | ", results[i].race_name, results[i].position);
print_bar(results[i].position);
printf("\n");
}
}
int main(void) {
run_driver_analyzer();
return 0;
}