Skip to content

Commit f71ffdb

Browse files
committed
changed default source to 2 (worldometers)
1 parent b5f2f7e commit f71ffdb

File tree

9 files changed

+106
-71
lines changed

9 files changed

+106
-71
lines changed

app.js

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99
getJSONDataForCountry,
1010
} = require('./lib/byCountry');
1111
const { getCompleteTable, getGraph } = require('./lib/corona');
12-
const { lookupCountry, htmlTemplate } = require('./lib/helpers');
12+
const { lookupCountry, htmlTemplate, footer } = require('./lib/helpers');
1313
const { getLiveUpdates } = require('./lib/reddit.js');
1414
const { getWorldoMetersTable } = require('./lib/worldoMeters.js');
1515
const { helpContent, countryNotFound } = require('./lib/constants');
@@ -18,12 +18,17 @@ const app = express();
1818
const port = process.env.PORT || 3001;
1919
const IS_CURL_RE = /\bcurl\b/im;
2020

21-
function errorHandler(error, res) {
21+
function errorHandler(error, req, res) {
2222
console.error(error);
23-
return res.status(500).send(htmlTemplate(`
23+
const body = `
2424
I am sorry. Something went wrong. Please report it\n
2525
${error.message}
26-
`));
26+
${footer(new Date)}
27+
`;
28+
if (req.isCurl) {
29+
return body;
30+
}
31+
return res.status(500).send(htmlTemplate(body));
2732
}
2833

2934
app.set('json escape', true);
@@ -47,58 +52,60 @@ app.use(helmet.referrerPolicy({ policy: 'strict-origin-when-cross-origin' }));
4752
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
4853
app.use((req, res, next) => {
4954
res.setHeader('Cache-Control', 'no-cache');
55+
req.isCurl = IS_CURL_RE.test(req.headers['user-agent']);
5056
next();
5157
});
5258

5359
app.get('/', (req, res) => {
54-
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
60+
const isCurl = req.isCurl;
5561
const format = req.query.format ? req.query.format : '';
5662
const minimal = req.query.minimal === 'true';
5763
const emojis = req.query.emojis === 'true';
5864
const top = req.query.top ? Number(req.query.top) : 1000;
59-
const source = req.query.source ? Number(req.query.source) : 1;
65+
const source = req.query.source ? Number(req.query.source) : 2;
66+
67+
if (source === 1) {
68+
if (format.toLowerCase() === 'json') {
69+
return getJSONData().then(result => {
70+
return res.json(result);
71+
}).catch(error => errorHandler(error, req, res));
72+
}
6073

61-
if (source === 2) {
62-
return getWorldoMetersTable({ isCurl, emojis, minimal, top, format})
74+
return getCompleteTable({ isCurl, emojis, minimal, top })
6375
.then(result => {
6476
return res.send(result);
65-
}).catch(error => errorHandler(error, res));
77+
}).catch(error => errorHandler(error, req, res));
6678
}
6779

68-
if (format.toLowerCase() === 'json') {
69-
return getJSONData().then(result => {
70-
return res.json(result);
71-
}).catch(error => errorHandler(error, res));
72-
}
73-
74-
return getCompleteTable({ isCurl, emojis, minimal, top })
80+
return getWorldoMetersTable({ isCurl, emojis, minimal, top, format})
7581
.then(result => {
7682
return res.send(result);
77-
}).catch(error => errorHandler(error, res));
83+
}).catch(error => errorHandler(error, req, res));
84+
7885
});
7986

8087
app.get('/updates', (req, res) => {
81-
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
88+
const isCurl = req.isCurl;
8289
const format = req.query.format ? req.query.format : '';
8390

8491
if (format.toLowerCase() === 'json') {
8592
return getLiveUpdates({ json: true, isCurl }).then(result => {
8693
return res.json(result);
87-
}).catch(error => errorHandler(error, res));
94+
}).catch(error => errorHandler(error, req, res));
8895
}
8996

9097
return getLiveUpdates({ json: false, isCurl }).then(result => {
9198
return res.send(result);
92-
}).catch(error => errorHandler(error, res));
99+
}).catch(error => errorHandler(error, req, res));
93100
});
94101

