-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
158 lines (136 loc) · 4.67 KB
/
server.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
"use strict";
const express = require('express');
const app = express();
const twitter = require('twitter');
const keys = require('./env.js');
const sentiment = require('sentiment');
const _ = require('lodash');
const moment = require('moment');
const firebase = require('firebase');
const roadsObj = require('./config/roads.json');
let Roadarray = [];
for (let road in roadsObj) {
Roadarray.push({ road: road, lat: roadsObj[road].lat, lon: roadsObj[road].lon })
}
//node server connection
const server = app.listen(keys.ports.node_port, () => {
console.log('listening on port:' + keys.ports.node_port);
});
//socket Io connection
var io = require('socket.io').listen(server);
//Firebase db connection
var Fdb = firebase.initializeApp({
apiKey: keys.firebase.apiKey,
authDomain: keys.firebase.authDomain,
databaseURL: keys.firebase.databaseURL,
storageBucket: keys.firebase.databaseURL,
messagingSenderId: keys.firebase.messagingSenderId
});
var Fdatabase = Fdb.database();
app.use(express.static('public'));
//establish socket connection
io.sockets.on('connection', function () {
console.log("CONNECTED !");
});
var twitterkeys = {
consumer_key: keys.twitterKeys.consumer_key,
consumer_secret: keys.twitterKeys.consumer_secret,
access_token_key: keys.twitterKeys.access_token_key,
access_token_secret: keys.twitterKeys.access_token_secret
};
// Twitter streaming and Sentiment Analysis
const twit = new twitter(twitterkeys);
twit.stream('statuses/filter', { 'track': 'ma3Route' }, function (stream) {
stream.on('data', function (data) {
var tweet = JSON.stringify(data.text);
for (var i = 0; i < Roadarray.length; i++) {
if (_.includes(tweet, Roadarray[i].road) == true) {
var s = sentiment(tweet);
var currdatetime = moment().format('h:mm:ss a');
var timeSec = moment().format('x');
var sentimentanalysis;
if (s.score == 0) {
sentimentanalysis = "neutral";
}
else if (s.score < 0) {
sentimentanalysis = "negative";
}
else if (s.score > 0) {
sentimentanalysis = "positive"
}
var tweetObject = {
text: tweet,
timestamp: currdatetime,
time: timeSec,
sentiment: sentimentanalysis,
name: Roadarray[i].road,
lat: Roadarray[i].lat,
lon: Roadarray[i].lon
}
var newTweetKey = firebase.database().ref().child('tweets').push().key;
//Socket.io display tweets on HTML Page
io.emit("tweet", tweetObject);
//save the tweet to firebase db
var updates = {};
updates['/tweets/' + newTweetKey] = tweetObject;
Fdatabase.ref().update(updates);
//log results to command line
console.log(currdatetime);
console.log(Roadarray[i].road);
console.log(Roadarray[i].lat + "lon:" + Roadarray[i].lon);
console.log("Tweet: " + tweet);
console.log("Sentiment: " + s.score)
setTimeout(function () {
location.reload()
}, 2000);
}
}
//Retweets sentiment analysis
// if((data.retweeted_status != undefined)){
// var retweet = JSON.stringify(data.retweeted_status.text);
// for (var i=0; i<Roadarray.length; i++){
// if(_.includes(retweet,Roadarray[i].road)){
// var s = sentiment(retweet);
// var currdatetime = moment().format('h:mm:ss a');
// var timeSec = moment().format('x');
// var sentimentanalysis;
// if(s.score == 0){
// sentimentanalysis = "neutral";
// }
// else if(s.score < 0){
// sentimentanalysis = "negative";
// }
// else if(s.score > 0){
// sentimentanalysis = "positive"
// }
// var tweetObject = {
// text:retweet,
// timestamp: currdatetime,
// time: timeSec,
// trial: trial,
// sentiment: sentimentanalysis,
// name:Roadarray[i].road,
// lat:Roadarray[i].lat,
// lon:Roadarray[i].lon
// }
// //Socket.io display tweets on HTML Page
// io.emit("tweet", tweetObject);
// //save the tweet to firebase db
// var updates = {};
// updates['/tweets/'+ newTweetKey] = tweetObject;
// Fdatabase.ref().update(updates);
// // log results to command line
// console.log(currdatetime);
// console.log(trial);
// console.log("Retweet: "+ retweet);
// console.log("Sentiment: "+ s.score)
// console.log(Roadarray[i].lat+"lon:"+Roadarray[i].lon);
// }
// }
// }
});
// console log error
stream.on('error', function (error) {
console.log("error occured:" + error);
});
});