-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
209 lines (191 loc) · 5.65 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var port = (process.env.VCAP_APP_PORT || 3000);
var express = require("express");
var sentiment = require('sentiment');
var twitter = require('ntwitter');
var DEFAULT_TOPIC = "Justin Bieber";
// defensiveness against errors parsing request bodies...
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err.stack);
});
var app = express();
// Configure the app web container
app.configure(function() {
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
});
// Sample keys for demo and article - you must get your own keys if you clone this application!
// Create your own app at: https://dev.twitter.com/apps
var tweeter = new twitter({
consumer_key: 'XDMBT427fgVzosqZlu07A',
consumer_secret: 'eWXL4TRv5B2UEHJ8cClCWt4Z2rF7yQIaVAbmj6A',
access_token_key: '14526702-uRxyEJuAcegZdlPahmMjY5gHIFleFiRL2ITpd0NR0',
access_token_secret: 'PrXTHWP2Mcpr7t1d7JCIaFlIR2yDcymeNFAhfqS9Y'
});
app.get('/twitterCheck', function (req, res) {
tweeter.verifyCredentials(function (error, data) {
res.send("Hello, " + data.name + ". I am in your twitters.");
});
});
var tweetCount = 0;
var tweetTotalSentiment = 0;
var monitoringPhrase;
app.get('/sentiment', function (req, res) {
res.json({monitoring: (monitoringPhrase !== null),
monitoringPhrase: monitoringPhrase,
tweetCount: tweetCount,
tweetTotalSentiment: tweetTotalSentiment,
sentimentImageURL: sentimentImage()});
});
app.post('/sentiment', function (req, res) {
try {
if (req.body.phrase) {
beginMonitoring(req.body.phrase);
res.send(200);
} else {
res.status(400).send('Invalid request: send {"phrase": "bieber"}');
}
} catch (exception) {
res.status(400).send('Invalid request: send {"phrase": "bieber"}');
}
});
function resetMonitoring() {
monitoringPhrase = "";
}
function beginMonitoring(phrase) {
var stream;
// cleanup if we're re-setting the monitoring
if (monitoringPhrase) {
resetMonitoring();
}
monitoringPhrase = phrase;
tweetCount = 0;
tweetTotalSentiment = 0;
tweeter.verifyCredentials(function (error, data) {
if (error) {
return "Error connecting to Twitter: " + error;
} else {
stream = tweeter.stream('statuses/filter', {
'track': monitoringPhrase
}, function (stream) {
console.log("Monitoring Twitter for " + monitoringPhrase);
stream.on('data', function (data) {
// only evaluate the sentiment of English-language tweets
if (data.lang === 'en') {
sentiment(data.text, function (err, result) {
tweetCount++;
tweetTotalSentiment += result.score;
});
}
});
});
return stream;
}
});
}
function sentimentImage() {
var avg = tweetTotalSentiment / tweetCount;
if (avg > 0.5) { // happy
return "/images/excited.png";
}
if (avg < -0.5) { // angry
return "/images/angry.png";
}
// neutral
return "/images/content.png";
}
app.get('/', function (req, res) {
var welcomeResponse = "<HEAD>" +
"<title>Twitter Sentiment Analysis</title>\n" +
"</HEAD>\n" +
"<BODY>\n" +
"<P>\n" +
"Welcome to the Twitter Sentiment Analysis app.<br>\n" +
"What would you like to monitor?\n" +
"</P>\n" +
"<FORM action=\"/monitor\" method=\"get\">\n" +
"<P>\n" +
"<INPUT type=\"text\" name=\"phrase\" value=\"" + DEFAULT_TOPIC + "\"><br><br>\n" +
"<INPUT type=\"submit\" value=\"Go\">\n" +
"</P>\n" + "</FORM>\n" + "</BODY>";
if (!monitoringPhrase) {
res.send(welcomeResponse);
} else {
var monitoringResponse = "<HEAD>" +
"<META http-equiv=\"refresh\" content=\"5; URL=http://" +
req.headers.host +
"/\">\n" +
"<title>Twitter Sentiment Analysis</title>\n" +
"</HEAD>\n" +
"<BODY>\n" +
"<P>\n" +
"The Twittersphere is feeling<br>\n" +
"<IMG align=\"middle\" src=\"" + sentimentImage() + "\"/><br>\n" +
"about " + monitoringPhrase + ".<br><br>" +
"Analyzed " + tweetCount + " tweets...<br>" +
"</P>\n" +
"<A href=\"/reset\">Monitor another phrase</A>\n" +
"</BODY>";
res.send(monitoringResponse);
}
});
app.get('/testSentiment', function (req, res) {
var response = "<HEAD>" +
"<title>Twitter Sentiment Analysis</title>\n" +
"</HEAD>\n" +
"<BODY>\n" +
"<P>\n" +
"Welcome to the Twitter Sentiment Analysis app. What phrase would you like to analyze?\n" +
"</P>\n" +
"<FORM action=\"/testSentiment\" method=\"get\">\n" +
"<P>\n" +
"Enter a phrase to evaluate: <INPUT type=\"text\" name=\"phrase\"><BR>\n" +
"<INPUT type=\"submit\" value=\"Send\">\n" +
"</P>\n" +
"</FORM>\n" +
"</BODY>";
var phrase = req.query.phrase;
if (!phrase) {
res.send(response);
} else {
sentiment(phrase, function (err, result) {
response = 'sentiment(' + phrase + ') === ' + result.score;
res.send(response);
});
}
});
app.get('/monitor', function (req, res) {
beginMonitoring(req.query.phrase);
res.redirect(302, '/');
});
app.get('/reset', function (req, res) {
resetMonitoring();
res.redirect(302, '/');
});
app.get('/hello', function (req, res) {
res.send("Hello world.");
});
app.get('/watchTwitter', function (req, res) {
var stream;
var testTweetCount = 0;
var phrase = 'bieber';
// var phrase = 'ice cream';
tweeter.verifyCredentials(function (error, data) {
if (error) {
res.send("Error connecting to Twitter: " + error);
}
stream = tweeter.stream('statuses/filter', {
'track': phrase
}, function (stream) {
res.send("Monitoring Twitter for \'" + phrase + "\'... Logging Twitter traffic.");
stream.on('data', function (data) {
testTweetCount++;
// Update the console every 50 analyzed tweets
if (testTweetCount % 50 === 0) {
console.log("Tweet #" + testTweetCount + ": " + data.text);
}
});
});
});
});
app.listen(port);
console.log("Server listening on port " + port);