-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtweets.h
306 lines (261 loc) · 11 KB
/
tweets.h
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#pragma once
namespace tweets {
inline milliseconds timestamp_ms(const Tweet& tw) {
auto& tweet = tw.data->tweet;
auto t = milliseconds(stoll(tweet["timestamp_ms"].get<string>()));
return t;
}
auto isEndOfTweet = [](const string& s){
if (s.size() < 2) return false;
auto it0 = s.begin() + (s.size() - 2);
auto it1 = s.begin() + (s.size() - 1);
return *it0 == '\r' && *it1 == '\n';
};
struct parseerror {
std::exception_ptr ep;
};
struct parsedtweets {
observable<Tweet> tweets;
observable<parseerror> errors;
};
inline auto parsetweets(observe_on_one_worker worker, observe_on_one_worker tweetthread) -> function<observable<parsedtweets>(observable<string>)> {
return [=](observable<string> chunks) -> observable<parsedtweets> {
return rxs::create<parsedtweets>([=](subscriber<parsedtweets> out){
// create strings split on \r
auto strings = chunks |
concat_map([](const string& s){
auto splits = split(s, "\r\n");
return iterate(move(splits));
}) |
filter([](const string& s){
return !s.empty();
}) |
publish() |
ref_count();
// filter to last string in each line
auto closes = strings |
filter(isEndOfTweet) |
rxo::map([](const string&){return 0;});
// group strings by line
auto linewindows = strings |
window_toggle(closes | start_with(0), [=](int){return closes;});
// reduce the strings for a line into one string
auto lines = linewindows |
flat_map([](const observable<string>& w) {
return w | start_with<string>("") | sum();
});
int count = 0;
rxsub::subject<parseerror> errorconduit;
observable<Tweet> tweets = lines |
filter([](const string& s){
return s.size() > 2 && s.find_first_not_of("\r\n") != string::npos;
}) |
group_by([count](const string&) mutable -> int {
return ++count % std::thread::hardware_concurrency();}) |
rxo::map([=](observable<string> shard) {
return shard |
observe_on(worker) |
rxo::map([=](const string& line) -> observable<Tweet> {
try {
auto tweet = json::parse(line);
return rxs::from(Tweet(tweet));
} catch (...) {
errorconduit.get_subscriber().on_next(parseerror{std::current_exception()});
}
return rxs::empty<Tweet>();
}) |
merge() |
as_dynamic();
}) |
merge(tweetthread) |
tap([](const Tweet&){},[=](){
errorconduit.get_subscriber().on_completed();
}) |
finally([=](){
errorconduit.get_subscriber().unsubscribe();
});
out.on_next(parsedtweets{tweets, errorconduit.get_observable()});
out.on_completed();
return out.get_subscription();
});
};
}
inline auto onlytweets() -> function<observable<Tweet>(observable<Tweet>)> {
return [](observable<Tweet> s){
return s | filter([](const Tweet& tw){
auto& tweet = tw.data->tweet;
return !!tweet.count("timestamp_ms");
});
};
}
enum class errorcodeclass {
Invalid,
TcpRetry,
ErrorRetry,
StatusRetry,
RateLimited
};
inline errorcodeclass errorclassfrom(const http_exception& ex) {
switch(ex.code()) {
case CURLE_COULDNT_RESOLVE_HOST:
case CURLE_COULDNT_CONNECT:
case CURLE_OPERATION_TIMEDOUT:
case CURLE_BAD_CONTENT_ENCODING:
case CURLE_REMOTE_FILE_NOT_FOUND:
return errorcodeclass::ErrorRetry;
case CURLE_GOT_NOTHING:
case CURLE_PARTIAL_FILE:
case CURLE_SEND_ERROR:
case CURLE_RECV_ERROR:
return errorcodeclass::TcpRetry;
default:
if (ex.code() == CURLE_HTTP_RETURNED_ERROR || ex.httpStatus() > 200) {
if (ex.httpStatus() == 420) {
return errorcodeclass::RateLimited;
} else if (ex.httpStatus() == 404 ||
ex.httpStatus() == 406 ||
ex.httpStatus() == 413 ||
ex.httpStatus() == 416) {
return errorcodeclass::Invalid;
}
}
};
return errorcodeclass::StatusRetry;
}
auto filechunks = [](observe_on_one_worker tweetthread, string filepath) {
return observable<>::create<string>([=](subscriber<string> out){
auto values = make_tuple(ifstream{filepath}, string{});
auto state = make_shared<decltype(values)>(move(values));
// creates a worker whose lifetime is the same as this subscription
auto coordinator = tweetthread.create_coordinator(out.get_subscription());
auto controller = coordinator.get_worker();
auto producer = [out, state](const rxsc::schedulable& self) {
if (!out.is_subscribed()) {
// terminate loop
return;
}
if (getline(get<0>(*state), get<1>(*state)))
{
get<1>(*state)+="\r\n";
out.on_next(get<1>(*state));
} else {
out.on_completed();
return;
}
// tail recurse this same action to continue loop
self();
};
controller.schedule(coordinator.act(producer));
});
};
auto twitter_stream_reconnection = [](observe_on_one_worker tweetthread){
return [=](observable<string> chunks){
return chunks |
// https://dev.twitter.com/streaming/overview/connecting
timeout(seconds(90), tweetthread) |
on_error_resume_next([=](std::exception_ptr ep) -> observable<string> {
try {rethrow_exception(ep);}
catch (const http_exception& ex) {
cerr << ex.what() << endl;
switch(errorclassfrom(ex)) {
case errorcodeclass::TcpRetry:
cerr << "reconnecting after TCP error" << endl;
return observable<>::empty<string>();
case errorcodeclass::ErrorRetry:
cerr << "error code (" << ex.code() << ") - ";
case errorcodeclass::StatusRetry:
cerr << "http status (" << ex.httpStatus() << ") - waiting to retry.." << endl;
return observable<>::timer(seconds(5), tweetthread) | stringandignore();
case errorcodeclass::RateLimited:
cerr << "rate limited - waiting to retry.." << endl;
return observable<>::timer(minutes(1), tweetthread) | stringandignore();
case errorcodeclass::Invalid:
cerr << "invalid request - propagate" << endl;
default:
cerr << "unrecognized error - propagate" << endl;
};
}
catch (const timeout_error& ex) {
cerr << "reconnecting after timeout" << endl;
return observable<>::empty<string>();
}
catch (const exception& ex) {
cerr << "unknown exception - terminate" << endl;
cerr << ex.what() << endl;
terminate();
}
catch (...) {
cerr << "unknown exception - not derived from std::exception - terminate" << endl;
terminate();
}
return observable<>::error<string>(ep, tweetthread);
}) |
repeat();
};
};
auto twitterrequest = [](observe_on_one_worker tweetthread, ::rxcurl::rxcurl factory, string URL, string method, string CONS_KEY, string CONS_SEC, string ATOK_KEY, string ATOK_SEC){
return observable<>::defer([=](){
string url;
{
char* signedurl = nullptr;
RXCPP_UNWIND_AUTO([&](){
if (!!signedurl) {
free(signedurl);
}
});
signedurl = oauth_sign_url2(
URL.c_str(), NULL, OA_HMAC, method.c_str(),
CONS_KEY.c_str(), CONS_SEC.c_str(), ATOK_KEY.c_str(), ATOK_SEC.c_str()
);
url = signedurl;
}
cerr << "start twitter stream request" << endl;
return factory.create(http_request{url, method, {}, {}}) |
rxo::map([](http_response r){
return r.body.chunks;
}) |
finally([](){cerr << "end twitter stream request" << endl;}) |
merge(tweetthread);
}) |
twitter_stream_reconnection(tweetthread);
};
auto sentimentrequest = [](observe_on_one_worker worker, ::rxcurl::rxcurl factory, string url, string key, vector<string> text) -> observable<string> {
std::map<string, string> headers;
headers["Content-Type"] = "application/json";
headers["Authorization"] = "Bearer " + key;
auto body = json::parse(R"({"Inputs":{"input1":[]},"GlobalParameters":{}})");
static const regex nonascii(R"([^A-Za-z0-9])");
auto& input1 = body["Inputs"]["input1"];
for(auto& t : text) {
auto ascii = regex_replace(t, nonascii, " ");
input1.push_back({{"tweet_text", ascii}});
}
return observable<>::defer([=]() -> observable<string> {
return factory.create(http_request{url, "POST", headers, body.dump()}) |
rxo::map([](http_response r){
return r.body.complete;
}) |
merge(worker) |
tap([=](exception_ptr){
cout << body << endl;
});
});
};
auto perspectiverequest = [](observe_on_one_worker worker, ::rxcurl::rxcurl factory, string url, string key, string text) -> observable<string> {
std::map<string, string> headers;
headers["Content-Type"] = "application/json";
url += "?key=" + key;
auto body = json::parse(R"({"comment": {"text": ""}, "languages": ["en"], "requestedAttributes": {"TOXICITY":{}, "INFLAMMATORY":{}, "SPAM":{}}, "doNotStore": true })");
body["comment"]["text"] = text;
return observable<>::defer([=]() -> observable<string> {
return factory.create(http_request{url, "POST", headers, body.dump()}) |
rxo::map([](http_response r){
return r.body.complete;
}) |
merge(worker) |
tap([=](exception_ptr){
cout << body << endl;
});
});
};
}