-
Notifications
You must be signed in to change notification settings - Fork 13
/
update_repos.js
76 lines (65 loc) · 1.66 KB
/
update_repos.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
/* Update the repositories listed on the homepage.
*
* This script gets repositories in the tableau organization from the GitHub API
* and filters the response down to a subset of fields.
* Note: We can't load the API response directly because we hit API rate limits.
*/
const fs = require("fs");
const https = require("https");
const repoFile = "js/github_repos.json";
const requestOpts = {
hostname: "api.github.com",
path: "/orgs/tableau/repos?per_page=100",
headers: {
"User-Agent": "request"
}
};
function filterJson(reposJson) {
let repos = [];
const fieldsToKeep = [
"name",
"html_url",
"id",
"description",
"stargazers_count",
"forks_count",
"language"
];
JSON.parse(reposJson).forEach(function(repo) {
let repoJsonSubset = {};
if (repo["archived"] == false) {
for (var field in repo) {
if (fieldsToKeep.indexOf(field) !== -1) {
repoJsonSubset[field] = repo[field];
}
}
repos.push(repoJsonSubset);
}
});
repos.sort(function (a, b) {
return b.stargazers_count - a.stargazers_count;
});
return repos;
}
function writeJsonToFile(outStr, outFile) {
let formattedJsonStr = JSON.stringify(outStr, null, 2);
fs.writeFile(outFile, formattedJsonStr, function(err) {
if (err) {
return console.log("Error: " + err);
}
});
}
const req = https
.get(requestOpts, resp => {
let fullResponseJson = "";
resp.on("data", d => {
fullResponseJson += d;
});
resp.on("end", () => {
writeJsonToFile(filterJson(fullResponseJson), repoFile);
});
})
.on("error", err => {
console.log("Error: " + err.message);
});
req.end();