-
Notifications
You must be signed in to change notification settings - Fork 14
/
updatecache.js
executable file
·56 lines (51 loc) · 1.89 KB
/
updatecache.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
#!/usr/bin/env nodejs
const https = require('https');
const fs = require('fs');
var projectcache = [];
function fetchPage(page, cb){
https.get({
host: 'api.github.com',
path: '/orgs/CDCgov/repos?type=public&sort=updated&direction=desc&page=' + page,
headers: {'User-Agent': 'request'}
}, function(res){
let json = '';
res.on('data', function(chunk){ json += chunk });
res.on('end', function(){
if (res.statusCode === 200) {
try {
var projects = JSON.parse(json);
var n = projects.length;
for (var i = 0; i < n; i++) {
var project = projects[i];
var match = projectcache.findIndex(function (p) {
return p.id === project.id;
});
if (match > -1) {
if (projectcache[match].updated_at === project.updated_at) continue;
projectcache[match] = project;
} else {
projectcache.splice(i, 0, project);
}
}
if (projects.length === 30){
fetchPage(page + 1, cb);
} else {
return cb();
}
} catch (e) {
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
});
}
fetchPage(1, function(){
projectcache.sort((a, b) => {
return a['updated_at'] < b['updated_at'] ? 1 : -1;
});
fs.writeFile('projects.json', JSON.stringify(projectcache), function(){
console.log('Cache file (projects.json) has been updated!\nDon\'t forget to commit it!');
});
});