-
Notifications
You must be signed in to change notification settings - Fork 2
/
github_cc.js
146 lines (127 loc) · 3.71 KB
/
github_cc.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
var express = require('express');
var app = express();
var https = require('https');
//var request = require('request');
var port = process.env.PORT || 8080;
var server = app.listen(port, function () {
console.log("running on port: %s", port);
})
app.get('/', function (req, res) {
res.send('howto: </br>\
/commits/:user - returns array with number of commits everyday</br>\
/total/:user - returns total number of commits this year</br>\
/commits/last/:user - returns number of commits for last 31 days');
})
app.get('/total/:user', function (req, res) {
var options = {
host: 'github.com',
port: 443,
path: '/' + req.params.user,
method: 'GET',
headers: {
accept: 'text/html'
}
};
https.request(options, function(cres){
console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
cres.setEncoding('utf8');
var body = '';
cres.on('data', function (chunk) {
//console.log('BODY: ' + chunk);
body += chunk;
});
cres.on('end', function () {
var counter = 0;
// get calendar wihtout svg tag
body = body.slice(body.indexOf('js-calendar-graph-svg')+23);
body = body.slice(0, body.indexOf('</svg>'));
body.split("\n").slice(2).map(c => c.trim()).forEach(c => {
let fill = c.match(/data-count="([0-9]+)"/);
if(fill){
counter += parseInt(fill[1]);
}
});
var obj = { "data" : counter}
res.send(JSON.stringify(obj));
});
console.log('test');
}).end();
})
app.get('/commits/:user', function (req, res) {
var options = {
host: 'github.com',
port: 443,
path: '/' + req.params.user,
method: 'GET',
headers: {
accept: 'text/html'
}
};
https.request(options, function(cres){
console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
cres.setEncoding('utf8');
var body = '';
cres.on('data', function (chunk) {
//console.log('BODY: ' + chunk);
body += chunk;
});
cres.on('end', function () {
var count = [];
// get calendar wihtout svg tag
body = body.slice(body.indexOf('js-calendar-graph-svg')+23);
body = body.slice(0, body.indexOf('</svg>'));
body.split("\n").slice(2).map(c => c.trim()).forEach(c => {
let fill = c.match(/data-count="([0-9]+)"/);
if(fill){
count.push(parseInt(fill[1]));
}
});
var obj = { "data" : count}
res.send(JSON.stringify(obj));
});
console.log('test');
}).end();
})
app.get('/commits/last/:user', function (req, res) {
var options = {
host: 'github.com',
port: 443,
path: '/' + req.params.user,
method: 'GET',
headers: {
accept: 'text/html'
}
};
https.request(options, function(cres){
console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
cres.setEncoding('utf8');
var body = '';
cres.on('data', function (chunk) {
//console.log('BODY: ' + chunk);
body += chunk;
});
cres.on('end', function () {
var count = [];
// get calendar wihtout svg tag
body = body.slice(body.indexOf('js-calendar-graph-svg')+23);
body = body.slice(0, body.indexOf('</svg>'));
console.log(body);
body.split("\n").slice(2).map(c => c.trim()).forEach(c => {
let fill = c.match(/data-count="([0-9]+)"/);
if(fill){
count.push(parseInt(fill[1]));
}
});
console.log(count.length);
count = count.slice(count.length-31, count.length);
var d = new Date();
var n = d.getDay()
var obj = { "day" : n,"data" : count}
res.send(JSON.stringify(obj));
});
console.log('test');
}).end();
})