forked from elneilios/AzureSpeedTestMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
237 lines (205 loc) · 5.48 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var http = require("http");
var https = require("https");
var express = require("express");
var fs = require('fs');
var _ = require("underscore");
var mapquest = require("mapquest");
var app = express();
app.use(express.bodyParser());
app.listen(process.env.PORT || 8080);
var twitterToken;
var STATE_FILEPATH = __dirname + '/state.json';
var STATE = {
refresh_url: undefined,
tweets: []
};
var dcs = ["West Europe",
"Southeast Asia",
"East Asia",
"North Central US",
"North Europe",
"South Central US",
"West US",
"East US",
"Japan East",
"Japan West",
"Brazil South",
"Central US",
"East US 2",
"Australia Southeast",
"Australia East",
"West UK",
"South UK",
"Canada Central",
"Canada East"];
app.get('/', function(req, res){
res.sendfile(__dirname + '/AzureSpeedTestMap.html');
});
app.get('/locations', function(req, res){
res.json(_.filter(STATE.tweets, function(t){return t.geo != undefined}));
});
parseTweets = function(tweets){
var ft = [];
tweets.statuses.forEach(function(status){
if(status.retweeted == false){
var formatted = {};
formatted.text = status.text;
formatted.dc = parseDataCentre(status.text);
formatted.latency = parseLatency(status.text);
formatted.addr = status.user.location;
formatted.geo = status.geo;
if(formatted.dc){
ft.push(formatted);
}
}
});
return ft;
}
parseLatency = function(tweet){
var endIx = tweet.indexOf("ms)");
var snippet = tweet.substring(endIx - 10, endIx); // in case "(" is used elsewhere in the tweet
var startIx = snippet.indexOf("(") + 1;
return snippet.substring(startIx, snippet.length);
}
parseDataCentre = function(tweet){
var result;
dcs.forEach(function(dc){
if(tweet.indexOf(dc) != -1){
result = dc;
}
});
return result;
}
geocodeAddresses = function(i){
if(i < STATE.tweets.length){
var ft = STATE.tweets[i];
if(ft.addr && !ft.geo){
// Throttle the calls to geocoding api so we don't hit the query limit
setTimeout(function(){
geocodeGoogle(ft, i);
//geocodeMapQuest(ft);
}, 1000 * i);
}
geocodeAddresses(i+1);
}
}
geocodeMapQuest = function(tweet){
mapquest.geocode(tweet.addr, function(err, location){
if(err){
console.log("geocodeMapQuest Error: " + err);
}
else{
if(location){
console.log("geocodeMapQuest Response: " + location);
tweet.geo = location.latLng;
}
}
});
}
geocodeGoogle = function(tweet, i){
var options = {
host:'maps.googleapis.com',
path:'/maps/api/geocode/json?sensor=false&address=' + encodeURI(tweet.addr),
};
http.get(options, function(res){
console.log("geocodeGoogle Response: " + res.statusCode);
var data = '';
res.on('data', function(chunk){
data += chunk;
});
res.on('end', function(){
console.log(data);
var response = JSON.parse(data);
if(response.status == "OK"){
console.log(response.results[0]);
tweet.geo = response.results[0].geometry.location;
}
else if(response.status == "ZERO_RESULTS"){
STATE.tweets.splice(i, 1);
}
})
}).on('error', function(e){
console.log("geocodeGoogle Error: " + e.message);
});
}
getToken = function(auth){
var options = {
host:'api.twitter.com',
path:'/oauth2/token',
method:'POST',
headers: {
'Authorization':'Basic ' + auth,
'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'
}
};
var req = https.request(options, onGetToken);
req.on('error', function(error){
console.log('getToken Error: ' + error);
});
req.write('grant_type=client_credentials');
req.end();
}
onGetToken = function(res){
res.on('data', function(chunk){
console.log("getToken Response: " + chunk);
twitterToken = JSON.parse(chunk);
console.log("Twitter Token: " + twitterToken.access_token);
setInterval(getTweets, 300000); // Refresh every five minutes
getTweets();
});
}
getTweets = function(){
rawTweets = "";
var query_string = STATE.refresh_url == undefined ? '?q=%23AzureSpeedTest&result_type=recent' : STATE.refresh_url;
console.log("query_string: " + query_string);
var options = {
host: 'api.twitter.com',
path: '/1.1/search/tweets.json' + query_string,
headers: {
'Authorization':'Bearer ' + twitterToken.access_token,
}
};
https.get(options, function(res){
console.log("getTweets Response: " + res.statusCode);
var rawTweets = "";
res.on('data', function(chunk){
rawTweets += chunk.toString('utf-8');
});
res.on('end', function(){
var tweets = JSON.parse(rawTweets);
console.log(tweets);
STATE.tweets = parseTweets(tweets).concat(STATE.tweets); // put new tweets at the top
// Limit to the latest 1000 tweets
/*
if(STATE.tweets.length > 1000){
STATE.tweets.splice(1000, STATE.tweets.length - 1000);
}
*/
console.log("Loaded " + STATE.tweets.length + " tweets");
geocodeAddresses(0);
STATE.refresh_url = tweets.search_metadata.refresh_url;
})
}).on('error', function(e){
console.log("getTweets Error: " + e.message);
});
}
saveSTATE = function(){
fs.writeFile(STATE_FILEPATH, JSON.stringify(STATE), function(err){
if(err){
console.log("Error writing state: " + err);
}
});
}
loadSTATE = function(){
fs.readFile(STATE_FILEPATH, function(err, data){
if(err){
console.log("Error reading state: " + err);
}
else{
STATE = JSON.parse(data);
}
});
}
loadSTATE();
setInterval(saveSTATE, 10000);
getToken(new Buffer(encodeURI('f8enoi8BcChm2rfEzFiUJQ') + ':' + encodeURI('MG0eCiSJAlcsrp68Xinm7xqhXuMWqF1SVXJBYV4g4Q')).toString('base64'));