1
0
Fork 0
mirror of https://github.com/veggiemonk/awesome-docker.git synced 2025-04-29 07:37:58 +02:00

Add historical data about repos

This commit is contained in:
Julien Bisconti 2018-06-06 18:05:59 +02:00
parent 7e1e70ef7c
commit e339187fed
5 changed files with 35430 additions and 332 deletions

View file

@ -1,5 +1,7 @@
const fs = require('fs');
const { promisify } = require('util');
const fetch = require('node-fetch');
const dayjs = require('dayjs');
require('draftlog').into(console);
@ -10,9 +12,17 @@ process.on('unhandledRejection', error => {
if (!process.env.TOKEN) {
throw new Error('no github token found');
}
// --- ENV VAR ---
const BATCH_SIZE = parseInt(process.env.BATCH_SIZE, 10) || 10;
const DELAY = parseInt(process.env.DELAY, 10) || 3000;
const readme = 'README.md';
// --- FILENAME ---
const README = 'README.md';
const GITHUB_METADATA_FILE = `data/${dayjs().format(
'YYYY-MM-DDTHH.mm.ss',
)}-fetched_repo_data.json`;
const GITHUB_REPOS = 'data/list_repos.json';
// --- HTTP ---
const API = 'https://api.github.com/';
const options = {
method: 'GET',
@ -23,6 +33,7 @@ const options = {
},
};
// --- UTILS ---
function get(path, opt) {
return fetch(`${API}repos/${path}`, {
...options,
@ -35,56 +46,62 @@ function get(path, opt) {
})
.catch(err => console.error(err));
}
function delay(ms) {
return new Promise(resolve => {
const delay = ms =>
new Promise(resolve => {
setTimeout(() => resolve(), ms);
});
}
function extractAllRepos(markdown) {
const extractAllRepos = markdown => {
const re = /https:\/\/github\.com\/([a-zA-Z0-9-._]+)\/([a-zA-Z0-9-._]+)/g;
const md = markdown.match(re);
return [...new Set(md)];
}
};
const barLine = console.draft('Starting batch...');
function ProgressBar(i, batchSize, total) {
const progress = Math.round(i / total * 100);
const ProgressBar = (i, batchSize, total) => {
const progress = Math.round((i / total) * 100);
const units = Math.round(progress / 2);
return barLine(
`[${'='.repeat(units)}${' '.repeat(50 - units)}] ${progress}% - # ${i}`,
);
}
};
const removeHost = x => x.slice('https://github.com/'.length, x.length);
const readFile = promisify(fs.readFile);
// ------------------------------------------------------------
async function main() {
const markdown = fs.readFileSync(readme, 'utf-8');
const githubRepos = extractAllRepos(markdown);
const repos = githubRepos.map(x =>
x.slice('https://github.com/'.length, x.length),
);
fs.writeFileSync(
'data/list_repos.json',
JSON.stringify(githubRepos, null, 2),
);
let data = [];
try {
const markdown = await readFile(README, { encoding: 'utf8' });
const githubRepos = extractAllRepos(markdown);
fs.writeFile(
GITHUB_REPOS,
JSON.stringify(githubRepos, null, 2),
err => err && console.error('FILE ERROR', err),
);
/* eslint-disable no-await-in-loop */
for (let i = 0; i < repos.length; i += BATCH_SIZE) {
const batch = repos.slice(i, i + BATCH_SIZE);
const res = await Promise.all(batch.map(async path => get(path)));
data = data.concat(res);
if (process.env.DEBUG) console.log({ batch });
ProgressBar(i, BATCH_SIZE, repos.length);
await delay(DELAY);
const repos = githubRepos.map(removeHost);
/* eslint-disable no-await-in-loop */
for (let i = 0; i < repos.length; i += BATCH_SIZE) {
const batch = repos.slice(i, i + BATCH_SIZE);
if (process.env.DEBUG) console.log({ batch });
const res = await Promise.all(batch.map(async path => get(path)));
fs.appendFile(
GITHUB_METADATA_FILE,
JSON.stringify(res, null, 2),
err => err && console.error(err),
);
ProgressBar(i, BATCH_SIZE, repos.length);
await delay(DELAY);
}
ProgressBar(repos.length, BATCH_SIZE, repos.length);
} catch (err) {
console.error('ERROR:', err);
}
ProgressBar(repos.length, BATCH_SIZE, repos.length);
if (process.env.DEBUG) console.log({ data });
fs.writeFileSync(
'data/fetched_repo_data.json',
JSON.stringify(data, null, 2),
);
}
main();