Skip to content

Commit b5f2f7e

Browse files
committed
fixing JSU api issue, adding status code in error and country not found
1 parent de10c01 commit b5f2f7e

File tree

5 files changed

+38
-77
lines changed

5 files changed

+38
-77
lines changed

app.js

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

1717
const app = express();
1818
const port = process.env.PORT || 3001;
1919
const IS_CURL_RE = /\bcurl\b/im;
2020

2121
function errorHandler(error, res) {
2222
console.error(error);
23-
return res.send(`
23+
return res.status(500).send(htmlTemplate(`
2424
I am sorry. Something went wrong. Please report it\n
2525
${error.message}
26-
`);
26+
`));
2727
}
2828

2929
app.set('json escape', true);
@@ -56,7 +56,7 @@ app.get('/', (req, res) => {
5656
const minimal = req.query.minimal === 'true';
5757
const emojis = req.query.emojis === 'true';
5858
const top = req.query.top ? Number(req.query.top) : 1000;
59-
const source = req.query.source ? Number(req.query.source) : 2;
59+
const source = req.query.source ? Number(req.query.source) : 1;
6060

6161
if (source === 2) {
6262
return getWorldoMetersTable({ isCurl, emojis, minimal, top, format})
@@ -103,14 +103,7 @@ app.get(['/:country/graph', '/graph'], (req, res) => {
103103
const lookupObj = lookupCountry(country);
104104

105105
if (!lookupObj) {
106-
return res.send(`
107-
Country not found.
108-
Try the full country name or country code.
109-
Example:
110-
- /UK: for United Kingdom
111-
- /US: for United States of America.
112-
- /Italy: for Italy.
113-
`);
106+
return res.status(404).send(countryNotFound(isCurl));
114107
}
115108
return getGraph({countryCode: lookupObj.iso2, isCurl })
116109
.then(result => res.send(result))
@@ -132,7 +125,7 @@ app.get('/:country', (req, res) => {
132125
const format = req.query.format ? req.query.format : '';
133126
const minimal = req.query.minimal === 'true';
134127
const emojis = req.query.emojis === 'true';
135-
const source = req.query.source ? Number(req.query.source) : 2;
128+
const source = req.query.source ? Number(req.query.source) : 1;
136129

137130
if (!country || country.toUpperCase() === 'ALL') {
138131
if (format.toLowerCase() === 'json') {
@@ -150,14 +143,7 @@ app.get('/:country', (req, res) => {
150143
const lookupObj = lookupCountry(country);
151144

152145
if (!lookupObj) {
153-
return res.send(`
154-
Country not found.
155-
Try the full country name or country code.
156-
Example:
157-
- /UK: for United Kingdom
158-
- /US: for United States of America.
159-
- /Italy: for Italy.
160-
`);
146+
return res.status(404).send(countryNotFound(isCurl));
161147
}
162148

163149

lib/byCountry.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ const api = require('./api');
66
const {
77
extraStats,
88
getConfirmed,
9-
getActive,
109
getDeaths,
11-
getRecovered,
1210
getMortalityPer,
13-
getRecoveredPer,
1411
getEmoji,
1512
getOneDayChange,
1613
getOneWeekChange,
@@ -19,11 +16,10 @@ const {
1916
htmlTemplate,
2017
} = require('./helpers');
2118

22-
function getDataByState(confirmed, deaths, recovered) {
19+
function getDataByState(confirmed, deaths) {
2320
const countryMap = {};
2421
const lastUpdated = confirmed.last_updated;
2522
const confirmedMap = _.keyBy(confirmed.locations, (i) => i.country + i.province);
26-
const recoveredMap = _.keyBy(recovered.locations, (i) => i.country + i.province);
2723
const deathsMap = _.keyBy(deaths.locations, (i) => i.country + i.province);
2824
confirmed.locations.forEach(obj => {
2925
const countryName = obj.country;
@@ -35,10 +31,8 @@ function getDataByState(confirmed, deaths, recovered) {
3531
province: provinceName,
3632
countryCode: obj.country_code,
3733
confirmed: confirmedMap[mapKey].latest,
38-
recovered: recoveredMap[mapKey].latest,
3934
deaths: deathsMap[mapKey].latest,
4035
confirmedByDay: helpers.historyObjToArr(confirmedMap[mapKey].history),
41-
recoveredByDay: helpers.historyObjToArr(recoveredMap[mapKey].history),
4236
deathsByDay: helpers.historyObjToArr(deathsMap[mapKey].history),
4337
lastUpdated,
4438
};
@@ -52,17 +46,17 @@ function getDataByState(confirmed, deaths, recovered) {
5246

5347
exports.getJSONData = async () => {
5448
const data = await api.getCoronaData();
55-
const { confirmed, deaths, recovered } = data;
56-
const countryData = getDataByState(confirmed, deaths, recovered);
49+
const { confirmed, deaths } = data;
50+
const countryData = getDataByState(confirmed, deaths);
5751
const totalStats = getTotalStats(countryData);
5852
totalStats.country = 'World';
5953
return countryData.concat(totalStats);
6054
};
6155

6256
exports.getJSONDataForCountry = async (countryCode) => {
6357
const data = await api.getCoronaData();
64-
const { confirmed, deaths, recovered } = data;
65-
const countryData = getDataByState(confirmed, deaths, recovered)
58+
const { confirmed, deaths } = data;
59+
const countryData = getDataByState(confirmed, deaths)
6660
.filter(obj => obj.countryCode === countryCode);
6761
return countryData;
6862
};
@@ -79,8 +73,8 @@ exports.getCountryTable = async ({
7973
style: helpers.getTableStyles(minimal),
8074
});
8175
const data = await api.getCoronaData();
82-
const { confirmed, deaths, recovered } = data;
83-
const countryData = getDataByState(confirmed, deaths, recovered)
76+
const { confirmed, deaths } = data;
77+
const countryData = getDataByState(confirmed, deaths)
8478
.filter(obj => obj.countryCode === countryCode);
8579

8680
if (countryData.length === 0) {
@@ -92,11 +86,8 @@ exports.getCountryTable = async ({
9286
[countryData[0].country]: [
9387
'Total',
9488
getConfirmed(totalStats.confirmed),
95-
getRecovered(totalStats.recovered),
9689
getDeaths(totalStats.deaths),
97-
getActive(totalStats.active),
9890
getMortalityPer(totalStats.mortalityPer),
99-
getRecoveredPer(totalStats.recoveredPer),
10091
getOneDayChange(totalStats),
10192
getOneWeekChange(totalStats),
10293
]
@@ -109,11 +100,8 @@ exports.getCountryTable = async ({
109100
const values = [
110101
cd.province,
111102
getConfirmed(cd.confirmed),
112-
getRecovered(cd.recovered),
113103
getDeaths(cd.deaths),
114-
getActive(cd.active),
115104
getMortalityPer(cd.mortalityPer),
116-
getRecoveredPer(cd.recoveredPer),
117105
getOneDayChange(cd),
118106
getOneWeekChange(cd),
119107
...(emojis ? [countryEmoji] : [])

lib/constants.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { htmlTemplate } = require('./helpers');
12
exports.countryNameMap = {
23
China: 'CN',
34
UK: 'GB',
@@ -264,4 +265,16 @@ https://corona-stats.online/china/graph
264265
265266
------------- Any issues or feedback - Hit me up on twitter @ekrysis --------------
266267
267-
`;
268+
`;
269+
270+
exports.countryNotFound = (isCurl) => {
271+
const body = `
272+
Country not found.
273+
Try the full country name or country code.
274+
Example:
275+
- /UK: for United Kingdom
276+
- /US: for United States of America.
277+
- /Italy: for Italy.
278+
`;
279+
return isCurl ? body : htmlTemplate(body);
280+
};

lib/corona.js

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ const {
99
extraStats,
1010
getCountry,
1111
getConfirmed,
12-
getActive,
1312
getDeaths,
14-
getRecovered,
1513
getMortalityPer,
16-
getRecoveredPer,
1714
getEmoji,
1815
getOneDayChange,
1916
getOneWeekChange,
@@ -22,11 +19,10 @@ const {
2219
htmlTemplate,
2320
} = require('./helpers');
2421

25-
function getDataByCountry(confirmed, deaths, recovered) {
22+
function getDataByCountry(confirmed, deaths) {
2623
const countryMap = {};
2724
const lastUpdated = confirmed.last_updated;
2825
const confirmedMap = _.keyBy(confirmed.locations, (i) => i.country + i.province);
29-
const recoveredMap = _.keyBy(recovered.locations, (i) => i.country + i.province);
3026
const deathsMap = _.keyBy(deaths.locations, (i) => i.country + i.province);
3127
confirmed.locations.forEach(obj => {
3228
const countryName = obj.country;
@@ -37,25 +33,18 @@ function getDataByCountry(confirmed, deaths, recovered) {
3733
country: countryName,
3834
countryCode: obj.country_code,
3935
confirmed: confirmedMap[mapKey].latest,
40-
recovered: recoveredMap[mapKey].latest,
4136
deaths: deathsMap[mapKey].latest,
4237
confirmedByDay: helpers.historyObjToArr(confirmedMap[mapKey].history),
43-
recoveredByDay: helpers.historyObjToArr(recoveredMap[mapKey].history),
4438
deathsByDay: helpers.historyObjToArr(deathsMap[mapKey].history),
4539
lastUpdated,
4640
};
4741
} else {
4842
countryMap[countryName].confirmed += confirmedMap[mapKey].latest;
49-
countryMap[countryName].recovered += recoveredMap[mapKey].latest;
5043
countryMap[countryName].deaths += deathsMap[mapKey].latest;
5144
countryMap[countryName].confirmedByDay = helpers.addArr(
5245
countryMap[countryName].confirmedByDay,
5346
helpers.historyObjToArr(confirmedMap[mapKey].history)
5447
);
55-
countryMap[countryName].recoveredByDay = helpers.addArr(
56-
countryMap[countryName].recoveredByDay,
57-
helpers.historyObjToArr(recoveredMap[mapKey].history)
58-
);
5948
countryMap[countryName].deathsByDay = helpers.addArr(
6049
countryMap[countryName].deathsByDay,
6150
helpers.historyObjToArr(deathsMap[mapKey].history)
@@ -70,14 +59,13 @@ function getDataByCountry(confirmed, deaths, recovered) {
7059

7160
exports.getGraph = async ({ countryCode = 'ALL', isCurl = true}) => {
7261
const data = await api.getCoronaData();
73-
const { confirmed, deaths, recovered } = data;
74-
const countryData = getDataByCountry(confirmed, deaths, recovered);
62+
const { confirmed, deaths, } = data;
63+
const countryData = getDataByCountry(confirmed, deaths,);
7564
const worldStats = getTotalStats(countryData);
7665
worldStats.countryCode = 'ALL';
7766
worldStats.countryName = 'World';
7867
countryData.push(worldStats);
7968

80-
const singleCountryData = countryData.filter(obj => obj.countryCode === countryCode);
8169
// const graphLength = ' ';
8270
const graphLength = ' Confirmed Cases Graph ';
8371
const padding = ' ';
@@ -86,7 +74,8 @@ exports.getGraph = async ({ countryCode = 'ALL', isCurl = true}) => {
8674
offset: 2,
8775
padding,
8876
};
89-
const { confirmedByDay, } = singleCountryData[0];
77+
const singleCountryData = countryData.filter(obj => obj.countryCode === countryCode);
78+
const { confirmedByDay } = singleCountryData[0];
9079

9180
const confirmedGraph = asciichart.plot(confirmedByDay, graphConfig);
9281
const body = chalk.greenBright(confirmedGraph)
@@ -112,17 +101,14 @@ exports.getCompleteTable = async ({
112101
style: helpers.getTableStyles(minimal),
113102
});
114103
const data = await api.getCoronaData();
115-
const { confirmed, deaths, recovered } = data;
116-
const countryData = getDataByCountry(confirmed, deaths, recovered);
104+
const { confirmed, deaths, } = data;
105+
const countryData = getDataByCountry(confirmed, deaths);
117106
const worldStats = getTotalStats(countryData);
118107
const worldRow = [
119108
'World',
120109
getConfirmed(worldStats.confirmed),
121-
getRecovered(worldStats.recovered),
122110
getDeaths(worldStats.deaths),
123-
getActive(worldStats.active),
124111
getMortalityPer(worldStats.mortalityPer),
125-
getRecoveredPer(worldStats.recoveredPer),
126112
getOneDayChange(worldStats),
127113
getOneWeekChange(worldStats),
128114
...(emojis ? ['🌎'] : [])
@@ -135,11 +121,8 @@ exports.getCompleteTable = async ({
135121
const values = [
136122
getCountry(`${cd.country} (${cd.countryCode})`),
137123
getConfirmed(cd.confirmed),
138-
getRecovered(cd.recovered),
139124
getDeaths(cd.deaths),
140-
getActive(cd.active),
141125
getMortalityPer(cd.mortalityPer),
142-
getRecoveredPer(cd.recoveredPer),
143126
getOneDayChange(cd),
144127
getOneWeekChange(cd),
145128
...(emojis ? [countryEmoji] : [])

lib/helpers.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,18 @@ e.getOneWeekChange = ({ confirmedByDay }) => {
128128
e.getTotalStats = (countryData) => {
129129
const worldStats = countryData.reduce((acc, countryObj) => {
130130
acc.confirmed += countryObj.confirmed;
131-
acc.recovered += countryObj.recovered;
132131
acc.deaths += countryObj.deaths;
133132
acc.confirmedByDay = e.addArr(acc.confirmedByDay, countryObj.confirmedByDay);
134-
acc.recoveredByDay = e.addArr(acc.recoveredByDay, countryObj.recoveredByDay);
135133
acc.deathsByDay = e.addArr(acc.deathsByDay, countryObj.deathsByDay);
136134
return acc;
137135
}, {
138136
confirmed: 0,
139-
recovered: 0,
140137
deaths: 0,
141138
confirmedByDay: [],
142-
recoveredByDay: [],
143139
deathsByDay: [],
144140
});
145141

146-
worldStats.active = e.calActive(worldStats);
147-
worldStats.recoveredPer = e.calRecoveredPer(worldStats);
148142
worldStats.mortalityPer = e.calMortalityPer(worldStats);
149-
150143
return worldStats;
151144
};
152145

@@ -178,8 +171,9 @@ ${chalk.greenBright('Twitter')}: ${chalk.blueBright('https://twitter.com/ekrysis
178171
179172
${chalk.magentaBright('Last Updated on:')} ${moment(lastUpdated).utc().format('DD-MMM-YYYY HH:MM')} UTC
180173
181-
${chalk.red.bold.underline('NEW REALTIME UPDATES')}: ${chalk.blueBright('https://corona-stats.online?source=2')}
182-
${chalk.red.bold.underline('HELP')}: ${chalk.blueBright('https://corona-stats.online/help')}
174+
${chalk.red.bold('NEW REALTIME UPDATES')}: ${chalk.blueBright('https://corona-stats.online?source=2')}
175+
${chalk.red.bold('HELP')}: ${chalk.blueBright('https://corona-stats.online/help')}
176+
${chalk.bgBlueBright.bold('API was down because JHU has removed recovered from their data :( ')}: ${chalk.blueBright('https://github.com/CSSEGISandData/COVID-19/issues/1250')}
183177
`;
184178

185179
e.getTableBorders = minimal => {
@@ -211,11 +205,8 @@ e.getTableHeaders = (emojis, secondColumnName) => {
211205
'Rank',
212206
secondColumnName,
213207
`Confirmed ${emojis ? ' ✅' : ''}`,
214-
`Recovered${emojis ? ' 😀' : ''}`,
215208
`Deaths${emojis ? ' 😞' : ''}`,
216-
`Active${emojis ? ' 😷' : ''}`,
217209
'CFR %',
218-
'Recovered %',
219210
'1 Day ▲',
220211
'1 Week ▲',
221212
...(emojis ? ['🏳'] : []),

0 commit comments

Comments
 (0)