Skip to content

Commit dd945bc

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 3139711 + c34e15f commit dd945bc

File tree

9 files changed

+1118
-35
lines changed

9 files changed

+1118
-35
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules
1+
node_modules/
2+
.DS_Store
3+
.now/

app.js

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,90 @@
11
const express = require('express');
22
const app = express();
3+
const lookup = require('country-code-lookup');
4+
const morgan = require('morgan');
35

46
const port = process.env.PORT || 3001;
57

6-
const { getCountryTable } = require('./lib/byCountry');
8+
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
79
const { getCompleteTable } = require('./lib/corona');
810
const { countryUpperCase } = require('./lib/helpers');
911

1012

13+
function errorHandler(error, res) {
14+
console.error(error);
15+
return res.send('I am sorry. Something went wrong. Please report it');
16+
}
17+
18+
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
19+
1120
app.get('/', (req, res) => {
21+
const format = req.query.format ? req.query.format : '';
22+
23+
if (format.toLowerCase() === 'json') {
24+
return getJSONData().then(result => {
25+
res.setHeader('Cache-Control', 's-maxage=86400');
26+
return res.json(result);
27+
}).catch(error => errorHandler(error, res));
28+
}
29+
1230
return getCompleteTable().then(result => {
31+
res.setHeader('Cache-Control', 's-maxage=900');
1332
return res.send(result);
14-
}).catch(error => res.send(error));
33+
}).catch(error => errorHandler(error, res));
1534
});
1635

1736
app.get('/:country', (req, res) => {
37+
38+
const { country } = countryUpperCase(req.params);
39+
let lookupObj = null;
40+
const format = req.query.format ? req.query.format : '';
1841

19-
let { country } = countryUpperCase(req.params);
2042
if (!country || country === 'All') {
43+
if (format.toLowerCase() === 'json') {
44+
return getJSONData().then(result => {
45+
res.setHeader('Cache-Control', 's-maxage=900');
46+
return res.json(result);
47+
}).catch(error => errorHandler(error, res));
48+
}
49+
50+
2151
return getCompleteTable().then(result => {
52+
res.setHeader('Cache-Control', 's-maxage=900');
2253
return res.send(result);
23-
}).catch(error => res.send(error));
54+
}).catch(error => errorHandler(error, res));
2455
}
25-
return getCountryTable(country).then(result => {
56+
57+
try {
58+
lookupObj = lookup.byIso(country)
59+
|| lookup.byFips(country)
60+
|| lookup.byCountry(country);
61+
} catch (error) {
62+
lookupObj = lookup.byFips(country) || lookup.byCountry(country);
63+
}
64+
if (!lookupObj) {
65+
return res.send(`
66+
Country not found.
67+
Try full country name or country code.
68+
Ex:
69+
- /UK: for United Kingdom
70+
- /US: for United States of America.
71+
- /India: for India.
72+
`);
73+
}
74+
const { iso2 } = lookupObj;
75+
76+
if (format.toLowerCase() === 'json') {
77+
return getJSONDataForCountry(iso2).then(result => {
78+
res.setHeader('Cache-Control', 's-maxage=900');
79+
return res.json(result);
80+
}).catch(error => errorHandler(error, res));
81+
}
82+
83+
return getCountryTable(iso2).then(result => {
84+
res.setHeader('Cache-Control', 's-maxage=900');
2685
return res.send(result);
27-
}).catch(error => res.send(error));
86+
}).catch(error => errorHandler(error, res));
2887
});
2988

89+
3090
app.listen(port, () => console.log(`Running on ${port}`));

lib/api.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const NodeCache = require( "node-cache" );
2+
const axios = require('axios');
3+
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 600 } );
4+
const CORONA_ALL_KEY = 'coronaAll';
5+
6+
exports.getCoronaData = async () => {
7+
const coronaCache = myCache.get(CORONA_ALL_KEY);
8+
if (coronaCache) {
9+
return coronaCache;
10+
}
11+
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
12+
if (!result || !result.data) {
13+
throw new Error(`Source API faliure.`);
14+
}
15+
myCache.set(CORONA_ALL_KEY, result.data, 60 * 15);
16+
return result.data;
17+
}

lib/byCountry.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const axios = require('axios');
33
const _ = require('lodash');
44
const chalk = require('chalk');
55
const helpers = require('./helpers');
6+
const api = require('./api');
67
const {
78
getCountry,
89
getConfirmed,
@@ -61,18 +62,33 @@ function extraStats(dataArr) {
6162
);
6263
}
6364

