-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
234 lines (215 loc) · 6.03 KB
/
Copy pathapp.js
File metadata and controls
234 lines (215 loc) · 6.03 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var fs = require('fs');
var twitter = require('ntwitter');
var sentiment = require('sentiment');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.use(express.favicon(path.join(__dirname, 'public/images/favicon.ico')));
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.engine('html', require('ejs').renderFile);
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function (req, res){res.render('index.html');});
app.get('/results', function (req, res){res.render('results.html');});
app.get('/hashlist', function (req, res){res.render('hashlist.html');});
app.get('/selection', function (req, res){res.render('selection.html');});
//authenticate with twitter dev account
var twit = new twitter({
consumer_key: 'zjVQiFUnRX5ikVCLVgIw',
consumer_secret: 'pNllpbhujW2inNcNCQ8ktIIQGRaL70dzLBm9tXNLHLc',
access_token_key: '43466231-KhsLhbx53cgremwCTYeY80na6FfCoUwiPlNO5ylaL',
access_token_secret: 'tF7bYNLjDLcOZRmuhUSK0lucn4ZkF1jGZgbRs0DGPQOMb'
});
twit
.verifyCredentials(function (err, data) {
console.log("Verifying Credentials...");
if(err)
console.log("Verification Failed : " + err)
else
//console.log(data);
console.log("Verification Success");
})
var hashlist = new Array(25);
var hashfreq = new Array(25);
for(var i=0; i<25; i++)
{
hashlist[i]= 'undefined';
hashfreq[i]= 0;
}
app.post('/form', function (req, res)
{
var hash1 = (req.body.textfield).toLowerCase();
var hash2 = (req.body.textfield2).toLowerCase();
var startDate = (req.body.startDate);
var endDate = (req.body.endDate);
var radius = (req.body.number);
var lat = (req.body.lat);
var lon = (req.body.lon);
//console.log(hash1,hash2,startDate,endDate,radius,lat,lon);
//sentiment evaluation and construct soundcloud api implementation
var score1, score2, Total;
sentiment(hash1, function (err, result1){
score1 = (result1.score);
});
sentiment(hash2, function (err, result2){
score2 = (result2.score);
});
Total = score1 + score2;
var string = '<strong>Sentiment Score:</strong> ' + score1 + ' + ' + score2 + ' = ' + Total + '<p>';
if(Total <= 1 && Total >= -1)
{
string += 'Neutral Sentiment: ' + '<p> (-4) (-3 - -2) <b>(-1 - +1)</b> (+2 - +3) (4+)';
}
else if(Total == -2 || Total == -3)
{
string += 'Negative Sentiment: ' + '<p> (-4) <b>(-3 - -2)</b> (-1 - +1) (+2 - +3) (4+)';
}
else if(Total == 2 || Total == 3)
{
string += 'Positive Sentiment: ' + '<p> (-4) (-3 - -2) (-1 - +1) <b>(+2 - +3)</b> (4+)';
}
else if (Total >= 4)
{
string += 'Very Positive Sentiment: ' + '<p> (-4) (-3 - -2) (-1 - +1) (+2 - +3) <b>(4+)</b>';
}
else if (Total <= -4)
{
string += 'Very Negative Sentiment: ' + '<p> <b>(-4)</b> (-3 - -2) (-1 - +1) (+2 - +3) (4+)';
}
string += '</p><p><iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/'
if(Total <= 1 && Total >= -1)
{
string += '85910201';
}
else if(Total == -2 || Total == -3)
{
string += '60360398';
}
else if(Total == 2 || Total == 3)
{
string += '65625445';
}
else if (Total >= 4)
{
string += '106687285';
}
else if (Total <= -4)
{
string += '122952876';
}
string += '&color=00FF00&auto_play=false&show_artwork=true"></iframe></p>';
fs.writeFile('views/selection.html', string, function (err) {
if (err) throw err;
console.log('Sentiment saved!');
});
//search and insert input1 to arrays
for(var i=0; i < 25; i++)
{
var result = hash1.localeCompare(hashlist[i]);
if(result == 0 && hashlist[i]!='undefined')
{
hashfreq[i] += 1;
result = 0;
break;
}
else if(result != 0 && hashlist[i]== 'undefined')
{
hashlist[i] = hash1;
hashfreq[i] = 1;
break;
}
}
//above but for input2
for(var i=0; i < 25; i++)
{
var result = hash2.localeCompare(hashlist[i]);
if(result == 0 && hashlist[i]!='undefined')
{
hashfreq[i] += 1;
result = 0;
break;
}
else if(result != 0 && hashlist[i]== 'undefined')
{
hashlist[i] = hash2;
hashfreq[i] = 1;
break;
}
}
//sort the fudging arrays by hashfreq
var j = 0;
var index = 0;
while (j < 23)
{
var num = hashfreq[index+1] - hashfreq[index];
if (num > 0)
{
var tempHash = hashlist[index];
var tempNum = hashfreq[index];
hashlist[index] = hashlist[index+1];
hashfreq[index] = hashfreq[index+1];
hashlist[index+1] = tempHash;
hashfreq[index+1] = tempNum;
index = 0;
j = 0;
}
else if (num < 0 || num == 0)
{
index++;
j++;
}
}
//save array to html file for ajax display
var bigString = '<ol>';
for(var i=0; i < 10; i++)
{
bigString += '<li>';
bigString += '#';
if (hashlist[i] == 'undefined')
{
bigString += ' ';
}
else
{
if (hashfreq[i] == 1)
{
bigString += hashlist[i] + ' \(' + hashfreq[i] + ' time searched\)';
}
else
{
bigString += hashlist[i] + ' \(' + hashfreq[i] + ' times searched\)';
}
}
bigString += '</li>'
}
bigString += '</ol>'
fs.writeFile('views/hashlist.html', bigString, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
//end saving
res.redirect('results');
res.location('/results');
console.log("post received", hash1, hash2);
});
//server junk
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});