-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet-searcher.js
51 lines (44 loc) · 1.18 KB
/
tweet-searcher.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
var twitter = require('twitter');
var db = require('./db');
var config = require('./config');
var client = new twitter({
consumer_key: config.twitter_consumer_key,
consumer_secret: config.twitter_consumer_secret,
access_token_key: config.twitter_access_token_key,
access_token_secret: config.twitter_access_token_secret
});
var t = {};
var retryMax = 10;
var retry = retryMax;
t.stream = function(cond, onData) {
if (retry-- < 0) {
throw new Error('can\'t establish twitter connection.');
}
db.storeSearch(cond, function(e, search) {
client.stream('statuses/filter', cond, function(stream) {
retry = retryMax;
stream.on('data', function(data) {
db.storeTweet(data, search, function() {
if (e != null) {
throw e;
}
return onData(data);
});
});
stream.on('error', function(err) {
if (err.code !== void 0) {
db.errorLog(err);
try {
stream.destroy();
} catch(e) {
db.errorLog(e);
}
setTimeout(function() {
t.stream(cond, onData);
}, 1000);
}
});
});
});
};
module.exports = t;