-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfetchnparse.c
226 lines (190 loc) · 7.24 KB
/
fetchnparse.c
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
#include "fetchnparse.h"
#include "json.h"
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;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if(ptr == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
CURL *curl_handle;
struct MemoryStruct chunk;
int _fetch_url(char *url)
{
CURLcode res;
chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "sparkler-agent/1.0");
/* get it! */
res = curl_easy_perform(curl_handle);
/* check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
return -1;
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*/
printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
}
return 0;
}
void _fetch_cleanup() {
curl_easy_cleanup(curl_handle);
free(chunk.memory);
curl_global_cleanup();
}
json_value *get_value_for_key(json_value *v, char *key) {
for (unsigned int i = 0; i < v->u.object.length; i++) {
if (strcmp(key, v->u.object.values[i].name) == 0)
return v->u.object.values[i].value;
}
return NULL;
}
char *fetch_latest_tweet() {
if (_fetch_url(TWEET_URL) != 0)
return NULL;
json_value *v = json_parse(chunk.memory, chunk.size);
/* Make sure we got a JSON object back */
if (v->type == json_object) {
/* Make sure that "status" is "success" */
json_value *v_status = get_value_for_key(v, "status");
if (v_status == NULL)
goto error_exit;
if (strcmp(v_status->u.string.ptr, "success") != 0)
goto error_exit;
/* Get value of the "text" key inside object pointed to by "tweet" */
json_value *v_tweet = get_value_for_key(v, "tweet");
if (v_tweet == NULL)
goto error_exit;
json_value *v_tweet_text = get_value_for_key(v_tweet, "text");
if (v_tweet_text == NULL)
goto error_exit;
char *tweet_text = malloc(v_tweet_text->u.string.length + 1);
if (!tweet_text)
goto error_exit;
bzero(tweet_text, v_tweet_text->u.string.length + 1);
strcpy(tweet_text, v_tweet_text->u.string.ptr);
json_value_free(v);
_fetch_cleanup();
return tweet_text;
}
/* Gotos are bad, but they're perfect for error handling scenarios */
error_exit:
_fetch_cleanup();
return NULL;
}
char *fetch_air_quality(char *country, char *city) {
char request_url[2048];
snprintf(request_url, sizeof(request_url), "%s?country=%s&city=%s", AIR_QUALITY_URL, country, city);
if (_fetch_url(request_url) != 0)
return NULL;
char *aq_report = malloc(8192);
if (!aq_report)
return NULL;
bzero(aq_report, 8192);
json_value *v = json_parse(chunk.memory, chunk.size);
/* Make sure we got a JSON object back */
if (v->type == json_object) {
/* Make sure that "status" is "success" */
json_value *v_status = get_value_for_key(v, "status");
if (v_status == NULL)
goto error_exit;
if (strcmp(v_status->u.string.ptr, "success") != 0)
goto error_exit;
json_value *v_data = get_value_for_key(v, "data");
if (v_data == NULL)
goto error_exit;
for (unsigned int i = 0; i < v_data->u.array.length; i++) {
char record[1024];
json_value *v_record = v_data->u.array.values[i];
char *v_location = v_record->u.object.values[0].name;
json_value *v_reading = v_record->u.object.values[0].value;
snprintf(record, sizeof(record), "%s: %s\n", v_location, v_reading->u.string.ptr);
strncat(aq_report, record, 8192);
}
_fetch_cleanup();
return aq_report;
}
/* Gotos are bad, but they're perfect for error handling scenarios */
error_exit:
_fetch_cleanup();
free(aq_report);
return NULL;
}
char *fetch_weather(char *city) {
char request_url[2048];
snprintf(request_url, sizeof(request_url), "%s?city=%s", WEATHER_URL, city);
if (_fetch_url(request_url) != 0)
return NULL;
char *weather_forecast = malloc(8192);
if (!weather_forecast)
return NULL;
bzero(weather_forecast, 8192);
json_value *v = json_parse(chunk.memory, chunk.size);
/* Make sure we got a JSON object back */
if (v->type == json_object) {
/* Make sure that "status" is "success" */
json_value *v_status = get_value_for_key(v, "status");
if (v_status == NULL)
goto error_exit;
if (strcmp(v_status->u.string.ptr, "success") != 0)
goto error_exit;
json_value *v_data = get_value_for_key(v, "data");
if (v_data == NULL)
goto error_exit;
json_value *v_weather = get_value_for_key(v_data, "consolidated_weather");
if (v_weather == NULL)
goto error_exit;
for (unsigned int i = 0; i < v_weather->u.array.length; i++) {
char record[1024];
json_value *v_record = v_weather->u.array.values[i];
json_value *v_date = get_value_for_key(v_record, "applicable_date");
json_value *v_weather_state_name = get_value_for_key(v_record, "weather_state_name");
json_value *v_min_temp = get_value_for_key(v_record, "min_temp");
json_value *v_max_temp = get_value_for_key(v_record, "max_temp");
json_value *v_humidity = get_value_for_key(v_record, "humidity");
snprintf(record, sizeof(record), "Date: %s\n\tWeather: %s\n\tMin. temp: %.02f\n\tMax. temp: %.02f\n\tHumidity: %ld\n",
v_date->u.string.ptr,
v_weather_state_name->u.string.ptr,
v_min_temp->u.dbl,
v_max_temp->u.dbl,
v_humidity->u.integer
);
strncat(weather_forecast, record, 8192);
}
_fetch_cleanup();
return weather_forecast;
}
/* Gotos are bad, but they're perfect for error handling scenarios */
error_exit:
_fetch_cleanup();
free(weather_forecast);
return NULL;
}