#include <iostream>
#include <fstream>
#include <string>
#include <curl/curl.h>
#include <chrono>
#include <thread>
using namespace std;
// This function is called by libcurl when data is received
static size_t WriteCallback(char* contents, size_t size, size_t nmemb, void* userp) {
((string*)userp)->append(contents, size * nmemb);
return size * nmemb;
}
int main() {
CURL* curl;
CURLcode res;
string readBuffer;
curl_global_init(CURL_GLOBAL_DEFAULT);
while (true) {
// Create a CURL object
curl = curl_easy_init();
if (curl) {
// Set the URL of the API that provides precipitation data
curl_easy_setopt(curl, CURLOPT_URL, "https://api.example.com/precipitation");
// Set the callback function for data received
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
// Set the buffer to store received data
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// Perform the request
res = curl_easy_perform(curl);
// Check for errors
if (res != CURLE_OK) {
cerr << "Error: " << curl_easy_strerror(res) << endl;
}
// Clean up the CURL object
curl_easy_cleanup(curl);
// Write the precipitation value to a CSV file
ofstream outfile;
outfile.open("precipitation.csv", ios_base::app);
outfile << readBuffer << endl;
outfile.close();
// Wait for 30 minutes before making the next request
this_thread::sleep_for(chrono::minutes(30));
}
}
// Clean up the CURL library
curl_global_cleanup();
return 0;
}
Note that this program runs indefinitely and retrieves precipitation data every 30 minutes. The data is appended to a CSV file named "precipitation.csv" in the current directory. You may need to modify the program to suit your specific needs, such as changing the API URL or the name of the output file.