forked from joshvermaire/node-tweetbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·98 lines (75 loc) · 2.16 KB
/
app.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
var Twitter, config, growl, growlTweet,streamTwitter,twitter;
var http = require('http')
, fs = require('fs')
growl = require("growl");
Twitter = require("ntwitter");
config = require("./config");
growlTimeout = false;
twitter = new Twitter({
consumer_key: config.key,
consumer_secret: config.secret,
access_token_key: config.tokenKey,
access_token_secret: config.tokenSecret
});
growlTweet = function(tweet,image) {
var length, title;
//if coming tweet was retweet
if(tweet.retweeted_status)
tweet = tweet.retweeted_status;
title = tweet.user.name;
growl(tweet.text, {
title: title,
image: image,
url:"tweetbot://"+tweet.user.screen_name+"/status/"+tweet.id_str
});
};
streamTwitter = function()
{
twitter.stream('user', function(stream) {
stream.on('data', function (tweet) {
if(tweet.user)
{
if(tweet.retweeted_status)
tweet = tweet.retweeted_status;
getAvatar(tweet);
}
});
stream.on('end', function (response) {
// Handle a disconnection
});
stream.on('destroy', function (response) {
// Handle a 'silent' disconnection from Twitter, no end/error event fired
});
});
}
getAvatar = function (tweet)
{
var crypto = require('crypto');
var hash = crypto.createHash('md5').update(tweet.user.profile_image_url).digest("hex");
var filename= __dirname+"/temp/"+hash+".png";
fs.exists(filename, function(exists) {
if (exists) {
//avatar exist .. go growl
growlTweet(tweet,filename);
} else {
//avatar doesn't exist .. download then Growl
var request = http.get(tweet.user.profile_image_url, function(res){
var imagedata = ''
res.setEncoding('binary');
res.on('data', function(chunk){
imagedata += chunk
})
res.on('end', function(){
fs.writeFile(filename, imagedata, 'binary', function(err){
if (err) throw err
growlTweet(tweet,filename);
})
})
})
}
});
}
twitter.verifyCredentials(function(err, data) {
streamTwitter();
});
console.log('wait for tweets .. ')