-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.c
197 lines (161 loc) · 4.43 KB
/
twitter.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
#include "twitter.h"
int
twid_twitter_authenticate(){
PSTREAM pstream_header = (PSTREAM)twid_init_stream_chunk();
PSTREAM pstream_body = (PSTREAM)twid_init_stream_chunk();
CURLcode code = twid_curl_perform(
"https://twitter.com/account/verify_credentials.json",
NULL, 1, pstream_header, pstream_body);
if (code != CURLE_OK){
/* error process */
fprintf(stderr,
"CURL Error[%d] occurred \
while submitting the authentication request, user: %s\n",
code, twid_twitter_user->username);
free(pstream_body);
free(pstream_header);
return 0;
}
/* done calling curl, now deal with the response */
if (twid_twitter_is_auth_valid(pstream_body)){
twid_twitter_cache_sessionid(pstream_header->text);
twid_twitter_get_limits();
}
else{
free(pstream_body);
free(pstream_header);
fprintf(stderr,
"Failed to verify your account: %s\n",
twid_twitter_user->username);
return 0;
}
free(pstream_body);
free(pstream_header);
return 1;
}
void
twid_twitter_end_session(){
CURLcode code = twid_curl_perform(
"API here",
NULL, 0, NULL, NULL );
}
void
twid_twitter_get_limits(){
PSTREAM pstream = (PSTREAM)twid_init_stream_chunk();
CURLcode code = twid_curl_perform(
"https://twitter.com/account/rate_limit_status.json", /* API entry */
NULL, /* NO POST */
0, /* No authentication */
NULL, /* No header callback */
pstream /* Response callback */
);
if (code != CURLE_OK){
fprintf(stderr,
"CURL Error[%d], which causes the failure of fetching rate limits\n",
code
);
return;
}
JsonParser *parser = json_parser_new();
JsonNode *root = (JsonNode *)twid_json_get_root(pstream->text, parser);
if (root == NULL){
fprintf(stderr,
"Failed to retrieve rate limits via API, \
which means your tweet might be omitted if you update so fast :p.\n"
);
}
GValue quota_hourly = {0, }, quota_remaining = {0, };
if (twid_json_get_value_by_name(root, "remaining_hits", "a_remaining) &&
twid_json_get_value_by_name(root, "hourly_limit", "a_hourly)){
if (G_VALUE_HOLDS_INT("a_hourly)){
twid_twitter_user->quota_hourly = g_value_get_int("a_hourly);
}
if (G_VALUE_HOLDS_INT("a_remaining)){
twid_twitter_user->quota_remaining =
g_value_get_int("a_remaining);
}
}
else{
fprintf(stderr,
"Failed to parse API response to get rate limits, \
which means your tweet might be omitted if you update so fast :p.\n"
);
}
free(pstream);
g_object_unref(parser);
}
int
twid_twitter_new_tweet(char *tweet_post_body){
PSTREAM pstream_body = (PSTREAM)twid_init_stream_chunk();
CURLcode code = twid_curl_perform(
"https://twitter.com/statuses/update.json", /* API entry */
tweet_post_body, /* POST body */
1, /* No authentication */
NULL, /* No header callback */
pstream_body /* Response callback */
);
if (code != CURLE_OK){
/* error process here */
syslog(LOG_ERR, "CURL ERROR while publishing a new tweet");
free(pstream_body);
return 0;
}
free(pstream_body);
return 1;
}
/* functions below for parsing JSON packets */
int
twid_twitter_is_auth_valid(PSTREAM chunk){
JsonParser *parser = json_parser_new();
JsonNode *root = (JsonNode *)twid_json_get_root(chunk->text, parser);
if (root == NULL){
return 0;
}
GValue gv_tmp = {0, };
if (twid_json_get_value_by_name(root, "error", &gv_tmp)){
json_node_free(root);
g_object_unref(parser);
return 0;
}
else{
/* some methods are omitted here */
json_node_free(root);
g_object_unref(parser);
return 1;
}
}
void
twid_twitter_cache_sessionid(char *buffer){
if (buffer){
int matches, i;
int ovector[30];
const char *error;
int erroffset;
pcre *re = pcre_compile(
"Set-Cookie: (_twitter_sess=[^;]+;).+",
0,
&error,
&erroffset,
NULL
);
matches = pcre_exec(re, NULL,
buffer, strlen(buffer), 0, 0, ovector, 30);
if (matches < 0){
if (matches == PCRE_ERROR_NOMATCH){
fprintf(stderr, "Couldn't match Set-Cookie in HTTP header.\n");
}
else{
fprintf(stderr, "Failed to execute regex.\n");
}
}
if (matches == 2){
/* $1 for the session id */
i = 1;
char *session_start = buffer + ovector[i * 2];
int session_len = ovector[i * 2 + 1] - ovector[i * 2];
twid_twitter_user->sessionid = (char *)malloc(session_len + 1);
/* copy session id to the user structure */
strncpy(twid_twitter_user->sessionid, session_start, session_len);
}
}
}