Skip to content

Commit 084e5c1

Browse files
committed
add us states api
1 parent c308b15 commit 084e5c1

File tree

8 files changed

+117
-6
lines changed

8 files changed

+117
-6
lines changed

app.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const { getCompleteTable, getGraph } = require('./lib/corona');
1212
const { lookupCountry, htmlTemplate, footer } = require('./lib/helpers');
1313
const { getLiveUpdates } = require('./lib/reddit.js');
1414
const { getWorldoMetersTable } = require('./lib/worldoMeters.js');
15+
const { getUsaStats } = require('./lib/country/us.js');
1516
const { helpContent, countryNotFound } = require('./lib/constants');
1617

1718
const app = express();
@@ -125,6 +126,26 @@ app.get('/help', (req, res) => {
125126
return res.send(chalk.green(helpContent));
126127
});
127128

129+
app.get('/states/:country', (req, res) => {
130+
const { country } = req.params;
131+
const isCurl = req.isCurl;
132+
const format = req.query.format ? req.query.format : '';
133+
const minimal = req.query.minimal === 'true';
134+
const top = req.query.top ? Number(req.query.top) : 1000;
135+
136+
const lookupObj = lookupCountry(country);
137+
138+
if (!lookupObj) {
139+
return res.status(404).send(countryNotFound(isCurl));
140+
}
141+
if (lookupObj.iso2 === 'US') {
142+
return getUsaStats({ isCurl, minimal, top, format})
143+
.then(result => {
144+
return res.send(result);
145+
}).catch(error => errorHandler(error, req, res));
146+
}
147+
});
148+
128149

129150
app.get('/:country', (req, res) => {
130151
const { country } = req.params;

bin/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { getCompleteTable, getGraph } = require('../lib/corona');
77
const { getCountryTable } = require('../lib/byCountry');
88
const { getWorldoMetersTable } = require('../lib/worldoMeters');
99
const { lookupCountry } = require('../lib/helpers');
10+
const { getUsaStats } = require('../lib/country/us');
1011

1112
const { argv } = yargs
1213
.command('$0 [country]', 'Tool to track COVID-19 statistics from terminal', yargs =>
@@ -68,12 +69,27 @@ const { argv } = yargs
6869
describe: 'Get graph',
6970
type: 'boolean',
7071
default: false,
72+
},
73+
st: {
74+
alias: 'states',
75+
describe: 'Get state level data of country ',
76+
type: 'string',
7177
}
7278
})
7379
.strict()
7480
.help('help');
7581

