-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
499 lines (406 loc) · 13.4 KB
/
main.cpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//
// C++ implementation of the HNReplies service.
//
// The service accumulates all comments that occur on Hacker News in real-time
// and categorizes them in a directory structure that is convenient for making
// queries about the replies to a certain user. The directory structure is as
// follows:
//
// ./data/
// username0/
// 29977271
// 29977272
// 29977279
// username1/
// 29977276
// 29977283
// some_one/
// 29977276
// 29977283
// .../
//
// The file "./data/$username/$id" contains the JSON obtained from the HN API query:
//
// https://hacker-news.firebaseio.com/v0/item/$id.json
//
// Sample result:
//
// $ cat data/mayoff/2921983
// {
// "by" : "norvig",
// "id" : 2921983,
// "kids" : [ 2922097, 2922429, 2924562, 2922709, 2922573, 2922140, 2922141 ],
// "parent" : 2921506,
// "text" : "Aw shucks, guys ... you make me blush with your compliments.<p>Tell you what, Ill make a deal: I'll keep writing if you keep reading. K?",
// "time" : 1314211127,
// "type" : "comment"
// }
//
// The above example means that item 2921983 is a comment written by user
// "norvig" in reply to a comment or a story by user "mayoff" with id "2921506".
//
// The main advantage of organizing the data in this way is that the items are
// grouped by "username", so it is trivial to query for all replies to a certain
// user.
//
// More info: https://github.com/ggerganov/hnreplies
//
#include "json.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <curl/curl.h>
#include <map>
#include <array>
#include <deque>
#include <string>
#include <chrono>
#include <vector>
#include <thread>
#include <fstream>
#include <filesystem>
// max number of parallel curl requests at a time
#define MAX_PARALLEL 64
//#define DEBUG_SIGPIPE
void sigpipe_handler([[maybe_unused]] int signal) {
#ifdef DEBUG_SIGPIPE
std::ofstream fout("SIGPIPE.OCCURED");
fout << signal << std::endl;
fout.close();
#endif
}
//
// helpers
//
namespace {
uint64_t t_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); // duh ..
}
}
//
// curl functionality
//
namespace curl {
struct Data {
CURL *eh = NULL;
bool running = false;
std::string uri = "";
std::string content = "";
};
CURLM *g_cm;
int g_nFetches = 0;
uint64_t g_totalBytesDownloaded = 0;
std::deque<std::string> g_fetchQueue;
std::map<std::string, std::string> g_fetchCache;
std::array<Data, MAX_PARALLEL> g_fetchData;
size_t writeFunction(void *ptr, size_t size, size_t nmemb, Data* data) {
size_t bytesDownloaded = size*nmemb;
g_totalBytesDownloaded += bytesDownloaded;
data->content.append((char*) ptr, bytesDownloaded);
g_fetchCache[data->uri] = std::move(data->content);
data->content.clear();
return bytesDownloaded;
}
void addTransfer(CURLM *cm, int idx, std::string && uri) {
if (g_fetchData[idx].eh == NULL) {
g_fetchData[idx].eh = curl_easy_init();
}
CURL *eh = g_fetchData[idx].eh;
curl_easy_setopt(eh, CURLOPT_URL, uri.c_str());
curl_easy_setopt(eh, CURLOPT_PRIVATE, &g_fetchData[idx]);
curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(eh, CURLOPT_WRITEDATA, &g_fetchData[idx]);
curl_multi_add_handle(cm, eh);
}
bool init() {
struct sigaction sh;
struct sigaction osh;
sh.sa_handler = &sigpipe_handler; //Can set to SIG_IGN
// Restart interrupted system calls
sh.sa_flags = SA_RESTART;
// Block every signal during the handler
sigemptyset(&sh.sa_mask);
if (sigaction(SIGPIPE, &sh, &osh) < 0) {
return false;
}
curl_global_init(CURL_GLOBAL_ALL);
g_cm = curl_multi_init();
curl_multi_setopt(g_cm, CURLMOPT_MAXCONNECTS, (long)MAX_PARALLEL);
return true;
}
void cleanup() {
curl_multi_cleanup(g_cm);
curl_global_cleanup();
}
// add an URI to the fetch queue
// at some point later, we will be able to obtain the result from this query
void requestJSONForURI(std::string uri) {
g_fetchQueue.push_back(std::move(uri));
}
// get the JSON result from a query that has been requested earlier
// returns empty string if the query has not been processed yet
std::string getJSONForURI(const std::string & uri) {
if (auto it = g_fetchCache.find(uri); it != g_fetchCache.end()) {
auto res = std::move(g_fetchCache[uri]);
g_fetchCache.erase(it);
return res;
}
return "";
}
// curl main loop
// processes any pending queries in the fetch queue
// call this function after making requests with via requestJSONForURI()
void updateRequests() {
CURLMsg *msg;
int msgs_left = -1;
while ((msg = curl_multi_info_read(g_cm, &msgs_left))) {
if (msg->msg == CURLMSG_DONE) {
Data* data;
CURL *e = msg->easy_handle;
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &data);
data->running = false;
curl_multi_remove_handle(g_cm, e);
//curl_easy_cleanup(e);
//data->eh = NULL;
} else {
fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
}
}
int still_alive = 1;
curl_multi_perform(g_cm, &still_alive);
while (still_alive < MAX_PARALLEL && g_fetchQueue.size() > 0) {
int idx = 0;
while (g_fetchData[idx].running) {
++idx;
if (idx == g_fetchData.size()) break;
}
if (idx == g_fetchData.size()) break;
auto uri = std::move(g_fetchQueue.front());
g_fetchQueue.pop_front();
++g_nFetches;
g_fetchData[idx].running = true;
g_fetchData[idx].uri = uri;
addTransfer(g_cm, idx, std::move(uri));
++still_alive;
}
}
}
namespace HN {
using URI = std::string;
using ItemId = int;
using ItemIds = std::vector<ItemId>;
using ItemData = std::map<std::string, std::string>;
// HN API
const URI kAPIItem = "https://hacker-news.firebaseio.com/v0/item/";
const URI kAPIUpdates = "https://hacker-news.firebaseio.com/v0/updates.json";
enum class ItemType : int {
Unknown,
Story,
Comment,
Job,
Poll,
PollOpt,
};
struct Comment {
std::string by;
ItemId id = 0;
ItemIds kids;
ItemId parent = 0;
std::string text;
uint64_t time = 0;
};
std::string getJSONForURI(const std::string & uri, int nRetries, int tRetry_ms) {
auto json = curl::getJSONForURI(uri);
// retry until the query has been processed or we run out of retries
while (json.empty() && nRetries-- > 0) {
curl::updateRequests();
json = curl::getJSONForURI(uri);
std::this_thread::sleep_for(std::chrono::milliseconds(tRetry_ms));
}
return json;
}
URI getItemURI(ItemId id) {
return kAPIItem + std::to_string(id) + ".json";
}
ItemIds getChangedItemsIds() {
auto data = JSON::parseJSONMap(getJSONForURI(HN::kAPIUpdates, 0, 0));
return JSON::parseIntArray(data["items"]);
}
ItemType getItemType(const ItemData & itemData) {
if (itemData.find("type") == itemData.end()) return ItemType::Unknown;
auto strType = itemData.at("type");
if (strType == "story") return ItemType::Story;
if (strType == "comment") return ItemType::Comment;
if (strType == "job") return ItemType::Job;
if (strType == "poll") return ItemType::Poll;
if (strType == "pollopt") return ItemType::PollOpt;
return ItemType::Unknown;
}
void parseComment(const ItemData & data, Comment & res) {
try {
res.by = data.at("by");
} catch (...) {
res.by = "[deleted]";
}
try {
res.id = std::stoi(data.at("id"));
} catch (...) {
res.id = 0;
}
try {
res.kids = JSON::parseIntArray(data.at("kids"));
} catch (...) {
res.kids.clear();
}
try {
res.parent = std::stoi(data.at("parent"));
} catch (...) {
res.parent = 0;
}
try {
//res.text = parseHTML(data.at("text"));
res.text = data.at("text");
} catch (...) {
res.text = "";
}
try {
res.time = std::stoll(data.at("time"));
} catch (...) {
res.time = 0;
}
}
bool same(const ItemIds & ids0, const ItemIds & ids1) {
if (ids0.size() != ids1.size()) return false;
int n = ids0.size();
for (int i = 0; i < n; ++i) {
if (ids0[i] != ids1[i]) return false;
}
return true;
}
}
int main() {
curl::init();
HN::ItemIds idsOld;
HN::ItemIds idsCur;
printf("[I] Connecting to the HN API ..\n");
while (true) {
// query the HN API about which items have been updated
// the API seems to provide updates every 30 seconds
curl::requestJSONForURI(HN::kAPIUpdates);
do {
curl::updateRequests();
std::this_thread::sleep_for(std::chrono::seconds(1));
idsCur = HN::getChangedItemsIds();
} while (idsCur.empty());
if (HN::same(idsCur, idsOld)) {
printf("[I] No new comments since last update -- sleeping ..\n");
std::this_thread::sleep_for(std::chrono::seconds(5));
continue;
}
idsOld = idsCur;
printf("[I] %d items have been updated\n", (int) idsCur.size());
const auto tStart = ::t_ms();
// enque queries to the HN API about the new items
for (const auto & id : idsCur) {
curl::requestJSONForURI(HN::getItemURI(id));
}
// process pending queries
curl::updateRequests();
HN::ItemIds parents;
std::map<HN::ItemId, std::string> by;
std::map<HN::ItemId, std::string> raw;
std::map<HN::ItemId, HN::Comment> comments;
int nComments = 0;
int nOther = 0;
int nUpdated = 0;
int nUnknown = 0;
int nErrors = 0;
for (const auto & id : idsCur) {
// have we already processed this item?
if (by.find(id) != by.end()) continue;
auto & json = raw[id];
json = HN::getJSONForURI(HN::getItemURI(id), 10, 1000);
if (json.empty()) {
++nErrors;
fprintf(stderr, "[E] Failed to get update for item %d\n", id);
continue;
}
const auto data = JSON::parseJSONMap(json);
const auto type = HN::getItemType(data);
try {
by[id] = data.at("by");
} catch (...) {
++nErrors;
fprintf(stderr, "[E] Failed to parse 'by' for item %d\n", id);
continue;
}
// we are only interested in replies, so we check if the item's type is "comment"
switch (type) {
case HN::ItemType::Comment:
{
++nComments;
auto & cur = comments[id];
parseComment(data, cur);
// we are interested who is this comment in reply to
// so we enque a query about the parent for later:
parents.push_back(cur.parent);
curl::requestJSONForURI(HN::getItemURI(cur.parent));
}
break;
default:
{
++nOther;
}
break;
};
}
// iterate all parent items that have been observed in the updated comments
for (const auto & id : parents) {
// we have already processed this item?
if (by.find(id) != by.end()) continue;
const auto json = HN::getJSONForURI(HN::getItemURI(id), 10, 1000);
if (json.empty()) {
++nErrors;
fprintf(stderr, "[E] Failed to get update for item %d\n", id);
continue;
}
const auto data = JSON::parseJSONMap(json);
try {
by[id] = data.at("by");
} catch (...) {
++nErrors;
fprintf(stderr, "[E] Failed to parse 'by' for item %d\n", id);
continue;
}
}
// otput the raw JSON of the updated items in the folders of the corresponding parent
for (const auto & [ id, cur ] : comments) {
if (by.find(cur.parent) == by.end()) {
++nUnknown;
fprintf(stderr, "[E] Parent %d of item %d is unknown\n", cur.parent, id);
continue;
}
++nUpdated;
const std::string pathDir = "./data/" + by[cur.parent];
std::filesystem::create_directories(pathDir);
const std::string pathReply = pathDir + "/" + std::to_string(id);
std::ofstream fout(pathReply);
fout << raw.at(id) << std::endl;
fout.close();
}
const auto tElapsed = ::t_ms() - tStart;
printf("[I] Time: %6lu ms Comments: %3d Updated: %3d Unknown: %3d Errors: %3d Other: %3d | Total requests: %7d (%lu bytes)\n",
tElapsed, nComments, nUpdated, nUnknown, nErrors, nOther, curl::g_nFetches, curl::g_totalBytesDownloaded);
if (tElapsed > 30000) {
fprintf(stderr, "[W] Update took more than 30 seconds - some data might have been missed\n");
}
}
curl::cleanup();
return 0;
}