-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
115 lines (92 loc) · 2.94 KB
/
Copy pathapp.js
File metadata and controls
115 lines (92 loc) · 2.94 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
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var mongoURL = 'mongodb://localhost:27017/tweets';
var fs = require('fs');
var http = require('http');
var url = require('url') ;
var util = require("util");
// NEVER use a Sync function except at start-up!
index = fs.readFileSync(__dirname + '/map.html');
//Lets define a port we want to listen to
const PORT=4040;
var itemsProcessed = 0;
var total =0;
var queryData;
const COLLECTION = 'tweets';
// Send index.html to all requests
var app = http.createServer(function(req, res) {
console.log("creating server");
res.writeHead(200, {'Content-Type': 'text/html'});
queryData = url.parse(req.url, true).query;
res.end(index);
//start();
});
app.listen(PORT);
// Socket.io server listens to our app
var io = require('socket.io').listen(app);
function start(){
console.log("starting");
MongoClient.connect(mongoURL, function(err, db) {
assert.equal(null, err);
console.log(JSON.stringify(queryData));
if(queryData){
if(queryData.page =="stream"){
console.log("starting stream");
findTweetsStream(db);
}
else if(queryData.page =="data"){
console.log("starting stats");
showStats(db);
}
}
});
}
// Emit welcome message on connection
io.on('connection', function(socket) {
// Use socket to communicate with this particular client only, sending it it's own id
MongoClient.connect(mongoURL, function(err, db) {
db.collection(COLLECTION).count(function(err, count){
socket.emit('welcome', { message: 'Welcome! '+count+' tweets tracked', id: socket.id });
});
});
start();
});
var showStats = function(db) {
var html = '';
db.collection(COLLECTION).count(function(err, count){
io.emit('data', count);
db.stats(function(err, stats){
io.emit('data', stats);
db.close();
});
});
};
var findTweetsStream = function(db, callback,res) {
var cursor =db.collection(COLLECTION).find({geo:{$ne:null }});
// var html = '<h2> Results '+queryData.search+' </h2>';
var counter=0;
cursor.on('data', function(tweet) {
if (tweet != null) {
var tweettext = tweet.text.toLowerCase();
console.log(tweettext);
//var data = { cord : tweet.geo.coordinates , eu : 'i' };
//io.emit('time', data);
var data = { cord : tweet.geo.coordinates , eu : 'x' };
io.emit('time', data);
if(tweettext.indexOf('brexit')>0){
data = { cord : tweet.geo.coordinates , eu : 'o' };
io.emit('time', data);
}
if(tweettext.indexOf('bremain')>0){
data = { cord : tweet.geo.coordinates , eu : 'i' };
io.emit('time', data);
}
console.log(counter++);
}
});
cursor.once('end', function() {
db.close();
});
};