-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.js
88 lines (73 loc) · 2.3 KB
/
twitter.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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Twit = require('twit');
var config = require('./config.json');
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
})
var happyFilter = ['happy', 'smile', 'laugh', 'win', 'joy']
var sadFilter = ['sad', 'frown', 'death', 'pain', 'low']
var angryFilter = ['angry', 'fight', 'hate', 'kill', 'punch']
var chillFilter = ['chill', 'sleep', 'relax', 'rest', 'calm']
var stream = T.stream('statuses/filter', {track : "filler"});
app.use(express.static(__dirname + '/'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
//socket.broadcast.emit('update', socket['id']);
stream.stop();
console.log('connected' + socket['id']);
socket.on('happy', function (data) {
console.log('happy')
stream.stop();
stream.params.track = happyFilter;
stream.start();
});
socket.on('sad', function (data) {
console.log('sad')
stream.stop();
stream.params.track = sadFilter;
stream.start();
});
socket.on('angry', function (data) {
console.log('angry')
stream.stop();
stream.params.track = angryFilter;
stream.start();
});
socket.on('chill', function (data) {
console.log('chill')
stream.stop();
stream.params.track = chillFilter;
stream.start();
});
socket.on('stop', function() {
console.log('stop');
stream.stop();
});
socket.on('disconnect', function () {
socket.broadcast.emit('disappear', socket['id']);
console.log(socket['id'] + ' has disconnected!');
});
});
stream.on('tweet', function (tweet) {
var tweet_text = tweet['text'];
var keyword = '';
var filter = stream.params.track;
for (var i = filter.length - 1; i >= 0; i--) {
if (tweet_text.indexOf(filter[i]) > -1) {
keyword = filter[i];
};
};
io.emit('update',{"keyword":keyword, "filter":filter});
console.log('stream');
});
http.listen(process.env.PORT || 3000, function(){
console.log('listening on', http.address().port);
});