-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
143 lines (114 loc) · 3.26 KB
/
index.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
'use strict';
const unirest = require('unirest');
const Twit = require('twit');
const events = require('events');
const StockerBot = function(config) {
var T = new Twit({
consumer_key: config.CONSUMER_KEY,
consumer_secret: config.CONSUMER_SECRET,
access_token: config.ACCESS_TOKEN,
access_token_secret: config.ACCESS_TOKEN_SECRET,
app_only_auth: config.APP_ONLY_AUTH,
timeout_ms: config.TIMEOUT_MS
});
// make self reference the StockerBot instance
var self = this;
const call_delay = config.call_delay || 0;
const logger = config.logger || true;
const logger_path = config.logger_path;
var eventEmitter = new events.EventEmitter();
this.newTweet = eventEmitter;
this.lastTweet = {};
const log = function(msg) {
if (logger) {
if (logger_path) {
fs.writefile(logger_path, msg, function(err) {
if (err) console.log(err);
});
} else{
console.log(msg);
}
}
};
this.analyzeTweet = function analyzeTweet(screen_name, tweet) {
if (tweet.id > (this.lastTweet[screen_name] || 0)) {
self.lastTweet[screen_name] = tweet.id;
//emit the event that a new tweet was analyzed
tweet.screen_name = screen_name;
self.emit('newTweet', screen_name, tweet);
}
};
this.pollSpawner = function pollSpawner(screen_names, interval) {
setInterval(function() {
var screen_name
for (var i=0; i <screen_names.length; i++) {
screen_name = screen_names[i];
self.pollWorker(screen_name);
}
}, interval);
};
this.pollWorker = function pollWorker(screen_name) {
const path = 'statuses/user_timeline'
const options = {
'screen_name': screen_name,
// 'trim_user': 'true',
'exclude_replies': 'true'
};
if (self.lastTweet[screen_name]) {
options.since_id = self.lastTweet[screen_name];
}
T.get(path, options, function(err, data, response) {
if (err) {
log(err);
}
else {
if (data.length) {
// analyze and store the tweet
self.analyzeTweet(screen_name, data[0]);
//update last tweet
self.lastTweet[screen_name] = data[0].id;
}
}
});
};
this.searchSymbol = function searchSymbol(symbol, count) {
/*
* if count is set to -1 and the since date parameter is
* provided, this API will accumulate the tweets in batches ~ if very
* large, it can take a while
*
* Keep in mind that the search index has a 7-day limit.
*
*/
const path = 'search/tweets';
if (symbol.charAt(0) != '$') { symbol = '$' + symbol; }
symbol = symbol.trim();
console.log('searching for symbol: ', symbol);
const options = {
'q' : symbol,
'count' : count
};
var tweets;
T.get(path, options, function(err, data, response) {
if (err) {
log(err);
}
else {
tweets = data.statuses;
self.emit('symbolTweets', symbol, tweets);
}
});
}
}
// set up the event emmiter and start it
StockerBot.prototype = new events.EventEmitter;
StockerBot.prototype.pollAccounts = function(screen_names, interval) {
console.log('Polling accounts ...');
this.pollSpawner(screen_names, interval);
};
StockerBot.prototype.searchSymbol = function(symbol, count) {
console.log('Searching Twitter for symbol ', symbol, '...');
this.searchSymbol(symbol, count);
};
// make available to import from other modules
module.exports = StockerBot;