-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
124 lines (103 loc) · 2.93 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Module dependencies.
*/
var express = require('express'),
http = require('http'),
fs = require('fs'),
path = require('path'),
uuid = require('uuid'),
_ = require('underscore'),
twitter = require('ntwitter'),
servername = "cloud",
lessMiddleware = require('less-middleware'),
n = 0;
function rotateServer() {
if (servername=="cloud") {
servername = "sandbox";
} else {
servername = "cloud";
}
}
var tags = process.argv.slice(2);
if (tags.length == 0) {
tags = ["obama", "putin", "ukraine"];
}
console.log("WATCHING TWEETS CONTAINING: " + tags.join(", "));
GLOBAL.connections = {};
var twit = new twitter({
consumer_key: process.env["consumer_key"],
consumer_secret: process.env["consumer_secret"],
access_token_key: process.env["access_token_key"],
access_token_secret: process.env["access_token_secret"],
});
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(lessMiddleware(path.join(__dirname, 'public'), {}, {}, {
compress: true,
sourceMap: true
}));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function(req, res) {
rotateServer();
n = (n+1) % 5;
res.render("classifier", { tags: tags, MODEL_N: n, servername: servername });
});
app.get('/raw', function(req, res) {
res.render('raw', { title: "Twitter Feed", keywords: tags.join(", ") });
});
app.get('/fun', function(req, res) {
res.render("fun");
});
app.get('/classifier', function(req, res) {
rotateServer();
n = (n+1) % 5;
res.render("classifier", { tags: tags, MODEL_N: n, servername: servername });
});
app.get('/tweets', function(req, res) {
// keep the connection open indefinitely
req.socket.setTimeout(Infinity);
// create a way for us to push data to the client
var conn = {
id: uuid.v4(),
send: function(data) {
var body = 'data: ' + JSON.stringify(data) + '\n\n';
res.write(body);
}
};
connections[conn.id] = conn;
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'X-Accel-Buffering': 'no' // Disable buffering for nginx
});
res.write('\n');
req.on('close', function() {
delete connections[conn.id];
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
twit.stream('statuses/filter', { track: tags }, function(stream) {
stream.on('data', function(tweet) {
_.each(connections, function(conn) {
if (/^RT/.exec(tweet.text) || tweet.retweet==true) {
return;
}
conn.send(tweet);
});
});
});