64-
exports.getCountryTable = async (country) => {
65+
exports.getJSONData = async () => {
66+
const data = await api.getCoronaData();
67+
const { latest, confirmed, deaths, recovered } = data;
68+
const countryData = getDataByState(confirmed, deaths, recovered);
69+
return countryData;
70+
}
71+
72+
exports.getJSONDataForCountry = async (countryCode) => {
73+
const data = await api.getCoronaData();
74+
const { latest, confirmed, deaths, recovered } = data;
75+
const countryData = getDataByState(confirmed, deaths, recovered, countryCode)
76+
.filter(obj => obj.countryCode === countryCode);
77+
return countryData;
78+
}
79+
80+
exports.getCountryTable = async (countryCode) => {
6581
const head = [
6682
'',
6783
'State',
68-
'Confirmed',
69-
'Recovered 😃',
70-
'Deaths 😞',
71-
'Active 😷',
84+
'Confirmed',
85+
'Recovered',
86+
'Deaths',
87+
'Active',
7288
'Mortality %',
7389
'Recovered %',
74-
'1 Day 🔺',
75-
'1 Week 🔺',
90+
'1 Day ',
91+
'1 Week ',
7692
// 'RoG',
7793
// '🏳',
7894
];
@@ -83,13 +99,13 @@ exports.getCountryTable = async (country) => {
8399
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
84100
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
85101
});
86-
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
87-
const { latest, confirmed, deaths, recovered } = result.data;
88-
const countryData = getDataByState(confirmed, deaths, recovered, country)
89-
.filter(obj => obj.country === country);
102+
const data = await api.getCoronaData();
103+
const { latest, confirmed, deaths, recovered } = data;
104+
const countryData = getDataByState(confirmed, deaths, recovered)
105+
.filter(obj => obj.countryCode === countryCode);
90106
const totalStats = getTotalStats(countryData);
91107
table.push({
92-
[country]: [
108+
[countryData[0].country]: [
93109
'Total',
94110
getConfirmed(totalStats.confirmed),
95111
getRecovered(totalStats.recovered),

lib/corona.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const axios = require('axios');
33
const _ = require('lodash');
44
const chalk = require('chalk');
55
const helpers = require('./helpers');
6+
const api = require('./api');
67
const {
78
getCountry,
89
getConfirmed,
@@ -22,8 +23,6 @@ const {
2223
footer,
2324
} = require('./helpers');
2425

25-
26-
2726
function getDataByCountry(confirmed, deaths, recovered) {
2827
const countryMap = {};
2928
const confirmedMap = _.keyBy(confirmed.locations, (i) => i.country + i.province);
@@ -82,14 +81,14 @@ exports.getCompleteTable = async () => {
8281
const head = [
8382
'',
8483
'Country',
85-
'Confirmed',
86-
'Recovered 😃',
87-
'Deaths 😞',
88-
'Active 😷',
84+
'Confirmed',
85+
'Recovered',
86+
'Deaths',
87+
'Active',
8988
'Mortality %',
9089
'Recovered %',
91-
'1 Day 🔺',
92-
'1 Week 🔺',
90+
'1 Day ',
91+
'1 Week ',
9392
// 'RoG',
9493
// '🏳',
9594
];
@@ -100,8 +99,8 @@ exports.getCompleteTable = async () => {
10099
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
101100
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
102101
});
103-
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
104-
const { latest, confirmed, deaths, recovered } = result.data;
102+
const data = await api.getCoronaData();
103+
const { latest, confirmed, deaths, recovered } = data;
105104
const countryData = getDataByCountry(confirmed, deaths, recovered)
106105
const worldStats = getTotalStats(countryData);
107106
table.push({
@@ -123,7 +122,7 @@ exports.getCompleteTable = async () => {
123122
countryData.forEach(cd => {
124123
const countryEmoji = getEmoji(cd.countryCode);
125124
const values = [
126-
getCountry(cd.country),
125+
getCountry(`${cd.country} (${cd.countryCode})`),
127126
getConfirmed(cd.confirmed),
128127
getRecovered(cd.recovered),
129128
getDeaths(cd.deaths),

lib/helpers.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ e.getOneDayChange = ({ confirmedByDay }) => {
104104
const present = confirmedByDay.length - 1;
105105
const dailyChange = confirmedByDay[present] - confirmedByDay[present - 1];
106106
return {
107-
content: `${h(dailyChange)}🔺`,
107+
content: `${h(dailyChange)}`,
108108
hAlign: 'right',
109109
};
110110
};
@@ -113,7 +113,7 @@ e.getOneWeekChange = ({ confirmedByDay }) => {
113113
const present = confirmedByDay.length - 1;
114114
const weeklyChange = confirmedByDay[present] - confirmedByDay[present - 7];
115115
return {
116-
content: `${h(weeklyChange)}🔺`,
116+
content: `${h(weeklyChange)}`,
117117
hAlign: 'right',
118118
};
119119
};
@@ -155,6 +155,9 @@ e.countryUpperCase = (countryParams) => {
155155

156156
e.footer = `
157157
158-
Source Code: https://github.com/sagarkarira/coronavirus-tracker-cli/
158+
Stay safe. Stay inside.
159+
160+
Code: https://github.com/sagarkarira/coronavirus-tracker-cli/
159161
Twitter: http://twitter.com/ekrysis
162+
160163
`;

0 commit comments

Comments
 (0)