-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtwitter_batch.js
229 lines (206 loc) · 8.29 KB
/
twitter_batch.js
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
// Generated by IcedCoffeeScript 108.0.11
(function() {
var BaseBearerToken, BaseScraper, Lock, TweetCache, TwitterBatchScraper, TwitterScraper, constants, decode_sig, iced, make_ids, schema, sncmp, urlmod, v_codes, ws_normalize, __iced_k, __iced_k_noop, _ref,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
iced = require('iced-runtime');
__iced_k = __iced_k_noop = function() {};
_ref = require('./base'), sncmp = _ref.sncmp, BaseScraper = _ref.BaseScraper, BaseBearerToken = _ref.BaseBearerToken;
TwitterScraper = require('./twitter').TwitterScraper;
make_ids = require('../base').make_ids;
constants = require('../constants').constants;
v_codes = constants.v_codes;
decode_sig = require('kbpgp').ukm.decode_sig;
Lock = require('../util').Lock;
urlmod = require('url');
schema = require('../schema3');
ws_normalize = function(x) {
var v;
v = x.split(/[\t\r\n ]+/);
if (v.length && v[0].length === 0) {
v.shift();
}
if (v.length && v.slice(-1)[0].length === 0) {
v.pop();
}
return v.join(' ');
};
exports.TwitterBatchScraper = TwitterBatchScraper = (function(_super) {
__extends(TwitterBatchScraper, _super);
function TwitterBatchScraper(opts) {
this._tweet_cache = opts.tweet_cache;
this.cache_refresh_interval = opts.cache_refresh_interval;
TwitterBatchScraper.__super__.constructor.call(this, opts);
}
TwitterBatchScraper.prototype._hunt_batch = function(cb) {
var created_at, err, i, id, json, query, rc, since_id, text, u, username, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
query = {
query: "\"Verifying myself\" \"Keybase.io\"",
expansions: "author_screen_name",
"user.fields": "url,username",
"tweet.fields": "created_at",
max_results: 60
};
if (since_id = this._tweet_cache.last_id) {
query.since_id = since_id;
}
u = urlmod.format({
host: "api.twitter.com",
protocol: "https:",
pathname: "/2/tweets/search/recent",
query: query
});
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/michal/SourceCode/keybase/go/src/github.com/keybase/server_test_progs/proofs/src/scrapers/twitter_batch.iced",
funcname: "TwitterBatchScraper._hunt_batch"
});
_this._get_body_api({
url: u
}, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
err = arguments[0];
rc = arguments[1];
return json = arguments[2];
};
})(),
lineno: 44
}));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
var _i, _len, _ref1, _ref2;
_this.log("| search index " + u + " -> " + rc);
if (rc !== v_codes.OK) {
} else if ((typeof json === "undefined" || json === null) || (json.length === 0)) {
rc = v_codes.EMPTY_JSON;
} else if (json.data == null) {
rc = v_codes.INVALID_JSON;
} else {
console.log(json.data);
_ref1 = json.data;
for (i = _i = 0, _len = _ref1.length; _i < _len; i = ++_i) {
_ref2 = _ref1[i], id = _ref2.id, created_at = _ref2.created_at, username = _ref2.username, text = _ref2.text;
created_at = new Date(created_at);
if (!isFinite(created_at)) {
_this.log("got invalid date in tweet JSON id: " + id + ", created_at: " + tweet.created_at);
continue;
}
_this.log("ingesting tweet: id: " + id + ", username: " + username + ", text: \"" + text + "\"");
_this._tweet_cache.inform({
id: id,
created_at: created_at,
username: username,
text: text
});
}
}
return cb(null, v_codes.OK);
};
})(this));
};
TwitterBatchScraper.prototype.hunt2 = function(_arg, cb) {
var api_url, current_tweet, err, human_url, name, now, out, proof_text_check, rc, remote_id, username, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
username = _arg.username, name = _arg.name, proof_text_check = _arg.proof_text_check;
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/michal/SourceCode/keybase/go/src/github.com/keybase/server_test_progs/proofs/src/scrapers/twitter_batch.iced",
funcname: "TwitterBatchScraper.hunt2"
});
_this._tweet_cache.lock.acquire(__iced_deferrals.defer({
lineno: 63
}));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
err = null;
now = Math.floor(Date.now() / 1000);
(function(__iced_k) {
if (now - _this._tweet_cache.fetched_at > _this.cache_refresh_interval) {
_this._tweet_cache.fetched_at = now;
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/michal/SourceCode/keybase/go/src/github.com/keybase/server_test_progs/proofs/src/scrapers/twitter_batch.iced",
funcname: "TwitterBatchScraper.hunt2"
});
_this._hunt_batch(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
err = arguments[0];
return rc = arguments[1];
};
})(),
lineno: 68
}));
__iced_deferrals._fulfill();
})(function() {
return __iced_k(!err && rc !== v_codes.OK ? err = new Error("rc: " + rc) : void 0);
});
} else {
return __iced_k();
}
})(function() {
_this._tweet_cache.lock.release();
if (err) {
_this.logl("error", "error when hunting batch: " + (err.toString()));
return cb(err);
}
out = {};
rc = v_codes.NOT_FOUND;
current_tweet = _this._tweet_cache.tweets.get(username);
if (current_tweet && (_this.find_sig_in_tweet({
inside: current_tweet.text,
proof_text_check: proof_text_check
})) === v_codes.OK) {
rc = v_codes.OK;
remote_id = current_tweet.id;
api_url = human_url = _this._id_to_url(username, remote_id);
out = {
remote_id: remote_id,
api_url: api_url,
human_url: human_url
};
}
out.rc = rc;
return cb(err, out);
});
};
})(this));
};
return TwitterBatchScraper;
})(TwitterScraper);
exports.TweetCache = TweetCache = (function() {
function TweetCache() {
this.tweets = new Map();
this.last_id = null;
this.fetched_at = 0;
this.lock = new Lock();
}
TweetCache.prototype.inform = function(_arg) {
var created_at, current, id, text, username;
id = _arg.id, created_at = _arg.created_at, username = _arg.username, text = _arg.text;
current = this.tweets.get(username);
if (current && current.created_at >= created_at) {
return;
}
return this.tweets.set(username, {
id: id,
created_at: created_at,
text: text
});
};
return TweetCache;
})();
}).call(this);