forked from mikehadlow/follow-retweeter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
follow-retweeter.js
64 lines (53 loc) · 1.42 KB
/
follow-retweeter.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
var util = require('util');
var twitter = require('twitter');
var twit = new twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
var hashtag = '#15below'
var hashtagRegex = new RegExp(hashtag, "i");
function write(data) {
if ( typeof data === 'string') {
console.log(data);
}
else if (data.text && data.user && data.user.screen_name) {
console.log(data.user.screen_name + ": " + data.text);
testForHashtag(data);
}
else if (data.delete) {
console.log('DELETE');
}
else if (data.message) {
console.log('ERROR' + data.message);
}
else {
console.log(util.inspect(data));
}
}
function testForHashtag(data) {
if(data.retweeted) return;
if (data.text.match(hashtagRegex) !== null) {
var tweetId = data.id_str;
console.log('retweet callback, tweetId: ' + tweetId);
twit.post('statuses/retweet/' + tweetId, function (error, tweet, response) {
var errorObj = util.inspect(error);
var tweetObj = util.inspect(tweet);
var responseObj = util.inspect(response);
// do something if there are errors.
});
}
}
function reconnect() {
setTimeout(startStreaming, 1000);
}
function startStreaming() {
twit.stream('user', function (stream) {
console.log('starting stream');
stream.on('data', write);
stream.on('end', reconnect);
});
}
startStreaming();
console.log('lisening for tweets');