2018-05-13 15:16:49 -04:00
|
|
|
const fs = require('fs');
|
2018-06-06 12:05:59 -04:00
|
|
|
const { promisify } = require('util');
|
2018-05-13 15:16:49 -04:00
|
|
|
const fetch = require('node-fetch');
|
2018-06-06 12:05:59 -04:00
|
|
|
const dayjs = require('dayjs');
|
2018-05-13 15:16:49 -04:00
|
|
|
|
2018-05-28 18:40:36 -04:00
|
|
|
require('draftlog').into(console);
|
|
|
|
|
2018-05-13 15:16:49 -04:00
|
|
|
process.on('unhandledRejection', error => {
|
|
|
|
console.log('unhandledRejection', error.message);
|
|
|
|
});
|
|
|
|
|
2018-05-28 18:07:41 -04:00
|
|
|
if (!process.env.TOKEN) {
|
|
|
|
throw new Error('no github token found');
|
|
|
|
}
|
2018-06-06 12:05:59 -04:00
|
|
|
|
|
|
|
// --- ENV VAR ---
|
2018-05-28 18:40:36 -04:00
|
|
|
const BATCH_SIZE = parseInt(process.env.BATCH_SIZE, 10) || 10;
|
|
|
|
const DELAY = parseInt(process.env.DELAY, 10) || 3000;
|
2018-06-06 12:05:59 -04:00
|
|
|
// --- 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 ---
|
2018-05-13 15:16:49 -04:00
|
|
|
const API = 'https://api.github.com/';
|
|
|
|
const options = {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'awesome-docker script listing',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `token ${process.env.TOKEN}`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-06-06 12:05:59 -04:00
|
|
|
// --- UTILS ---
|
2018-05-13 15:16:49 -04:00
|
|
|
function get(path, opt) {
|
|
|
|
return fetch(`${API}repos/${path}`, {
|
|
|
|
...options,
|
|
|
|
...opt,
|
|
|
|
})
|
|
|
|
.catch(err => console.error(err))
|
2018-05-28 18:40:36 -04:00
|
|
|
.then(r => {
|
|
|
|
if (r.ok) return r.json();
|
|
|
|
throw new Error('Network response was not ok.');
|
|
|
|
})
|
|
|
|
.catch(err => console.error(err));
|
2018-05-13 15:16:49 -04:00
|
|
|
}
|
2018-06-06 12:05:59 -04:00
|
|
|
const delay = ms =>
|
|
|
|
new Promise(resolve => {
|
2018-05-13 15:16:49 -04:00
|
|
|
setTimeout(() => resolve(), ms);
|
|
|
|
});
|
|
|
|
|
2018-06-06 12:05:59 -04:00
|
|
|
const extractAllRepos = markdown => {
|
2018-05-13 15:16:49 -04:00
|
|
|
const re = /https:\/\/github\.com\/([a-zA-Z0-9-._]+)\/([a-zA-Z0-9-._]+)/g;
|
|
|
|
const md = markdown.match(re);
|
2018-05-28 18:40:36 -04:00
|
|
|
return [...new Set(md)];
|
2018-06-06 12:05:59 -04:00
|
|
|
};
|
2018-05-28 18:40:36 -04:00
|
|
|
|
|
|
|
const barLine = console.draft('Starting batch...');
|
|
|
|
|
2018-06-06 12:05:59 -04:00
|
|
|
const ProgressBar = (i, batchSize, total) => {
|
|
|
|
const progress = Math.round((i / total) * 100);
|
2018-05-28 18:40:36 -04:00
|
|
|
const units = Math.round(progress / 2);
|
|
|
|
return barLine(
|
|
|
|
`[${'='.repeat(units)}${' '.repeat(50 - units)}] ${progress}% - # ${i}`,
|
|
|
|
);
|
2018-06-06 12:05:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const removeHost = x => x.slice('https://github.com/'.length, x.length);
|
|
|
|
|
|
|
|
const readFile = promisify(fs.readFile);
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
2018-05-13 15:16:49 -04:00
|
|
|
|
|
|
|
async function main() {
|
2018-06-06 12:05:59 -04:00
|
|
|
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),
|
|
|
|
);
|
|
|
|
|
|
|
|
const repos = githubRepos.map(removeHost);
|
2018-05-28 18:40:36 -04:00
|
|
|
|
2018-06-09 10:08:57 -04:00
|
|
|
const metadata = [];
|
2018-06-06 12:05:59 -04:00
|
|
|
/* 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)));
|
2018-06-09 10:08:57 -04:00
|
|
|
metadata.push(...res);
|
2018-06-06 12:05:59 -04:00
|
|
|
ProgressBar(i, BATCH_SIZE, repos.length);
|
|
|
|
await delay(DELAY);
|
|
|
|
}
|
2018-06-09 10:08:57 -04:00
|
|
|
|
|
|
|
fs.writeFile(
|
|
|
|
GITHUB_METADATA_FILE,
|
|
|
|
JSON.stringify(metadata, null, 2),
|
|
|
|
err => err && console.error(err),
|
|
|
|
);
|
2018-06-06 12:05:59 -04:00
|
|
|
ProgressBar(repos.length, BATCH_SIZE, repos.length);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('ERROR:', err);
|
2018-05-13 15:16:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|