-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
205 lines (166 loc) · 6.18 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
require('dotenv').config()
var express = require('express'),
app = express(),
mongoose = require('mongoose'),
PORT = process.env.PORT;
State = require('./server/models/statename'),
compression = require('compression'),
cors = require('cors'),
{PythonShell} = require('python-shell'),
Twitter = require('twitter'),
Sentiment = require('sentiment');
var sentiment = new Sentiment();
var client = new Twitter({
consumer_key: process.env.TWITTER_CK,
consumer_secret: process.env.TWITTER_CS,
access_token_key: process.env.TWITTER_AK,
access_token_secret: process.env.TWITTER_AS
});
app.use(compression());
app.use(cors());
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
mongoose.connect('mongodb+srv://predictcoviduser:ruhacks@cluster0-ztew1.mongodb.net/predictcovid?retryWrites=true&w=majority', () => {
console.log("connected to database on atlas");
});
app.use('/', express.static('covidSent/templates'));
app.use('/show', express.static('covidSent/templates/leaflet'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.post('/findSentimentInLocation', function(req, res){
// run python script and then get the results back
// with the results, post in a database, and then in client side code, make following request showing results of database
var location = req.body.area;
var searchterms = location + " AND ";
var sum = 0;
var comparativeSum = 0;
var sum2 = 0;
var comparativeSum2 = 0;
var numPosTweets = 0;
var numNegTweets = 0;
var numNeutralTweets = 0;
var numPosTweets2 = 0;
var numNegTweets2 = 0;
var numNeutralTweets2 = 0;
client.get('search/tweets', {q: searchterms + "covid", count: 100}, function(error, tweets, response) {
for(var i=0; i<tweets.statuses.length; i++){
var score = sentiment.analyze(tweets.statuses[i].text).score;
var comparativeScore = sentiment.analyze(tweets.statuses[i].text).comparative;
comparativeSum += comparativeScore;
sum += score;
// console.log(score)
//console.log(sum)
if(score < 0) {
numNegTweets++;
}
else if(score == 0) {
numNeutralTweets++;
}
else {
numPosTweets++;
}
//console.log(sum);
}
client.get('search/tweets', {q: searchterms + "#socialdistancing", count: 100}, function(error, tweets, response) {
for(var i=0; i<tweets.statuses.length; i++){
var score = sentiment.analyze(tweets.statuses[i].text).score;
var comparativeScore = sentiment.analyze(tweets.statuses[i].text).comparative;
comparativeSum2 += comparativeScore;
sum2 += score;
//console.log(score)
//console.log(sum)
if(score < 0) {
numNegTweets2++;
}
else if(score == 0) {
numNeutralTweets2++;
}
else {
numPosTweets2++;
}
//console.log(sum);
}
var obj = {score: sum+sum2, comparative: comparativeSum+comparativeSum2, numGood: numPosTweets2+numPosTweets, numBad: numNegTweets2+numNegTweets, numNeutral: numNeutralTweets2+numNeutralTweets2};
res.send(JSON.stringify(obj));
});
var obj = {score: sum, comparative: comparativeSum, numGood: numPosTweets, numBad: numNegTweets, numNeutral: numNeutralTweets};
//res.send(JSON.stringify(obj));
});
/*
var cityname = new CityName({
city: location,
num: 1
});
cityname.save();
*/
/*
PythonShell.run('covidSent/main.py', function (err) {
if (err) throw err;
console.log('finished');
});
*/
});
app.post('/findSentimentInState', function(req, res){
// run python script and then get the results back
// with the results, post in a database, and then in client side code, make following request showing results of database
var location = req.body.area;
var searchterms = location + " AND covid";
var sum = 0;
var comparativeSum = 0;
var numPosTweets = 0;
var numNegTweets = 0;
var numNeutralTweets = 0;
client.get('search/tweets', {q: searchterms, count: 100}, function(error, tweets, response) {
for(var i=0; i<tweets.statuses.length; i++){
var score = sentiment.analyze(tweets.statuses[i].text).score;
var comparativeScore = sentiment.analyze(tweets.statuses[i].text).comparative;
comparativeSum += comparativeScore;
sum += score;
// console.log(score)
//console.log(sum)
if(score < 0) {
numNegTweets++;
}
else if(score == 0) {
numNeutralTweets++;
}
else {
numPosTweets++;
}
//console.log(sum);
}
var storage = new State({stateName: location, score: sum, comparative: comparativeSum, numGood: numPosTweets, numBad: numNegTweets, numNeutral: numNeutralTweets});
storage.save();
});
/*
var cityname = new CityName({
city: location,
num: 1
});
cityname.save();
*/
/*
PythonShell.run('covidSent/main.py', function (err) {
if (err) throw err;
console.log('finished');
});
*/
});
app.get('/getStateData', function(req, res){
var arrStates = [];
State.find({} , (error, arr) => {
if(error){
} //do something...
arr.forEach(function(eachState){
var obj = {score: eachState.score, stateName: eachState.stateName, comparative: eachState.comparative, numGood: eachState.numGood, numBad: eachState.numBad, numNeutral: eachState.numNeutral};
arrStates.push(obj);
});
res.send(JSON.stringify(arrStates));
});
});
// Listen on specified port
app.listen(PORT, function() {
console.log(`Server running at http://localhost:${PORT}/`);
});