Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,40 @@ function errorHandler(error, res) {
}

app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
app.use((req, res, next) => {
res.setHeader('Cache-Control', 'no-cache');
next();
});

app.get('/', (req, res) => {
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
const format = req.query.format ? req.query.format : '';

if (format.toLowerCase() === 'json') {
return getJSONData().then(result => {
res.setHeader('Cache-Control', 's-maxage=900');
return res.json(result);
}).catch(error => errorHandler(error, res));
}

return getCompleteTable().then(result => {
res.setHeader('Cache-Control', 's-maxage=900');
return getCompleteTable({ isCurl }).then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
});

app.get('/:country', (req, res) => {
const { country } = req.params;
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
const format = req.query.format ? req.query.format : '';

if (!country || 'ALL' === country.toUpperCase()) {
if (format.toLowerCase() === 'json') {
return getJSONData().then(result => {
res.setHeader('Cache-Control', 's-maxage=900');
return res.json(result);
}).catch(error => errorHandler(error, res));
}


return getCompleteTable().then(result => {
res.setHeader('Cache-Control', 's-maxage=900');
return getCompleteTable({ isCurl }).then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
}
Expand All @@ -69,13 +71,11 @@ app.get('/:country', (req, res) => {

if (format.toLowerCase() === 'json') {
return getJSONDataForCountry(iso2).then(result => {
res.setHeader('Cache-Control', 's-maxage=900');
return res.json(result);
}).catch(error => errorHandler(error, res));
}

return getCountryTable(iso2).then(result => {
res.setHeader('Cache-Control', 's-maxage=900');
return getCountryTable({ countryCode: iso2, isCurl }).then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
});
Expand Down
9 changes: 4 additions & 5 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env node
const yargonaut = require('yargonaut')
.style('green');
const yargonaut = require('yargonaut').style('green');
const yargs = require('yargs');
const chalk = require('chalk');
const { getCompleteTable } = require('../lib/corona');
const { getCountryTable } = require('../lib/byCountry');
const { lookupCountry } = require('../lib/helpers');

const { argv } = yargs
.command('$0 [country]','Tool to COVID-19 statistics for the world or the given country', yargs =>
.command('$0 [country]','Tool to track COVID-19 statistics for the world or the given country', yargs =>
yargs.positional('country', {
coerce(arg) {
if ('ALL' === arg.toUpperCase()) {
Expand Down Expand Up @@ -50,8 +49,8 @@ const { argv } = yargs
const { emojis, country } = argv;
(
country === 'ALL'
? getCompleteTable(emojis)
: getCountryTable(country, emojis)
? getCompleteTable({emojis})
: getCountryTable({ countryCode: country, emojis })
)
.then(console.log)
.catch(console.error);
2 changes: 1 addition & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const NodeCache = require( "node-cache" );
const NodeCache = require('node-cache');
const axios = require('axios');
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 600 } );
const CORONA_ALL_KEY = 'coronaAll';
Expand Down
28 changes: 27 additions & 1 deletion lib/byCountry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash');
const chalk = require('chalk');
const helpers = require('./helpers');
const api = require('./api');
const stripAnsi = require('strip-ansi');
const {
getCountry,
getConfirmed,
Expand Down Expand Up @@ -79,7 +80,7 @@ exports.getJSONDataForCountry = async (countryCode) => {
return countryData;
}

exports.getCountryTable = async (countryCode, emojis = false) => {
exports.getCountryTable = async ({countryCode, emojis = false, isCurl = true}) => {
const head = [
'',
'State',
Expand Down Expand Up @@ -147,5 +148,30 @@ exports.getCountryTable = async (countryCode, emojis = false) => {
});
}
const lastUpdated = countryData[0].lastUpdated;
if (!isCurl) {
const template = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono&display=swap" rel="stylesheet">
<title>Coronavirus Tracker</title>
<style>
body {
background: #0D0208;
color: #00FF41;
}
pre {
font-family: 'Roboto Mono', monospace;
white-space: pre;
}
</style>
</head>
<body>
<pre>${table.toString() + footer(lastUpdated)}</pre>
</body>
</html>`;
return stripAnsi(template);
}
return table.toString() + footer(lastUpdated);
}
30 changes: 28 additions & 2 deletions lib/corona.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash');
const chalk = require('chalk');
const helpers = require('./helpers');
const api = require('./api');
const stripAnsi = require('strip-ansi');
const {
getCountry,
getConfirmed,
Expand Down Expand Up @@ -79,7 +80,7 @@ function extraStats(dataArr) {
);
}

exports.getCompleteTable = async (emojis = false) => {
exports.getCompleteTable = async ({isCurl = true, emojis = false}) => {
const head = [
'',
'Country',
Expand Down Expand Up @@ -139,5 +140,30 @@ exports.getCompleteTable = async (emojis = false) => {
table.push({ [rank++]: values })
});
const lastUpdated = countryData[0].lastUpdated;
if (!isCurl) {
const template = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono&display=swap" rel="stylesheet">
<title>Coronavirus Tracker</title>
<style>
body {
background: #0D0208;
color: #00FF41;
}
pre {
font-family: 'Roboto Mono', monospace;
white-space: pre;
}
</style>
</head>
<body>
<pre>${table.toString() + footer(lastUpdated)}</pre>
</body>
</html>`
return stripAnsi(template);
}
return table.toString() + footer(lastUpdated);
}
}
35 changes: 25 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"corona": "./bin/index.js"
},
"scripts": {
"dev": "nodemon app.js",
"dev": "nodemon --inspect app.js",
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -29,6 +29,7 @@
"moment": "^2.24.0",
"morgan": "^1.9.1",
"node-cache": "^5.1.0",
"strip-ansi": "^6.0.0",
"yargonaut": "^1.1.4",
"yargs": "15.3.1"
},
Expand Down