-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
506 lines (429 loc) · 18.6 KB
/
main.cpp
File metadata and controls
506 lines (429 loc) · 18.6 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include <iostream>
#include <curl/curl.h>
#include <string>
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include <map>
using namespace rapidjson;
using namespace std;
//weather api {https://openweathermap.org/forecast5}
string getWeatherIcon(const string& weatherStatus);
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output);
const Value& geocoding(string cityName);
string getWDataByLatLon(const string& apiKey);
string getWDataByCity(const string& apiKey);
string getWDataByZipCode(const string& apiKey);
void getFDataByCity(const string& apiKey);
void getAirQuality(const string& apiKey);
double KelvinToCelsius(double kel);
void displayWeatherInfo(const string& weatherData);
void displayAirPollution(const string &airPollution, string cityName);
void displayWeatherForecast(const string& weatherData);
//curl 사용 시에 나오는 json을 callback 시켜주는 함수
size_t WriteCallback(void* contents, size_t size, size_t nmemb, string* output) {
size_t totalSize = size * nmemb;
output->append((char*)contents, totalSize);
return totalSize;
}
// 날씨 상태에 해당하는 아이콘 반환
string getWeatherIcon(const string& weatherStatus) {
map<string, string> weatherIcons;
weatherIcons.insert(make_pair("Clear", "☀️"));
weatherIcons.insert(make_pair("Clouds", "☁️"));
weatherIcons.insert(make_pair("Rain", "🌧️"));
weatherIcons.insert(make_pair("Drizzle", "🌦️"));
weatherIcons.insert(make_pair("Snow", "❄️"));
weatherIcons.insert(make_pair("Thunderstorm", "⛈️"));
weatherIcons.insert(make_pair("Mist", "🌫️"));
weatherIcons.insert(make_pair("Smoke", "🌫️"));
weatherIcons.insert(make_pair("Haze", "🌫️"));
weatherIcons.insert(make_pair("Dust", "🌫️"));
weatherIcons.insert(make_pair("Fog", "🌫️"));
weatherIcons.insert(make_pair("Sand", "🌫️"));
weatherIcons.insert(make_pair("Ash", "🌫️"));
weatherIcons.insert(make_pair("Squall", "🌫️"));
weatherIcons.insert(make_pair("Tornado", "🌪️"));
if (weatherIcons.count(weatherStatus) > 0) {
return weatherIcons[weatherStatus];
}
return "";
}
const Value& geocoding(string cityName){
//Mozilla5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
// Nominatim API URL 생성
string apiUrl = "https://nominatim.openstreetmap.org/search?q=" + cityName + "&format=json&limit=1";
// HTTP 요청 초기화
CURL* curl = curl_easy_init();
if (curl){
// HTTP 요청 설정
curl_easy_setopt(curl, CURLOPT_URL, apiUrl.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
// User-Agent 설정
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
// HTTP 요청 수행
string latlonjson;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &latlonjson);
CURLcode result = curl_easy_perform(curl);
// HTTP 요청 결과 처리
if (result == CURLE_OK){
// JSON 데이터 파싱
Document document;
document.Parse(latlonjson.c_str());
// 결과 확인
if (!document.Empty()){
if (document.IsArray()){
const Value& result = document[0];
if (result.HasMember("lat") && result.HasMember("lon")){
if (result.HasMember("licence") && result["licence"].IsString()) {
const std::string& licence = result["licence"].GetString();
cout << "------------------------" << endl;
cout << "Licence: " << licence << endl;
}
return result;
}
else{
cout << "도시의 위도와 경도를 찾을 수 없습니다." << endl;
}
}
else{
cout << "잘못된 JSON 형식 - 배열이 필요합니다." << endl;
}
}
else{
cout << "도시에 대한 검색 결과가 없습니다." << endl;
}
}
else{
cout << "HTTP 요청을 수행하지 못했습니다: " << curl_easy_strerror(result) << endl;
}
// CURL 자원 정리
curl_easy_cleanup(curl);
}
else{
cout << "CURL을 초기화하지 못했습니다." << endl;
}
// 반환되는 값이 없는 경우에 대한 처리
static Value emptyResult;
return emptyResult;
}
//latitude, longitude(위도, 경도)로 weatherData 받아오는 함수
string getWDataByLatLon(const string& apiKey){
string lat, lon;
cout << "위도를 입력하세요: ";
getline(cin, lat);
cout << "경도를 입력하세요: ";
getline(cin, lon);
CURL *curl = curl_easy_init();
string url = "https://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid="+apiKey;
string weatherData;
if(curl){
// URL
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &weatherData);
// METHOD
// DEFAULT : GET
// EXECUTE
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res != CURLE_OK){
cout << "날씨 정보를 불러오는 도중에 문제 발생:" << curl_easy_strerror(res) << endl;
}
}
return weatherData;
}
// city 정보로 weatherData 받아오는 함수
string getWDataByCity(const string& apiKey){
string city;
cout << "도시 이름을 영어로 입력하세요: ";
getline(cin, city);
CURL *curl = curl_easy_init();
string url = "https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid="+apiKey;
string weatherData;
if(curl){
// URL
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &weatherData);
// METHOD
// DEFAULT : GET
// EXECUTE
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res != CURLE_OK){
cout << "날씨 정보를 불러오는 도중에 문제 발생:" << curl_easy_strerror(res) << endl;
}
}
return weatherData;
}
// zipcode(우편 번호) 정보로 weatherData 받아오는 함수
string getWDataByZipCode(const string& apiKey){
string zip;
cout << "우편 번호를 입력하세요: ";
getline(cin, zip);
CURL *curl = curl_easy_init();
string url = "https://api.openweathermap.org/data/2.5/weather?zip="+zip+"&appid="+apiKey;
string weatherData;
if(curl){
// URL
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &weatherData);
// METHOD
// DEFAULT : GET
// EXECUTE
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res != CURLE_OK){
cout << "날씨 정보를 불러오는 도중에 문제 발생:" << curl_easy_strerror(res) << endl;
}
}
return weatherData;
}
//위도 경도 정보를 가지고 미세먼지 정보 확인
void getAirQuality(const string& apiKey) {
CURL* curl;
string airPollution;
string city;
cout << "도시 이름을 영어로 입력하세요: ";
getline(cin, city);
const Value &latLonList = geocoding(city);
string lat = latLonList["lat"].GetString();
string lon = latLonList["lon"].GetString();
// OpenWeatherMap API 요청 URL (API 키를 포함해야 합니다)
string url = "http://api.openweathermap.org/data/2.5/air_pollution?lat=" + lat + "&lon=" + lon + "&appid=" + apiKey;
// cURL 초기화
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl){
// URL
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &airPollution);
// METHOD
// DEFAULT : GET
// EXECUTE
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res != CURLE_OK){
cout << "날씨 정보를 불러오는 도중에 문제 발생:" << curl_easy_strerror(res) << endl;
}
}
displayAirPollution(airPollution, city);
}
//Kelvin 온도를 Celsius 온도로 변환
double KelvinToCelsius(double kel){
double cel;
cel = kel - 273.15;
return cel;
}
void displayWeatherInfo(const string& weatherData){
// JSON 파싱
Document data;
data.Parse(weatherData.c_str());
if(!data.HasParseError()) { // 파싱 오류 체크
if(data.IsObject()) { // JSON 문서가 객체 형식인지 확인
cout << "------------------------" << endl;
if(data.HasMember("name") && data["name"].IsString()){
string cityName = data["name"].GetString();
cout << "도시명: " << cityName << endl;
}
if(data.HasMember("main") && data["main"].IsObject()){
Value& mainObj = data["main"];
if (mainObj.HasMember("temp") && mainObj["temp"].IsNumber()) {
double temperature = mainObj["temp"].GetDouble();
cout << "온도: " << KelvinToCelsius(temperature) << "°C" << endl;
}
if (mainObj.HasMember("humidity") && mainObj["humidity"].IsNumber()) {
int humidity = mainObj["humidity"].GetInt();
cout << "습도: " << humidity << "%" << endl;
}
}
if(data.HasMember("wind") && data["wind"].IsObject()){
Value& windObj = data["wind"];
if (windObj.HasMember("speed") && windObj["speed"].IsNumber()) {
double windSpeed = windObj["speed"].GetDouble();
cout << "풍속: " << windSpeed << " m/s" << endl;
}
}
if(data.HasMember("weather") && data["weather"].IsArray()) {
Value &weatherArray = data["weather"];
if (!weatherArray.Empty()) {
const Value &firstWeatherObject = weatherArray[0];
if (firstWeatherObject.HasMember("main") && firstWeatherObject["main"].IsString()) {
string weatherStatus = firstWeatherObject["main"].GetString();
// 이제 mainValue 변수에 "main" 필드의 값이 저장됩니다.
string weatherIcon = getWeatherIcon(weatherStatus);
cout << "날씨 상태: " << weatherStatus << " " << weatherIcon << endl;
}
}
}
}else {
cout << "잘못된 JSON 형식" << endl;
}
}else {
cout << "날씨 데이터를 분석하는 동안 오류가 발생했습니다." << endl;
}
}
// 5일 동안의 일기 예보 정보 출력 (15시 기준)
void displayWeatherForecast(const string& weatherData) {
// JSON 파싱
Document data;
data.Parse(weatherData.c_str());
if (!data.HasParseError()) { // 파싱 오류 체크
if (data.IsObject()) { // JSON 문서가 객체 형식인지 확인
if (data.HasMember("list") && data["list"].IsArray()) {
Value& forecastArray = data["list"];
if (!forecastArray.Empty()) {
cout << "------------------------" << endl;
cout << "5일 동안의 15시 기준 일기 예보:" << endl;
for (SizeType i = 0; i < forecastArray.Size(); i++) {
const Value& forecastObj = forecastArray[i];
if (forecastObj.HasMember("dt_txt") && forecastObj["dt_txt"].IsString()) {
string forecastDateTime = forecastObj["dt_txt"].GetString();
// 시간이 15시일 때만 출력
if (forecastDateTime.substr(11, 2) == "15") {
cout << "------------------------" << endl;
cout << "일시: " << forecastDateTime << endl;
if (forecastObj.HasMember("main") && forecastObj["main"].IsObject()) {
const Value& mainObj = forecastObj["main"];
if (mainObj.HasMember("temp") && mainObj["temp"].IsNumber()) {
double temperature = mainObj["temp"].GetDouble();
cout << "온도: " << KelvinToCelsius(temperature) << "°C" << endl;
}
if (mainObj.HasMember("humidity") && mainObj["humidity"].IsNumber()) {
int humidity = mainObj["humidity"].GetInt();
cout << "습도: " << humidity << "%" << endl;
}
}
if (forecastObj.HasMember("weather") && forecastObj["weather"].IsArray()) {
const Value& weatherArray = forecastObj["weather"];
if (!weatherArray.Empty()) {
const Value& firstWeatherObject = weatherArray[0];
if (firstWeatherObject.HasMember("main") && firstWeatherObject["main"].IsString()) {
string weatherStatus = firstWeatherObject["main"].GetString();
// 이제 mainValue 변수에 "main" 필드의 값이 저장됩니다.
string weatherIcon = getWeatherIcon(weatherStatus);
cout << "날씨 상태: " << weatherStatus << " " << weatherIcon << endl;
}
}
}
}
}
}
}
}
}
else {
cout << "잘못된 JSON 형식" << endl;
}
}
else {
cout << "날씨 데이터를 분석하는 동안 오류가 발생했습니다." << endl;
}
}
void displayAirPollution(const string &airPollution, string cityName){
// JSON 파싱
Document data;
data.Parse(airPollution.c_str());
if(!data.HasParseError()) { // 파싱 오류 체크
if(data.IsObject()) { // JSON 문서가 객체 형식인지 확인
cout << "------------------------" << endl;
cout << "도시 이름: " << cityName << endl;
if(data.HasMember("list") && data["list"].IsArray()){
Value& airPollutionArray = data["list"];
if (!airPollutionArray.Empty()) {
// 미세먼지 농도 추출
const Value& airQuality = airPollutionArray[0]["components"];
int pm25 = airQuality["pm2_5"].GetDouble();
int pm10 = airQuality["pm10"].GetDouble();
// 미세먼지 정보 출력
cout << "미세먼지 : " << pm10 << " μg/m^3" << endl;
cout << "초미세먼지 : " << pm25 << " μg/m^3" << endl;
}
}
}
else {
cout << "잘못된 JSON 형식" << endl;
}
}
else {
std::cout << "미세먼지 데이터를 분석하는 동안 오류가 발생했습니다." << std::endl;
}
}
// city(도시 이름) 정보로 forecastData 받아오는 함수
void getFDataByCity(const string& apiKey){
string city;
cout << "도시 이름을 영어로 입력하세요: ";
getline(cin, city);
CURL *curl = curl_easy_init();
string url = "https://api.openweathermap.org/data/2.5/forecast?q="+city+"&appid="+apiKey;
string forecastData;
if(curl){
// URL
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &forecastData);
// METHOD
// DEFAULT : GET
// EXECUTE
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res != CURLE_OK){
cout << "날씨 정보를 불러오는 도중에 문제 발생:" << curl_easy_strerror(res) << endl;
}
}
displayWeatherForecast(forecastData);
}
int main() {
string apiKey = "b1e668504d0535c4e1cc989cdcb31fa2";
string weatherData;
int choice, method, break_point=0;
//1. 위도 경도 2. 도시 이름 3. 우편 번호
while(true){
int curWeather=1;
cout << "------------------------" << endl;
cout << "어느 정보를 검색하시겠습니까?" << endl;
cout << "1. 현재 날씨 2. 일기 예보 3. 미세 먼지 4. 종료" << endl;
cout << "해당하는 숫자를 입력하세요: ";
cin >> choice;
getchar();
switch (choice) {
case 1:
cout << "------------------------" << endl;
cout << "검색 방식을 선택합니다." << endl;
cout << "1. 위도 경도 2. 도시 이름 3. 우편 번호" << endl;
cout << "해당하는 숫자를 입력하세요 : ";
cin >> method;
getchar();
// 현재 날씨
switch (method) {
case 1:
weatherData = getWDataByLatLon(apiKey);
break;
case 2:
weatherData = getWDataByCity(apiKey);
break;
case 3:
weatherData = getWDataByZipCode(apiKey);
break;
}
displayWeatherInfo(weatherData);
break;
case 2:
//일기 예보
getFDataByCity(apiKey);
break;
case 3:
//미세 먼지
getAirQuality(apiKey);
break;
case 4:
break_point=1;
break;
}
if(break_point) break;
}
return 0;
}