-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (41 loc) · 1.13 KB
/
server.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
var nconf = require('nconf');
var _ = require('lodash');
var Twit = require('twit');
var Faye = require('faye');
/*
* get config from file
*/
nconf.file({ file: 'config.json' });
var hashtags = nconf.get('hashtags');
/*
* Bayeux Pub/Sub
*/
var pubsub = new Faye.Client(nconf.get('pubsub').url);
var normalize = function(tweet){
return {
text: tweet.text,
timestamp: tweet.created_at,
persona: {
handle: tweet.user.screen_name + '@twitter.com',
url: 'https://twitter.com/' + tweet.user.screen_name
}
};
};
/*
* twitter thinkgy ;)
*/
var forward = function(tweet){
var inTweet = _.map(tweet.entities.hashtags, function(hashtag){ return hashtag.text.toLowerCase(); });
var common = _.intersection(hashtags, inTweet);
var channels = _.map(common, function(hashtag){ return '/hashtags/' + hashtag; });
_.forEach(channels, function(channel){
pubsub.publish(channel, normalize(tweet));
});
};
var T = new Twit(nconf.get('auth'));
var stream = T.stream('statuses/filter', {
track: _.map(hashtags, function(hashtag){ return '#' + hashtag; })
});
stream.on('tweet', function(tweet) {
forward(tweet);
});