Skip to content

Instantly share code, notes, and snippets.

@russellbeattie
Last active June 5, 2020 01:48
Show Gist options
  • Save russellbeattie/f9edf91115b43d6d7ca3cf0b4f9eb060 to your computer and use it in GitHub Desktop.
Save russellbeattie/f9edf91115b43d6d7ca3cf0b4f9eb060 to your computer and use it in GitHub Desktop.
const https = require('https');
main();
async function main() {
let args = process.argv.slice(2);
let days = parseInt(args[0]) || 14;
let urlStr = 'https://www.cdc.gov/coronavirus/2019-ncov/json/new-cases-chart-data.json';
let jsonText = await new Promise((resolve, reject) => {
https.get(urlStr, res => {
let data = '';
res.on('data', (d) => data += d );
res.on('end', () => resolve(data) );
res.statusCode == 200 || reject(res.statusCode);
}).on('error', (e) => reject(e));
});
let columns = JSON.parse(jsonText);
columns[0].shift();
columns[1].shift();
let graph = columns[0].map((v,i) => {
return {
day: v.replace('/2020',''),
val: parseInt(columns[1][i])
};
});
graph = graph.slice(-days);
let values = graph.map((v) => v.val);
let sum = values.reduce((a, b) => a + b, 0);
let avg = Math.round(sum / values.length) || 0;
console.log('\nAverage new cases: ' + new Intl.NumberFormat().format(avg));
console.log('day\t\tval');
console.log('------------------------');
graph.forEach((row) => {
console.log(row.day + '\t\t' + new Intl.NumberFormat().format(row.val));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment