-
Notifications
You must be signed in to change notification settings - Fork 1
/
reddit_client.c
205 lines (170 loc) · 4.07 KB
/
reddit_client.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
#include <u.h>
#include <libc.h>
#include <libString.h>
/* Undefine p9 aliases as it conflicts with CURL */
#undef listen
#undef accept
#include <curl/curl.h>
#include <jansson.h>
#include "reddit_client.h"
/* Constants */
#define URL_FORMAT "http://reddit.com/r/%s/.json"
#define HTTP_OK 200
/* Local funcs */
static void seterror(Error*, char*, ...);
static Post* getpost(json_t*, Error*);
static char* httpget(const char*, Error*);
/* Retrieve list of posts from given subreddit */
Post**
getposts(const char* subreddit, Error* error)
{
json_error_t err;
json_t *root, *data, *children, *child, *node;
Post **result = nil;
size_t size;
size_t index;
char *url;
if(subreddit == nil || strlen(subreddit) == 0) {
seterror(error, "subreddit name cannot be null or empty");
return nil;
}
url = smprint(URL_FORMAT, subreddit);
char* buffer = httpget(url, error);
if(buffer == nil) {
return nil;
}
root = json_loads(buffer, 0, &err);
free(buffer);
if(!root) {
seterror(error, "error: on line %d: %s", err.line, err.text);
return nil;
}
data = json_object_get(root, "data");
if(!json_is_object(data)) {
seterror(error, "unable to get data node");
goto error;
}
children = json_object_get(data, "children");
if(!json_is_array(children)) {
seterror(error, "unable to get children list");
goto error;
}
size = json_array_size(children);
result = calloc(size + 1, sizeof(Post*));
if(result == nil) {
seterror(error, "unable to allocate memory for result list");
goto error;
}
json_array_foreach(children, index, child) {
node = json_object_get(child, "data");
if(!json_is_object(node)) {
seterror(error, "unable to get child data");
goto error;
}
Post* post = getpost(node, error);
if(post == nil) {
goto error;
}
result[index] = post;
}
result[size] = nil;
json_decref(root);
return result;
error:
if(result)
free(result);
json_decref(root);
return nil;
}
static
Post*
getpost(json_t *node, Error* error)
{
json_t *title, *url, *score;
Post* post = nil;
title = json_object_get(node, "title");
if(!json_is_string(title)) {
seterror(error, "title has invalid element type");
return nil;
}
url = json_object_get(node, "url");
if(!json_is_string(url)) {
seterror(error, "url has invalid element type");
return nil;
}
score = json_object_get(node, "score");
if(!json_is_integer(score)) {
seterror(error, "score has invalid element type");
return nil;
}
post = malloc(sizeof(Post));
if(post == nil) {
seterror(error, "out of memory");
return nil;
}
post->title = strdup((char*)json_string_value(title));
post->url = strdup((char*)json_string_value(url));
post->score = (long)json_integer_value(score);
return post;
}
static
size_t
writecallback(void* contents, size_t size, size_t nmemb, void* data)
{
String* buffer;
size_t sz;
buffer = data;
sz = nmemb * size;
s_memappend(buffer, contents, sz);
return sz;
}
static
char*
httpget(const char* url, Error* error)
{
CURL* curl = nil;
CURLcode rc;
long code;
char* data = nil;
String *buffer;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(!curl)
return nil;
buffer = s_newalloc(4096);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buffer);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
rc = curl_easy_perform(curl);
if(rc != 0) {
seterror(error, "unable to request '%s' (%s)", url, curl_easy_strerror(rc));
goto cleanup;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code != HTTP_OK) {
seterror(error, "server responded with code %ld", code);
goto cleanup;
}
data = strdup(s_to_c(buffer));
s_free(buffer);
cleanup:
curl_easy_cleanup(curl);
curl_global_cleanup();
return data;
}
static
void
seterror(Error* error, char* fmt, ...)
{
if(error == nil)
return;
va_list ap;
va_start(ap, fmt);
error->message = vsmprint(fmt, ap);
if(error->message == nil) {
fprint(2, "fatal: unable to allocate memory for error message\n");
exits("oom");
}
va_end(ap);
}