7682
argv.countryCode = argv.country;
83+
if (argv.states === 'US') {
84+
getUsaStats(argv).then(result => {
85+
console.log(result);
86+
process.exit(1);
87+
}).catch(error => {
88+
console.error(error);
89+
process.exit(0);
90+
});
91+
}
92+
7793
if (argv.source === 1) {
7894
(
7995
argv.country === 'ALL'

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Version 0.9.1
4+
5+
* Added US states api `corona --states=US`
6+
37
## Version 0.9.0
48

59
* Changed default source to worldoMeters. i.e source 2 is now default

lib/api.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ exports.getWorldoMetersData = async (countryCode = 'ALL') => {
7575
});
7676

7777
result.data.forEach(obj => {
78-
obj.countryCode = countryNameMap[obj.country];
7978
obj.confirmed = obj.cases;
79+
obj.countryCode = obj.countryInfo.iso2 || '';
8080
});
8181
worldStats.casesPerOneMillion = (worldStats.cases / 7794).toFixed(2);
8282
worldStats.confirmed = worldStats.cases;
@@ -89,3 +89,18 @@ exports.getWorldoMetersData = async (countryCode = 'ALL') => {
8989
myCache.set(key, returnObj, 60 * 15);
9090
return returnObj;
9191
};
92+
93+
exports.usaStats = async () => {
94+
const key = 'usaStats';
95+
const cache = myCache.get(key);
96+
97+
if (cache) {
98+
console.log('cache', key);
99+
return cache;
100+
}
101+
const result = await axios('https://corona.lmao.ninja/states');
102+
if (!result || !result.data) {
103+
throw new Error('usa stats API failure');
104+
}
105+
return result;
106+
};

lib/corona.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ exports.getGraph = async ({ countryCode = 'ALL', isCurl = true}) => {
7979

8080
const confirmedGraph = asciichart.plot(confirmedByDay, graphConfig);
8181
const body = chalk.greenBright(confirmedGraph)
82-
+ chalk.cyanBright('\n\n' +padding + '22 Feb' + graphLength + '22 Mar') + '\n';
82+
+ chalk.cyanBright('\n\n' +padding + '22 Feb' + graphLength + 'Today') + '\n';
8383

8484
if (!isCurl) {
8585
return htmlTemplate(body);

lib/country/us.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const Table = require('cli-table3');
2+
const chalk = require('chalk');
3+
const helpers = require('../helpers');
4+
const api = require('../api');
5+
const { cFormatter } = helpers;
6+
7+
8+
const getUsaStatsHeaders = (emojis, secondColumnName) => {
9+
const head = [
10+
'Rank',
11+
secondColumnName,
12+
`Total Cases ${emojis ? ' ✅' : ''}`,
13+
'New Cases ▲',
14+
`Total Deaths${emojis ? ' 😞' : ''}`,
15+
`New Deaths ▲${emojis ? ' 😞' : ''}`,
16+
`Active${emojis ? ' 😷' : ''}`,
17+
18+
];
19+
return head;
20+
};
21+
22+
23+
exports.getUsaStats = async ({
24+
isCurl = true,
25+
minimal = false,
26+
top = 1000,
27+
format,
28+
}) => {
29+
const secondColumnName = 'US States';
30+
const table = new Table({
31+
head: getUsaStatsHeaders(null, secondColumnName),
32+
chars: helpers.getTableBorders(minimal),
33+
style: helpers.getTableStyles(minimal),
34+
});
35+
const { data } = await api.usaStats();
36+
if (format === 'json') {
37+
return { data };
38+
}
39+
40+
let rank = 1;
41+
data.some(cd => {
42+
const values = [
43+
cFormatter(cd.state , chalk.cyanBright),
44+
cFormatter(cd.cases, chalk.green, 'right', true),
45+
cFormatter(cd.todayCases, chalk.cyanBright, 'right', true, ' ▲'),
46+
cFormatter(cd.deaths, chalk.whiteBright, 'right', true),
47+
cFormatter(cd.todayDeaths, chalk.redBright, 'right', true, ' ▲'),
48+
cFormatter(cd.active, chalk.blueBright , 'right', true),
49+
];
50+
table.push({ [rank++]: values });
51+
return rank === top + 1;
52+
});
53+
54+
const lastUpdated = new Date();
55+
const ret = table.toString() + helpers.footer(lastUpdated);
56+
return isCurl ? ret : helpers.htmlTemplate(ret);
57+
};

lib/helpers.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ ${chalk.greenBright('Twitter')}: ${chalk.blueBright('https://twitter.com/ekrysis
171171
172172
${chalk.magentaBright('Last Updated on:')} ${moment(lastUpdated).utc().format('DD-MMM-YYYY HH:MM')} UTC
173173
174-
${chalk.red.bold('UPDATE')}: ${chalk.blueBright('Source 2 is now default source')}
175-
${chalk.red.bold('JHU Source 1 table')}: ${chalk.blueBright('https://corona-stats.online?source=1')}
174+
${chalk.red.bold('US STATES API')}: ${chalk.blueBright('https://corona-stats.online/states/us')}
176175
${chalk.red.bold('HELP')}: ${chalk.blueBright('https://corona-stats.online/help')}
177176
`;
178177

@@ -230,7 +229,6 @@ e.getTableHeadersV2 = (emojis, secondColumnName) => {
230229
];
231230
return head;
232231
};
233-
234232
e.extraStats = (dataArr) => {
235233
return dataArr.map(obj => {
236234
return {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coronavirus-tracker-cli",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"description": "track conronavirus cases from cli",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)