-
Notifications
You must be signed in to change notification settings - Fork 42
/
app.js
83 lines (67 loc) · 1.91 KB
/
app.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
var express = require('express'),
https = require('https'),
Q = require('q');
var app = express();
app.set('port', process.env.PORT || 4000);
app.set('views', __dirname + '/views');
app.set('view engine', 'hjs');
app.use(express.compress());
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.static('./public'));
app.use(app.router);
app.configure('development', function() {
app.use(express.errorHandler());
app.use(express.logger('dev'));
});
getGitHubData = function(name) {
var deferred = Q.defer(),
url = 'https://github.com/users/' + name + '/contributions';
https.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
deferred.resolve(body);
});
}).on('error', function(e) {
deferred.reject(e.message);
});
return deferred.promise;
};
app.get('/', function(req, res) {
if(req.query.username){
var names = req.query.username.replace(/\s/g, '').split(','),
promises = [],
promise;
names.forEach(function(name){
promises.push(getGitHubData(name));
});
promise = Q.allSettled(promises);
promise.then(function(results) {
var returning = [],
validNames = [];
names.forEach(function(name, i) {
if (results[i] != 'invalid') {
returning.push({ key: name, value: results[i].value });
validNames.push(name);
}
});
res.render('index', {
calendarData: returning,
names: validNames,
anyValidNames: validNames.length > 0,
namesString: validNames.join(','),
embeddable: req.query.embeddable,
playbutton: req.query.playbutton
});
}).fail(function(){
res.render('index');
});
} else {
res.render('index');
}
});
app.listen(app.get('port'));
console.log('Express server listening on port ' + app.get('port'));