1+ #include < string>
2+ #include < vector>
3+ #include < curl/curl.h>
4+ #include < cstdlib>
5+ #include < fmt/core.h>
6+ #include < fmt/format.h>
7+ #include < simdjson.h>
8+
9+ #if !SIMDJSON_STATIC_REFLECTION
10+ #error "You need to enable static reflection for this to work"
11+ #endif
12+
13+
14+ struct weather_data {
15+ std::vector<std::string> time;
16+ std::vector<float > temperature_2m;
17+ std::vector<float > relative_humidity_2m;
18+ std::vector<float > winddirection_10m;
19+ std::vector<float > precipitation;
20+ std::vector<float > windspeed_10m;
21+ };
22+
23+ std::string grab_weather_data (const std::string& latitude, const std::string& longitude) {
24+ std::string url = fmt::format (" https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&hourly=temperature_2m,relative_humidity_2m,winddirection_10m,precipitation,windspeed_10m" , latitude, longitude);
25+ CURL *curl = curl_easy_init ();
26+ if (!curl) {
27+ throw std::runtime_error (" Could not initialize cURL" );
28+ }
29+ std::string response_data;
30+ curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
31+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, +[](char *ptr, size_t size, size_t nmemb, void *userdata) -> size_t {
32+ auto *str = static_cast <std::string*>(userdata);
33+ str->append (ptr, size * nmemb);
34+ return size * nmemb;
35+ });
36+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &response_data);
37+ CURLcode res = curl_easy_perform (curl);
38+ if (res != CURLE_OK) {
39+ curl_easy_cleanup (curl);
40+ throw std::runtime_error (" Request failed cURL: " + std::string (curl_easy_strerror (res)));
41+ }
42+ curl_easy_cleanup (curl);
43+ return response_data;
44+ }
45+
46+ int main () {
47+ std::string weather_data_str = grab_weather_data (" 45.5017" , " -73.5673" );
48+ simdjson::ondemand::parser parser;
49+ simdjson::ondemand::document doc = parser.iterate(simdjson::pad (weather_data_str));
50+ weather_data wd = doc[" hourly" ].get <weather_data>();
51+ // Assuming all vectors have the same length
52+ for (size_t i = 0 ; i < wd.time .size (); ++i) {
53+ fmt::print (" Time: {}, Temperature: {:.1f}°C, Humidity: {:.1f}%, Wind Direction: {:.1f}°, Precipitation: {:.1f}mm, Wind Speed: {:.1f}km/h\n " ,
54+ wd.time [i],
55+ wd.temperature_2m [i],
56+ wd.relative_humidity_2m [i],
57+ wd.winddirection_10m [i],
58+ wd.precipitation [i],
59+ wd.windspeed_10m [i]);
60+ }
61+ return EXIT_SUCCESS;
62+ }
0 commit comments