-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFollower.js
More file actions
63 lines (51 loc) · 1.41 KB
/
Follower.js
File metadata and controls
63 lines (51 loc) · 1.41 KB
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
const EventEmitter = require('events');
const Twitter = require('twitter');
const colors = require('colors/safe');
// Vicki's Twitter ID
const ID = '834940874643615744';
class Follower extends EventEmitter {
constructor(config, pairs) {
super();
this.pairs = pairs;
this.twitter = new Twitter(config.twitter);
}
onTweet(pair, direction, callback) {
return this.on(`${pair}/${direction}`, callback);
}
startFollowing() {
const params = { follow: ID };
const stream = this.twitter.stream('statuses/filter', params);
stream.on('data', tweet => {
if (!tweet || tweet.user.id_str != ID) {
return;
}
let direction = null;
let text = tweet.text;
if (text.includes('long')) {
direction = 'long';
text = colors.green(text);
} else if (text.includes('short')) {
direction = 'short';
text = colors.red(text);
}
let pair;
this.pairs.forEach(_pair => {
if (tweet.text.includes(_pair)) {
pair = _pair;
}
});
if (!pair || !direction) {
return;
}
this.emit(`${pair}/${direction}`);
console.log(colors.gray(tweet.created_at));
console.log(text);
console.log('Currency pair: ' + colors.yellow(pair));
console.log('');
});
stream.on('error', error => {
console.error(error);
});
}
}
module.exports = Follower;