-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (84 loc) · 2.18 KB
/
index.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
var express = require('express');
const bodyParser = require('body-parser');
var Twit = require('twit');
var Sentiment = require('sentiment');
var sentiment = new Sentiment();
//var fs = require('fs');
var config = require('./config');
var T = new Twit(config);
var app = express();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.set('view engine', 'ejs');
// GET
app.get('/', function (req, res) {
res.render('index', {
query: null,
red: null,
green: null
});
});
// POST default
app.post('/', function (req, res) {
var query = req.body.query;
var date = getDate();
var query = {
q: `${query} since:${date}`,
result_type: 'popular',
count: 20,
lang: 'en',
truncated: 'false',
tweet_mode: 'extended',
};
T.get('search/tweets', query)
.catch(function (err) {
console.log('ERROR: ', err);
}).then(function (result) {
var x = [];
var json_res = [];
var pos = 0,neg = 0;
result.data.statuses.forEach(element => {
var text = element.full_text;
x.push(text);
var model = sentiment.analyze(text);
// console.log("Analysis:: ", model.score);
if (model.score >= 0)
{ // to add the neutral statement
pos++;
} else {
neg++;
}
k = {
'text': text,
'score': model.score,
'user': element.user.name,
'location': element.user.location
};
json_res.push(k);
});
res.render('index', {
query: x,
red: neg,
green: pos,
res: json_res
});
});
});
function getDate() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
return yyyy - mm - dd;
}
app.listen(3000, () => {
console.log(`App running at http://localhost:3000`)
});