forked from arscan/encom-boardroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-server.js
289 lines (234 loc) Β· 7.62 KB
/
stream-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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* portions based on http://www.smartjava.org/content/html5-server-sent-events-angularjs-nodejs-and-expressjs */
var express = require('express'),
http = require('http'),
os = require('os'),
path = require('path'),
url = require("url"),
map = require("map-stream"),
request = require("request");
// some helper services
var LOCATIONLOOKUP = "http://localhost:8080/",
IPLOOKUP = "http://localhost:8081/json/",
USERLOOKUP = "http://localhost:8082/users/",
REPOLOOKUP = "http://localhost:8082/repos/";
// env vars
var PORT = process.env.PORT || 8081,
GITHUB_TOKEN = process.env.GITHUB_TOKEN;
// express middleware
var favicon = require('serve-favicon');
// create the streams
var GithubTimelineStream = require("github-timeline-stream"),
WikipediaStream = require("wikipedia-stream");
var githubStream = new GithubTimelineStream({token: GITHUB_TOKEN}),
wikipediaStream = new WikipediaStream();
console.log("UP ON PORT: " + PORT);
// create the app
var app = express();
// configure everything, just basic setup
app.set('port', PORT);
app.use(favicon("images/favicon.ico"));
app.use("/js", express.static(path.join(__dirname, 'js')));
app.use("/images", express.static(path.join(__dirname, 'images')));
app.use("/css", express.static(path.join(__dirname, 'css')));
app.use("/build", express.static(path.join(__dirname, 'build')));
app.enable('trust proxy');
var openConnections = [];
app.get('/', function(req, res){
res.sendfile(__dirname + "/index.html");
});
app.get('/events.js', function(req, res) {
req.socket.setTimeout(Number.MAX_VALUE);
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*'
});
res.write('\n');
openConnections.push(res);
console.log("New Connection. Current Connections: " + openConnections.length);
req.on("close", function() {
var toRemove;
for (var j =0 ; j < openConnections.length ; j++) {
if (openConnections[j] == res) {
toRemove =j;
break;
}
}
openConnections.splice(j,1);
sendData({
stream: "meta",
type: "disconnect",
size: openConnections.length
});
console.log("Closed Connection. Current Connections: " + openConnections.length);
});
sendData({
stream: "meta",
type: "connect",
size: openConnections.length
});
});
setInterval(function(){
sendData({
stream: "meta",
type: "heartbeat",
size: openConnections.length
});
}, 3000);
var sendData = function(data){
openConnections.forEach(function(resp) {
resp.write('id: ' + Date.now() + '\n');
resp.write('data:' + JSON.stringify(data) + '\n\n');
});
};
/* helpers to reformat the data */
/* should probably split this out */
var wikipediaLanguageMap = {
"en": "English",
"zh": "Chinese",
"fr": "French",
"ru": "Russian",
"es": "Spanish",
"it": "Italian",
"pt": "Portuguese",
"nl": "Dutch",
"de": "German",
"sv": "Swedish",
"vi": "Vietnamese",
"ja": "Japanese"
}
var lookupByLocation = function(loc, cb){
request.get(LOCATIONLOOKUP + loc, function(error, response, body){
var latlon = null;
try{
latlon = JSON.parse(body);
} catch (ex){
}
cb(latlon);
});
};
var formatAndSendGithubData = function(data){
if(data.location){
lookupByLocation(data.location, function(latlon){
if(latlon){
data.latlon = {
lat: parseFloat(latlon.lat),
lon: parseFloat(latlon.lng)
};
}
sendData(data);
});
} else {
sendData(data);
}
};
var lookupByIP = function(ip, cb){
request.get(IPLOOKUP + encodeURIComponent(ip), function(error, response, body){
var result = null;
try{
result = JSON.parse(body);
} catch (ex){
}
cb(result);
});
};
var formatAndSendWikipediaData = function(data){
if(data.ip){
lookupByIP(data.ip, function(result){
if(result){
if(result.latitude && result.longitude){
data.latlon = {
lat: result.latitude,
lon: result.longitude
};
}
if(result.city && result.city.length){
data.location = result.city + ", " + result.country_code;
} else {
data.location = result.country_name;
}
}
sendData(data);
});
} else {
sendData(data);
}
};
githubStream.pipe(map(function(data, callback){
var outdata = {
stream: "github",
ip: null
};
outdata.action = data.type;
/* TODO: REFACTOR TO USE ASYNC */
if(data.actor){
if(data.actor.avatar_url && data.actor.avatar_url.length > 0){
outdata.picSmall = data.actor.avatar_url + 's=89';
outdata.picLarge = data.actor.avatar_url + 's=184';
}
outdata.username = data.actor.login;
outdata.userurl = "http://github.com/" + data.actor.login + "/";
request.get(USERLOOKUP + data.actor.login, function(error, response, body){
if(error){
console.log("error looking up user..." + error);
}
try{
var actorInfo = JSON.parse(body);
if(actorInfo.location){
outdata.location = actorInfo.location;
}
} catch(ex) {
console.log("error looking up user... " + ex);
}
if(data.repo){
outdata.title = data.repo.name;
outdata.url = data.repo.url;
request.get(REPOLOOKUP + data.repo.name, function(error, response, body){
if(error){
console.log("error looking up repo..." + error);
}
try{
var repoInfo = JSON.parse(body);
outdata.size = repoInfo.size;
outdata.popularity = repoInfo.stargazers_count;
outdata.type = repoInfo.language;
} catch(ex) {
console.log("error looking up repo... " + ex);
}
callback(null, outdata);
})
} else {
callback(null, outdata);
}
});
} else {
callback(null, outdata);
}
})).on("data", formatAndSendGithubData);
wikipediaStream.pipe(map(function(data, callback){
var outdata = {
stream: "wikipedia",
location: null,
title: data.page,
type: data.language,
url: data.url,
size: parseInt(data.size, 10),
ip: data.ip,
username: data.user,
action: data.type
};
if(data.user){
outdata.picSmall = 'images/not_available_small.png';
outdata.picLarge = 'images/not_available_large.png';
outdata.userurl = "http://" + data.language + ".wikipedia.org/wiki/User:" + data.user;
}
if(data.language && wikipediaLanguageMap[data.language]){
outdata.type = wikipediaLanguageMap[data.language];
}
callback(null, outdata);
})).on("data", formatAndSendWikipediaData);
// startup everything
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
})