-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_main.cpp
231 lines (193 loc) · 6.69 KB
/
weather_main.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
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
#include "include/rapidjson/document.h"
#include "include/rapidjson/writer.h"
#include "include/rapidjson/stringbuffer.h"
#include "include/rapidjson/pointer.h"
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <curl/curl.h>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <ctime>
#include <math.h>
#include <stdexcept>
// weather_main.cpp
// This is a C++ version of my Desktop Weather program.
// Original located at https://github.com/kruug/Desktop-Weather-CSharp
using namespace rapidjson;
using namespace std;
string URL_FIRST = "https://api.forecast.io/forecast/";
string API_KEY = "/"; /* Always use the `/` as the last character */
string URL_LOCATION = "44.936905,-91.392935";
string URL = URL_FIRST + API_KEY + URL_LOCATION;
char buffer_zero[80];
char buffer_one[80];
char buffer_two[80];
int date_zero;
int date_one;
int date_two;
double temp_current;
int temp_zero_high;
int temp_one_high;
int temp_two_high;
int temp_zero_low;
int temp_one_low;
int temp_two_low;
time_t rawtime_zero;
time_t rawtime_one;
time_t rawtime_two;
string conditions_current;
string conditions_zero;
string conditions_one;
string conditions_two;
string weather_data;
struct tm * timeinfo_zero;
struct tm * timeinfo_one;
struct tm * timeinfo_two;
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
std::memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
string split_condition(string condition, int line)
{
try {
switch( line ) {
case 1:
return condition.substr(0, 18);
break;
case 2:
return condition.substr(18, 18);
break;
case 3:
return condition.substr(36, 18);
break;
default:
return "";
break;
}
} catch (exception& e) {
return "";
}
}
int main(void)
{
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
Document d;
chunk.memory = (char*)malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
d.Parse(chunk.memory);
if (Value* currently = GetValueByPointer(d, "/currently")) {
// rawtime = (*currently)["time"].GetInt();
temp_current = (*currently)["temperature"].GetDouble();
conditions_current = (*currently)["summary"].GetString();
}
if (Value* day_zero = GetValueByPointer(d, "/daily/data/0")) {
rawtime_zero = (*day_zero)["time"].GetInt();
temp_zero_high = round((*day_zero)["temperatureMax"].GetDouble());
temp_zero_low = round((*day_zero)["temperatureMin"].GetDouble());
conditions_zero = (*day_zero)["summary"].GetString();
}
if (Value* day_one = GetValueByPointer(d, "/daily/data/1")) {
rawtime_one = (*day_one)["time"].GetInt();
temp_one_high = round((*day_one)["temperatureMax"].GetDouble());
temp_one_low = round((*day_one)["temperatureMin"].GetDouble());
conditions_one = (*day_one)["summary"].GetString();
}
if (Value* day_two = GetValueByPointer(d, "/daily/data/2")) {
rawtime_two = (*day_two)["time"].GetInt();
temp_two_high = round((*day_two)["temperatureMax"].GetDouble());
temp_two_low = round((*day_two)["temperatureMin"].GetDouble());
conditions_two = (*day_two)["summary"].GetString();
}
timeinfo_zero = localtime (&rawtime_zero);
strftime(buffer_zero, 80, "%m-%d", timeinfo_zero);
timeinfo_one = localtime (&rawtime_one);
strftime(buffer_one, 80, "%m-%d", timeinfo_one);
timeinfo_two = localtime (&rawtime_two);
strftime(buffer_two, 80, "%m-%d", timeinfo_two);
cout << "----------";
cout << "----------";
cout << "----------";
cout << "----------";
cout << "----------";
cout << "----------";
cout << "----------";
cout << "----------";
cout << "---------" << endl;
cout << "| " << setw(20) << left << "Currently";
cout << "| " << setw(20) << left << "Today";
cout << "| " << setw(20) << left << "Tomorrow";
cout << "| " << setw(20) << left << "Day After Tomorrow";
cout << "|" << endl;
cout << "| " << setw(20) << left << temp_current;
cout << "| " << setw(20) << left << to_string(temp_zero_high) + "/" + to_string(temp_zero_low) + " F";
cout << "| " << setw(20) << left << to_string(temp_one_high) + "/" + to_string(temp_one_low) + " F";
cout << "| " << setw(20) << left << to_string(temp_two_high) + "/" + to_string(temp_two_low) + " F";
cout << "|" << endl;
cout << "| " << setw(20) << left << split_condition(conditions_current, 1);
cout << "| " << setw(20) << left << split_condition(conditions_zero, 1);
cout << "| " << setw(20) << left << split_condition(conditions_one, 1);
cout << "| " << setw(20) << left << split_condition(conditions_two, 1);
cout << "|" << endl;
// (18, 18)
cout << "| " << setw(20) << left << split_condition(conditions_current, 2);
cout << "| " << setw(20) << left << split_condition(conditions_zero, 2);
cout << "| " << setw(20) << left << split_condition(conditions_one, 2);
cout << "| " << setw(20) << left << split_condition(conditions_two, 2);
cout << "|" << endl;
// (36, 18)
cout << "| " << setw(20) << left << split_condition(conditions_current, 3);
cout << "| " << setw(20) << left << split_condition(conditions_zero, 3);
cout << "| " << setw(20) << left << split_condition(conditions_one, 3);
cout << "| " << setw(20) << left << split_condition(conditions_two, 3);
cout << "|" << endl;
cout << "| " << setw(20) << left << "";
cout << "| " << setw(20) << left << buffer_zero;
cout << "| " << setw(20) << left << buffer_one;
cout << "| " << setw(20) << left << buffer_two;
cout << "|" << endl;
cout << "----------";
cout << "----------";
cout << "----------";
cout << "----------";
cout << "----------";
cout << "----------";
cout << "----------";
cout << "----------";
cout << "---------" << endl;
return 0;
}