-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_f1_driver_lookup.c
More file actions
120 lines (97 loc) · 2.97 KB
/
basic_f1_driver_lookup.c
File metadata and controls
120 lines (97 loc) · 2.97 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "cJSON.h"
typedef struct {
char *data;
size_t size;
} Memory;
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 == NULL) {
printf("Memory allocation failed.\n");
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 == NULL) {
curl_easy_cleanup(curl);
return 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_driver(cJSON *driver) {
cJSON *num = cJSON_GetObjectItemCaseSensitive(driver, "driver_number");
cJSON *name = cJSON_GetObjectItemCaseSensitive(driver, "full_name");
cJSON *team = cJSON_GetObjectItemCaseSensitive(driver, "team_name");
cJSON *country = cJSON_GetObjectItemCaseSensitive(driver, "country_code");
if (cJSON_IsNumber(num) && cJSON_IsString(name) && cJSON_IsString(team)) {
printf("\nDriver Info\n");
printf("-------------------------\n");
printf("Number : %d\n", num->valueint);
printf("Name : %s\n", name->valuestring);
printf("Team : %s\n", team->valuestring);
if (cJSON_IsString(country)) {
printf("Country: %s\n", country->valuestring);
}
} else {
printf("Missing expected fields.\n");
}
}
int main(void) {
int driver_number;
char url[256];
printf("Enter driver number: ");
scanf("%d", &driver_number);
snprintf(url, sizeof(url),
"https://api.openf1.org/v1/drivers?driver_number=%d",
driver_number);
Memory chunk;
if (!fetch_api(url, &chunk)) {
return 1;
}
cJSON *root = cJSON_Parse(chunk.data);
if (root == NULL) {
printf("JSON parse error.\n");
free(chunk.data);
return 1;
}
if (!cJSON_IsArray(root) || cJSON_GetArraySize(root) == 0) {
printf("No driver found.\n");
cJSON_Delete(root);
free(chunk.data);
return 0;
}
cJSON *driver = cJSON_GetArrayItem(root, 0);
if (cJSON_IsObject(driver)) {
print_driver(driver);
}
cJSON_Delete(root);
free(chunk.data);
return 0;
}