-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·197 lines (168 loc) · 5.31 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Module dependencies.
*/
var express = require('express')
, io = require('socket.io')
, http = require('http')
, twitter = require('ntwitter')
, _ = require('underscore')
, path = require('path')
, fs = require('fs');
//Create an express app
var app = express();
//Create the HTTP server with the express app as an argument
var server = http.createServer(app);
// IMPORTANT!!
//You will need to get your own key. Don't worry, it's free. But I cannot provide you one
//since it will instantiate a connection on my behalf and will drop all other streaming connections.
//Check out: https://dev.twitter.com/ You should be able to create an application and grab the following
//crednetials from the API Keys section of that application.
/*var api_key = 'HJC58RkbqZeHo0Z6sesPrKWRc'; // <---- Fill me in
var api_secret = '2pDKqITFdoqRZlVFJvQ1iDfE6kyMqlDxcDlOpC6NuV6nAn5Jl4'; // <---- Fill me in
var access_token = '1007063444-nU2zSoqqvoOrKtttK6RBL4YK7PQ0UvnZAVb0GFo'; // <---- Fill me in
var access_token_secret = 'C7SYtnNCX5mAZLFvhP5QaYam5iTnvokOfKQ7LlDFEBEL4'; // <---- Fill me in
*/
var api_key = 'pue278QEfBJtbyWBXfnbBA'; // <---- Fill me in
var api_secret = 'WtYadmJb3atkSW6VKFS52WqHWNtnsfxyGmk9oiuTw4'; // <---- Fill me in
var access_token = '2436381469-k5jBECUwClQEVU1P8IuxM2ysP67yOVmBk9kpxN0'; // <---- Fill me in
var access_token_secret = '0AXmv8ekjvVmXYoAZKc3O7EJQotA9mhwPub1gA32Ulh18'; // <---- Fill me in
// Twitter symbols array.
var watchSymbols = ['@Columbia', 'columbia university', '#ColumbiaUniversity', '#Barnard', '#BarnardCollege', '@BarnardCollege'];
var tweets = [];
var sents = {};
initialize();
//Generic Express setup
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
//We're using bower components so add it to the path to make things easier
app.use('/components', express.static(path.join(__dirname, 'components')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
//Our only route! Render it with the current watchList
app.get('/', function(req, res) {
res.render("index", {});
});
app.get('/data', function(req, res)
{
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(tweets));
res.end();
});
//Start a Socket.IO listen
var sockets = io.listen(server);
//Set the sockets.io configuration.
//THIS IS NECESSARY ONLY FOR HEROKU!
sockets.configure(function() {
sockets.set('transports', ['xhr-polling']);
sockets.set('polling duration', 30);
});
//If the client just connected, give them fresh data!
sockets.sockets.on('connection', function(socket) {
console.log("Connected");
socket.emit('data', tweets);
//socket.emit('data', tweets);
});
// Instantiate the twitter connection
var t = new twitter({
consumer_key: api_key,
consumer_secret: api_secret,
access_token_key: access_token,
access_token_secret: access_token_secret
});
t.stream('statuses/filter', { track: watchSymbols }, function(stream) {
//We have a connection. Now watch the 'data' event for incomming tweets.
stream.on('data', function(tweet) {
var dict = {time : Date.parse(tweet.created_at)/1000, text : tweet.text, sent:calc_sentiment(tweet.text)};
if(dict['text'].indexOf('RT') === -1)
{
tweets.push(dict);
fs.writeFileSync('./tweets.txt', JSON.stringify(tweets));
sockets.sockets.emit('new_tweet', dict);
}
});
});
//Create the server
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
function initialize()
{
var lines = fs.readFileSync('./tweets.txt', 'utf8');
tweets = JSON.parse(lines);
lines = fs.readFileSync('./sent.txt', 'utf8');
sents = JSON.parse(lines);
console.log("initialized");
}
function calc_sentiment(tweet)
{
var value = 0.0;
var words = tweet_words(tweet);
var edited = false;
for (var i = 0; i < words.length; i++)
{
if (sents[words[i]] != null)
{
value += sents[words[i]];
edited = true;
}
}
if (!edited) return 0
return value
}
function tweet_words(tweet)
{
var tweet_string = removePunc(tweet);
tweet_string = strip(tweet_string);
var result = tweet_string.split(" ");
for (var i = 0; i < result.length; i++)
{
result[i]=result[i].toLowerCase();
}
return result;
}
function removePunc(tweet)
{
var tweet_string = "";
var punctuation = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
for (var i = 0; i < punctuation.length; i++)
{
var p = punctuation.charAt(i);
tweet_string = "";
for (var j = 0; j < tweet.length; j++)
{
var c = tweet.charAt(j);
if(p==c)
{
tweet_string += " ";
}
else
{
tweet_string += c;
}
}
tweet = tweet_string;
}
return tweet_string;
}
function strip(text)
{
var a = "";
for (var i = 0; i < text.length; i++)
{
var c = text.charAt(i);
if (text.charCodeAt(i) >= 128 || c=='\n' || c=='\t')
a += ' ';
else
a += c;
}
return a;
}