-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-github.js
83 lines (65 loc) · 2 KB
/
get-github.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
const axios = require('axios');
var fs = require('fs')
const FILENAME_STARS = 'data/github-stars.json'
const FILENAME_REPOS = 'data/github-repos.json'
const ORG = 'openclimatefix'
async function getReposForPage(page) {
const {
status,
data
} = await axios.get(`https://api.github.com/orgs/${ORG}/repos?per_page=100&page=${page}`);
if (status !== 200) {
throw err;
}
return data;
}
async function getSumStargazersForAllRepos() {
let i = 1;
let allRepos = [];
while (true) {
const repos = await getReposForPage(i)
allRepos = allRepos.concat(repos)
if (repos.length !== 100) {
break
}
}
const totalStargazers = allRepos.reduce((acc, cur) => acc + cur.stargazers_count, 0)
return totalStargazers;
}
function handleRepoCount() {
axios.get(`https://api.github.com/orgs/${ORG}`)
.then(response => {
fs.readFile(FILENAME_REPOS, function (err, data) {
if (err) throw err;
var output = JSON.parse(data)
output.push({
x: new Date(),
y: response.data.public_repos
})
fs.writeFile(FILENAME_REPOS, JSON.stringify(output), function (err) {
if (err) throw err;
console.log('Data was appended to file!');
});
})
})
.catch(error => {
console.log(error);
});
}
async function getGithubData() {
handleRepoCount();
const totalStars = await getSumStargazersForAllRepos();
fs.readFile(FILENAME_STARS, function (err, data) {
if (err) throw err;
var output = JSON.parse(data)
output.push({
x: new Date(),
y: totalStars
})
fs.writeFile(FILENAME_STARS, JSON.stringify(output), function (err) {
if (err) throw err;
console.log('Data was appended to file!');
});
})
}
getGithubData();