-
Notifications
You must be signed in to change notification settings - Fork 3
/
plurc.c
224 lines (209 loc) · 5.3 KB
/
plurc.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
/*
plurc or gtfo
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "libplurc.h"
#include "json.h"
#include "config.h" /* configuration file */
int main(int argc, char **argv)
{
int rc = 0;
PLURK *ph;
ph = plurk_open(PLURK_KEY);
JSON_OBJ *jo, *jo2;
long long int friends, favorer;
const char *privacy;
double karma;
const JSON_OBJ *user_info, *plurk;
const JSON_ARRAY *plurk_array, *favorers;
const char *plurk_msg, *nickname;
if (argc==3) {
if (!strcmp(argv[1], "add")) {
/*
example of adding new Plurks
*/
printf("You are gonna add new plurk with content:\n %s\n", argv[2]);
if (plurk_login(ph, PLURK_USERNAME, PLURK_PASSWORD)) {
fprintf(stderr, "Plurk login error\n");
rc = -1;
goto out;
}
if (!plurk_add(ph, argv[2], "says")) {
jo = json_create_obj(ph->body);
if (jo) {
json_print_obj(jo, 0);
json_free_obj(jo);
}
} else {
fprintf(stderr, "Plurk add error\n");
rc = -1;
goto out;
}
if (!plurk_logout(ph)) {
jo = json_create_obj(ph->body);
if (jo) {
json_print_obj(jo, 0);
json_free_obj(jo);
}
} else {
fprintf(stderr, "Plurk logout error\n");
rc = -1;
goto out;
}
} else if (!strcmp(argv[1], "resps_get")) {
/*
exeample of fetching responses
*/
printf("You wanna plurk responses %s\n", argv[2]);
if (!plurk_resps_get(ph, argv[2], "0")) {
jo = json_create_obj(ph->body);
if (jo) {
json_print_obj(jo, 0);
json_free_obj(jo);
}
} else {
fprintf(stderr, "Plurk get response error\n");
rc = -1;
goto out;
}
} else if (!strcmp(argv[1], "pprofile_get")) {
/*
example of fetching public profile
*/
printf("You wanna public profile get for %s\n", argv[2]);
if (!plurk_pprofile_get(ph, argv[2])) {
jo = json_create_obj(ph->body);
if (!jo)
goto out;
json_print_obj(jo, 0);
if (!json_get_integer(jo, &friends, "friends_count"))
printf("Have %lld friends\n", friends);
if (!json_get_string(jo, &privacy, "privacy"))
printf("Plurk privacy setting is: \"%s\"\n", privacy);
if (!json_get_object(jo, &user_info, "user_info") &&
!json_get_floating(user_info, &karma, "karma")) {
printf("Karma is: %lf\n", karma);
}
if (json_get_array(jo, &plurk_array, "plurks")) {
json_free_obj(jo);
goto out;
}
printf("Recent plurks:\n");
int i;
json_array_foreach_object(plurk_array, &plurk, i) {
if (!json_get_string(plurk, &plurk_msg, "content_raw"))
printf("\t%s\n", plurk_msg);
if (json_get_array(plurk, &favorers, "favorers")) {
printf("\n");
continue;
}
int j;
printf("\tFavorers:");
json_array_foreach_integer(favorers, &favorer, j) {
printf(" %lld", favorer);
if (plurk_pprofile_get_byint(ph, favorer))
continue;
jo2 = json_create_obj(ph->body);
if (!jo2)
continue;
if (json_get_object(jo2, &user_info, "user_info"))
continue;
if (json_get_string(user_info, &nickname, "nick_name"))
continue;
printf("(%s)", nickname);
}
printf("\n\n");
}
json_free_obj(jo);
} else {
fprintf(stderr, "Plurk get response error\n");
rc = -1;
goto out;
}
}
} else if (argc==4) {
if (!strcmp(argv[1], "resps_radd")) {
/*
example of adding responses to plurk
*/
printf("You wanna add response for plurk %s\n", argv[2]);
if (plurk_login(ph, PLURK_USERNAME, PLURK_PASSWORD)) {
fprintf(stderr, "Plurk login error\n");
rc = -1;
goto out;
}
if (!plurk_resps_radd(ph, argv[2], argv[3], "says")) {
jo = json_create_obj(ph->body);
if (jo) {
json_print_obj(jo, 0);
json_free_obj(jo);
}
} else {
fprintf(stderr, "Plurk add error\n");
rc = -1;
goto out;
}
if (!plurk_logout(ph)) {
jo = json_create_obj(ph->body);
if (jo) {
json_print_obj(jo, 0);
json_free_obj(jo);
}
} else {
fprintf(stderr, "Plurk logout error\n");
rc = -1;
goto out;
}
}
} else if (argc==2) {
if (!strcmp(argv[1], "oprofile_get")) {
/*
example of fetching own profile data
*/
printf("You wanna your own profile\n");
if (plurk_login(ph, PLURK_USERNAME, PLURK_PASSWORD)) {
fprintf(stderr, "Plurk login error\n");
rc = -1;
goto out;
}
if (!plurk_oprofile_get(ph)) {
jo = json_create_obj(ph->body);
if (jo) {
json_print_obj(jo, 0);
if (!json_get_integer(jo, &friends, "friends_count"))
printf("Have %lld friends\n", friends);
if (!json_get_string(jo, &privacy, "privacy"))
printf("Plurk privacy setting is: \"%s\"\n", privacy);
json_free_obj(jo);
}
} else {
fprintf(stderr, "Plurk add error\n");
rc = -1;
goto out;
}
if (!plurk_logout(ph)) {
jo = json_create_obj(ph->body);
if (jo) {
json_print_obj(jo, 0);
json_free_obj(jo);
}
} else {
fprintf(stderr, "Plurk logout error\n");
rc = -1;
goto out;
}
};
} else {
printf("Only supported uses now are: \n");
printf(" %s add message\n", argv[0]);
printf(" %s resps_get plurk_id\n", argv[0]);
printf(" %s pprofile_get user_id\n", argv[0]);
printf(" %s oprofile_get\n", argv[0]);
printf(" %s resps_radd plurk_id response\n", argv[0]);
}
out:
plurk_close(ph); /* call it to make some cleanups */
return rc;
}