-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
176 lines (146 loc) · 5 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// server.js
// Set up the Twitter library.
var Twit = require('twit')
function createTwitClient() {
return new Twit({
consumer_key: 'Z2LyQpbFs0OzmFRQHPJmmzhoS',
consumer_secret: '5T5Ep2yQHjf0XiGDUgSRJC38iliTdJg7teutnu50S4Z4e4nMEm',
access_token: '3837516989-IgSf9kbiEa2Df9EhD0Rut7sPh0QVh5cOJHsMk2j',
access_token_secret: 'rk40a0TCd7mGcy5HvOqj5gn15VwqgMJpX9S51kgg3iUn3'
});
}
// Set up express for serving the client.
var express = require('express');
var app = express();
// Specify the directory for static files.
app.use(express.static(__dirname + '/client'));
// Serve the index to clients.
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
// Set up the http server for sockets.
var http = require('http').Server(app);
var io = require('socket.io')(http);
// Listen for connections on the default port.
http.listen(process.env.PORT, function() {
console.log('listening on *:' + process.env.PORT);
});
// Listen for connections.
io.on('connection', function(client) {
console.log("* client " + client.id + " connected");
var streams = [];
var streamType = null;
var client_location = null;
var keyword = null;
var twitter = createTwitClient();
var lastTweetTime = new Date();
var paused = false;
// Listen for disconnects.
client.on('disconnect', function() {
console.log("* client " + client.id + " disconnected");
// Stop all streams.
disconnectAllStreams();
});
var disconnectAllStreams = function() {
streams.forEach(function (stream) {
stream.stop();
});
streams = [];
}
client.on('toggle-pause', function() {
console.log("* toggling pause...");
paused = !paused;
});
// Aggregate function for location and keyword streams.
var handleTweet = function(tweet) {
// Ignore retweets.
if (tweet.hasOwnProperty('retweeted_status')) {
if (tweet.retweeted_status.retweet_count > 0) {
//console.log("* client " + client.id + " ignoring retweet");
return;
}
}
// Ignore potentially sensitive tweets.
if (tweet.hasOwnProperty('possibly_sensitive')) {
if (tweet.possibly_sensitive) {
console.log("* client " + client.id + " ignoring sensitive content");
return;
}
}
if(tweet.place === null || tweet.place.country_code != "MX"){
// If a keyword filter has not been set just do location
if(keyword === null ){
console.log("* keyword not defined, falling back to location");
sendTweet(client, tweet)
}
else{
var tweetString = tweet.text.toLowerCase();
var regexExpression = "(\\b" + keyword + ")\\w*\\b";
var regex = new RegExp(regexExpression, 'g');
var matches = tweetString.match(regex);
// console.log("* found matches: " + matches);
// Send the tweet to the client.
if (matches != null || streamType == "Keyword Based"){
sendTweet(client, tweet)
}
}
}
};
var sendTweet = function(client, tweet) {
// Skip tweets while we're paused.
if (paused) {
console.log("* currently paused, skipping tweet");
return;
}
// Only send 1 tweet per second.
var currentTime = new Date();
var timeElapsed = ((currentTime - lastTweetTime) / 1000);
if (timeElapsed < 1) {
// console.log("* skipping tweet, only " + timeElapsed + "s elapsed");
return;
} else {
console.log("* " + timeElapsed + "s elapsed, sending tweet");
lastTweetTime = currentTime;
}
console.log("* client " + client.id + " was sent tweet: " + tweet.id);
client.emit('tweets', tweet);
}
//Listen for start stream button
client.on('start', function(data){
// Clear any previous streams.
disconnectAllStreams();
keyword = data.keyword;
streamType = data.type;
//Checking what type of stream to set up
//Location Stream
if(streamType == "Location Based"){
console.log("Starting Location Stream");
var stream = twitter.stream('statuses/filter', { locations: client_location});
// Push to streams array for multiple streams.
streams.push(stream);
// What to do with tweets
stream.on('tweet', function (tweet) {
handleTweet(tweet);
});
}
//Keyword Stream
else{
console.log("Starting Keyword Stream with keyword: " + keyword);
var stream = twitter.stream('statuses/filter', { track: keyword});
// Push to streams array for multiple streams.
streams.push(stream);
// What to do with tweets
stream.on('tweet', function (tweet) {
handleTweet(tweet);
});
}
});
// Listen for location notifications sent from the client.
client.on('location', function(data) {
console.log("* client " + client.id + " sent location: " + JSON.stringify(data));
// Set up a new stream to feed to the client.
// Set up stream based upon client location
client_location = [data.longitude - 1, data.latitude, data.longitude, data.latitude + 1];
});
// End IO connection
});