forked from IpursueI/WeChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetWeather.cpp
More file actions
117 lines (94 loc) · 2.7 KB
/
GetWeather.cpp
File metadata and controls
117 lines (94 loc) · 2.7 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
//g++ -o GetWeather GetWeather.cpp -lcurl -ljsoncpp
#include "GetWeather.h"
#include <jsoncpp/json/json.h>
#include <jsoncpp/json/reader.h>
#include <jsoncpp/json/writer.h>
#include <jsoncpp/json/value.h>
#include <string>
struct url_data {
size_t size;
char *data;
};
size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data){
size_t index = data->size;
size_t n = (size * nmemb);
void *tmp;
data->size +=(size*nmemb);
tmp = realloc((void *)data->data, data->size+1);
if(tmp){
data->data = (char *)tmp;
}else {
free(data->data);
fprintf(stderr, "Failed to allocate memory.h");
return 0;
}
memcpy((data->data+index), ptr, n);
data->data[data->size] = '\0';
return size * nmemb;
}
string parseJson(string strJson){
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(strJson.c_str(), root);
if(!parsingSuccessful){
// std::cout<<"Failed to parse"<<reader.getFormattedErrorMessages();
return 0;
}
string requestRes = root.get("errMsg","A Default Value").asString();
if(requestRes == "success"){
Json::Value detail = root["retData"];
string res = "";
res += "城市:";
res += detail.get("city","unknown").asString();
res += '\n';
res += "时间:";
res += detail.get("date","unknown").asString();
res += '\n';
res += "天气:";
res += detail.get("weather","unknown").asString();
res += '\n';
res += "最低温度:";
res += detail.get("l_tmp","unknown").asString();
res += '\n';
res += "最高温度:";
res += detail.get("h_tmp","unknown").asString();
return res;
}
return "failed";
}
string getWeather(const string &city){
CURL *curl;
CURLcode res;
struct url_data data;
data.size = 0;
data.data = (char *)malloc(4096);
if(NULL == data.data){
fprintf(stderr, "Failed to allocate memory\n");
return NULL;
}
data.data[0] = '\0';
curl = curl_easy_init();
if(curl){
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, "apikey:e6c194f817d5ce9880eedc2a7eb694cc");
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
string getUrl = "http://apis.baidu.com/apistore/weatherservice/cityname?cityname=";
getUrl += city;
curl_easy_setopt(curl, CURLOPT_URL, getUrl.c_str());
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
return parseJson(data.data);
}
}
/*
int main(){
cout<<parseJson(getWeather("深圳"))<<endl;
return 0;
}*/