-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchCurrentWeatherReport.cpp
66 lines (55 loc) · 1.95 KB
/
FetchCurrentWeatherReport.cpp
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
#include <iostream>
#include <thread>
#include <vector>
#include <curl/curl.h>
// Callback function to handle data received from curl
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp) {
size_t totalSize = size * nmemb;
userp->append((char*)contents, totalSize);
return totalSize;
}
// Function to fetch weather data for a given city
void fetchWeather(const std::string& city, const std::string& apiKey) {
CURL* curl;
CURLcode res;
std::string readBuffer;
// Initialize curl
curl = curl_easy_init();
if (curl) {
std::string url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// Perform the request
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
} else {
std::cout << "Weather data for " << city << ":\n" << readBuffer << "\n" << std::endl;
}
// Cleanup
curl_easy_cleanup(curl);
} else {
std::cerr << "Failed to initialize CURL" << std::endl;
}
}
int main() {
// Replace this with your OpenWeatherMap API key
const std::string apiKey = "<YOUR_API_KEY>";
// List of cities
std::vector<std::string> cities = {"New York", "London", "Tokyo", "Sydney", "Mumbai"};
// Vector to hold threads
std::vector<std::thread> threads;
// Launch threads to fetch weather data for each city
for (const auto& city : cities) {
threads.emplace_back(fetchWeather, city, apiKey);
}
// Join all threads
for (auto& t : threads) {
if (t.joinable()) {
t.join();
}
}
std::cout << "All weather data fetched!" << std::endl;
return 0;
}