95102
app.get(['/:country/graph', '/graph'], (req, res) => {
96103
const { country } = req.params;
97-
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
104+
const isCurl = req.isCurl;
98105
if (!country) {
99106
return getGraph({ isCurl })
100107
.then(result => res.send(result))
101-
.catch(error => errorHandler(error, res));
108+
.catch(error => errorHandler(error, req, res));
102109
}
103110
const lookupObj = lookupCountry(country);
104111

@@ -107,11 +114,11 @@ app.get(['/:country/graph', '/graph'], (req, res) => {
107114
}
108115
return getGraph({countryCode: lookupObj.iso2, isCurl })
109116
.then(result => res.send(result))
110-
.catch(error => errorHandler(error, res));
117+
.catch(error => errorHandler(error, req, res));
111118
});
112119

113120
app.get('/help', (req, res) => {
114-
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
121+
const isCurl = req.isCurl;
115122
if (!isCurl) {
116123
return res.send(htmlTemplate(helpContent));
117124
}
@@ -121,23 +128,41 @@ app.get('/help', (req, res) => {
121128

122129
app.get('/:country', (req, res) => {
123130
const { country } = req.params;
124-
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
131+
const isCurl = req.isCurl;
125132
const format = req.query.format ? req.query.format : '';
126133
const minimal = req.query.minimal === 'true';
127134
const emojis = req.query.emojis === 'true';
128-
const source = req.query.source ? Number(req.query.source) : 1;
135+
const source = req.query.source ? Number(req.query.source) : 2;
129136

130137
if (!country || country.toUpperCase() === 'ALL') {
131138
if (format.toLowerCase() === 'json') {
132-
return getJSONData().then(result => {
139+
return getWorldoMetersTable({ isCurl, emojis, minimal, format }).then(result => {
133140
return res.json(result);
134-
}).catch(error => errorHandler(error, res));
141+
}).catch(error => errorHandler(error, req, res));
142+
}
143+
144+
return getWorldoMetersTable({ isCurl, emojis, minimal })
145+
.then(result => {
146+
return res.send(result);
147+
}).catch(error => errorHandler(error, req, res));
148+
}
149+
if (source === 1) {
150+
const lookupObj = lookupCountry(country);
151+
152+
if (!lookupObj) {
153+
return res.status(404).send(countryNotFound(isCurl));
135154
}
155+
const { iso2 } = lookupObj;
136156

137-
return getCompleteTable({ isCurl, emojis, minimal })
157+
if (format.toLowerCase() === 'json') {
158+
return getJSONDataForCountry(iso2).then(result => {
159+
return res.json(result);
160+
}).catch(error => errorHandler(error, req, res));
161+
}
162+
return getCountryTable({ countryCode: iso2, isCurl, emojis, minimal })
138163
.then(result => {
139164
return res.send(result);
140-
}).catch(error => errorHandler(error, res));
165+
}).catch(error => errorHandler(error, req, res));
141166
}
142167

143168
const lookupObj = lookupCountry(country);
@@ -146,26 +171,12 @@ app.get('/:country', (req, res) => {
146171
return res.status(404).send(countryNotFound(isCurl));
147172
}
148173

149-
150174
const { iso2 } = lookupObj;
151175

152-
if (source === 2) {
153-
return getWorldoMetersTable({ countryCode: iso2, isCurl, emojis, minimal, format })
154-
.then(result => {
155-
return res.send(result);
156-
}).catch(error => errorHandler(error, res));
157-
}
158-
159-
if (format.toLowerCase() === 'json') {
160-
return getJSONDataForCountry(iso2).then(result => {
161-
return res.json(result);
162-
}).catch(error => errorHandler(error, res));
163-
}
164-
165-
return getCountryTable({ countryCode: iso2, isCurl, emojis, minimal })
176+
return getWorldoMetersTable({ countryCode: iso2, isCurl, emojis, minimal, format })
166177
.then(result => {
167178
return res.send(result);
168-
}).catch(error => errorHandler(error, res));
179+
}).catch(error => errorHandler(error, req, res));
169180
});
170181

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

bin/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const { argv } = yargs
3838
s: {
3939
alias: 'source',
4040
describe: 'fetch data from other source',
41-
default: 1,
41+
default: 2,
4242
type: 'int'
4343
},
4444
e: {
@@ -74,15 +74,15 @@ const { argv } = yargs
7474
.help('help');
7575

7676
argv.countryCode = argv.country;
77-
if (argv.source === 2) {
78-
getWorldoMetersTable(argv).then(console.log).catch(console.error);
79-
}
80-
else if (argv.graph === true) {
81-
getGraph(argv.countryCode).then(console.log).catch(console.error);
82-
} else {
77+
if (argv.source === 1) {
8378
(
8479
argv.country === 'ALL'
8580
? getCompleteTable(argv)
8681
: getCountryTable(argv)
8782
).then(console.log).catch(console.error);
8883
}
84+
else if (argv.graph === true) {
85+
getGraph(argv.countryCode).then(console.log).catch(console.error);
86+
} else {
87+
getWorldoMetersTable(argv).then(console.log).catch(console.error);
88+
}

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.0
4+
5+
* Changed default source to worldoMeters. i.e source 2 is now default
6+
37
## Version 0.8.0
48

59
* Added confirmed cases graphs `corona -g` or `corona italy -g`

lib/api.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ exports.getWorldoMetersData = async (countryCode = 'ALL') => {
7373
critical: 0,
7474
});
7575

76-
result.data.forEach(obj => obj.countryCode = countryNameMap[obj.country]);
76+
result.data.forEach(obj => {
77+
obj.countryCode = countryNameMap[obj.country];
78+
obj.confirmed = obj.cases;
79+
});
7780
worldStats.casesPerOneMillion = (worldStats.cases / 7794).toFixed(2);
81+
worldStats.confirmed = worldStats.cases;
7882
let finalData = result.data;
7983
if (countryCode && countryCode !== 'ALL') {
8084
finalData = finalData.filter(obj => obj.countryCode === countryCode);

lib/constants.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { htmlTemplate } = require('./helpers');
1+
const { htmlTemplate, footer } = require('./helpers');
22
exports.countryNameMap = {
33
China: 'CN',
44
UK: 'GB',
@@ -181,6 +181,20 @@ exports.countryNameMap = {
181181
'Dominican Republic': 'DO',
182182
'Burkina Faso': 'BF',
183183
Uzbekistan: 'UZ',
184+
'Madagascar': 'MG',
185+
'Uganda': 'UG',
186+
'Zimbabwe': 'ZW',
187+
'Dominica': 'DM',
188+
'Laos': 'LA',
189+
'Myanmar': 'MM',
190+
'Belize': 'BZ',
191+
'Eritrea': 'ER',
192+
'Grenada': 'GD',
193+
'Mozambique': 'MZ',
194+
'Papua New Guinea': 'PG',
195+
'Syria': 'SY',
196+
'Timor-Leste': 'TL',
197+
'Turks and Caicos': 'TC'
184198
};
185199

186200
exports.helpContent = `
@@ -201,6 +215,7 @@ https://corona-stats.online
201215
202216
---------------------------------------------------------------------------------
203217
218+
(DEFAULT SOURCE)
204219
# Source 2 stats - updated every 15 minutes from worldometers.info
205220
https://corona-stats.online?source=2
206221
@@ -213,12 +228,14 @@ https://corona-stats.online/[countryCode]
213228
https://corona-stats.online/[countryName]
214229
215230
## Example: From source 1
216-
https://corona-stats.online/Italy
217-
https://corona-stats.online/UK
231+
https://corona-stats.online/Italy?source=1
232+
https://corona-stats.online/UK?source=1
218233
219-
## Example: From source 2
234+
## Example: From source 2 (DEFAULT)
235+
https://corona-stats.online/italy
220236
https://corona-stats.online/italy?source=2
221237
https://corona-stats.online/UK?source=2
238+
https://corona-stats.online/UK
222239
223240
---------------------------------------------------------------------------------
224241
@@ -227,7 +244,7 @@ https://corona-stats.online/UK?source=2
227244
## Example:
228245
https://corona-stats.online?minimal=true
229246
https://corona-stats.online/Italy?minimal=true (with country filter)
230-
https://corona-stats.online?minimal=true&source=2 (with source)
247+
https://corona-stats.online?minimal=true&source=1 (with source)
231248
https://corona-stats.online/uk?source=2&minimal=true (with source and country)
232249
233250
---------------------------------------------------------------------------------
@@ -246,7 +263,7 @@ https://corona-stats.online/uk?source=2&json=true (with source and countr
246263
247264
## Example:
248265
https://corona-stats.online?top=25
249-
https://corona-stats.online?source=2&top=10 (with source)
266+
https://corona-stats.online?source=1&top=10 (with source)
250267
https://corona-stats.online/uk?minimal=true&top=20 (with minimal)
251268
252269
@@ -275,6 +292,8 @@ exports.countryNotFound = (isCurl) => {
275292
- /UK: for United Kingdom
276293
- /US: for United States of America.
277294
- /Italy: for Italy.
295+
296+
${footer(new Date)}
278297
`;
279298
return isCurl ? body : htmlTemplate(body);
280299
};

lib/helpers.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ ${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('NEW REALTIME UPDATES')}: ${chalk.blueBright('https://corona-stats.online?source=2')}
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')}
175176
${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')}
177177
`;
178178

179179
e.getTableBorders = minimal => {
@@ -235,9 +235,7 @@ e.extraStats = (dataArr) => {
235235
return dataArr.map(obj => {
236236
return {
237237
...obj,
238-
active: e.calActive(obj),
239238
mortalityPer: e.calMortalityPer(obj),
240-
recoveredPer: e.calRecoveredPer(obj)
241239
};
242240
});
243241
};

lib/worldoMeters.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ exports.getWorldoMetersTable = async ({
1919
style: helpers.getTableStyles(minimal),
2020
});
2121
const { data, worldStats } = await api.getWorldoMetersData(countryCode);
22-
console.log(format);
2322
if (format === 'json') {
2423
return { data, worldStats };
2524
}
@@ -28,7 +27,7 @@ exports.getWorldoMetersTable = async ({
2827
data.some(cd => {
2928
const countryEmoji = getEmoji(cd.countryCode) || '🏳️';
3029
const values = [
31-
cFormatter(cd.country, chalk.cyanBright),
30+
cFormatter(`${cd.country} (${cd.countryCode})` , chalk.cyanBright),
3231
cFormatter(cd.cases, chalk.green, 'right', true),
3332
cFormatter(cd.todayCases, chalk.cyanBright, 'right', true, ' ▲'),
3433
cFormatter(cd.deaths, chalk.whiteBright, 'right', true),

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.8.0",
3+
"version": "0.9.0",
44
"description": "track conronavirus cases from cli",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)