Skip to content

Commit 0da6e06

Browse files
authored
Merge pull request sagarkarira#72 from sagarkarira/feat-graphs
added graphs, changed readme, added help api
2 parents 401b9af + 8f6d0aa commit 0da6e06

File tree

10 files changed

+207
-25
lines changed

10 files changed

+207
-25
lines changed

app.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
const express = require('express');
22
const morgan = require('morgan');
3-
4-
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
5-
const { getCompleteTable } = require('./lib/corona');
6-
const { lookupCountry } = require('./lib/helpers');
3+
const chalk = require('chalk');
4+
5+
const {
6+
getCountryTable,
7+
getJSONData,
8+
getJSONDataForCountry,
9+
} = require('./lib/byCountry');
10+
const { getCompleteTable, getGraph } = require('./lib/corona');
11+
const { lookupCountry, htmlTemplate } = require('./lib/helpers');
712
const { getLiveUpdates } = require('./lib/reddit.js');
813
const { getWorldoMetersTable } = require('./lib/worldoMeters.js');
14+
const { helpContent } = require('./lib/constants');
915

1016
const app = express();
1117
const port = process.env.PORT || 3001;
@@ -69,6 +75,40 @@ app.get('/updates', (req, res) => {
6975
}).catch(error => errorHandler(error, res));
7076
});
7177

78+
app.get(['/:country/graph', '/graph'], (req, res) => {
79+
const { country } = req.params;
80+
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
81+
if (!country) {
82+
return getGraph({ isCurl })
83+
.then(result => res.send(result))
84+
.catch(error => errorHandler(error, res));
85+
}
86+
const lookupObj = lookupCountry(country);
87+
88+
if (!lookupObj) {
89+
return res.send(`
90+
Country not found.
91+
Try the full country name or country code.
92+
Example:
93+
- /UK: for United Kingdom
94+
- /US: for United States of America.
95+
- /Italy: for Italy.
96+
`);
97+
}
98+
return getGraph({countryCode: lookupObj.iso2, isCurl })
99+
.then(result => res.send(result))
100+
.catch(error => errorHandler(error, res));
101+
});
102+
103+
app.get('/help', (req, res) => {
104+
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
105+
if (!isCurl) {
106+
return res.send(htmlTemplate(helpContent));
107+
}
108+
return res.send(chalk.green(helpContent));
109+
});
110+
111+
72112
app.get('/:country', (req, res) => {
73113
const { country } = req.params;
74114
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);

bin/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require('yargonaut').style('green');
44
const yargs = require('yargs');
55
const chalk = require('chalk');
6-
const { getCompleteTable } = require('../lib/corona');
6+
const { getCompleteTable, getGraph } = require('../lib/corona');
77
const { getCountryTable } = require('../lib/byCountry');
88
const { getWorldoMetersTable } = require('../lib/worldoMeters');
99
const { lookupCountry } = require('../lib/helpers');
@@ -62,22 +62,27 @@ const { argv } = yargs
6262
alias: 'top',
6363
describe: 'Filter table by rank',
6464
type: 'int'
65+
},
66+
g: {
67+
alias: 'graph',
68+
describe: 'Get graph',
69+
type: 'boolean',
70+
default: false,
6571
}
6672
})
6773
.strict()
6874
.help('help');
6975

7076
argv.countryCode = argv.country;
7177
if (argv.source === 2) {
72-
getWorldoMetersTable(argv)
73-
.then(console.log)
74-
.catch(console.error);
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);
7582
} else {
7683
(
7784
argv.country === 'ALL'
7885
? getCompleteTable(argv)
7986
: getCountryTable(argv)
80-
)
81-
.then(console.log)
82-
.catch(console.error);
87+
).then(console.log).catch(console.error);
8388
}

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.8.0
4+
5+
* Added confirmed cases graphs ``corona -g`` or ``corona italy -g`
6+
37
## Version 0.7.0
48

59
* Added new source to fetch realtime data. ``corona --source=2``

lib/byCountry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Table = require('cli-table3');
22
const _ = require('lodash');
33
const helpers = require('./helpers');
44
const api = require('./api');
5+
56
const {
67
extraStats,
78
getConfirmed,

lib/constants.js

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,88 @@ exports.countryNameMap = {
180180
'Dominican Republic': 'DO',
181181
'Burkina Faso': 'BF',
182182
Uzbekistan: 'UZ',
183-
};
183+
};
184+
185+
exports.helpContent = `
186+
187+
/$$ /$$ /$$$$$$$$ /$$ /$$$$$$$
188+
| $$ | $$| $$_____/| $$ | $$____$$
189+
| $$ | $$| $$ | $$ | $$ $$
190+
| $$$$$$$$| $$$$$ | $$ | $$$$$$$/
191+
| $$__ $$| $$__/ | $$ | $$____/
192+
| $$ | $$| $$ | $$ | $$
193+
| $$ | $$| $$$$$$$$| $$$$$$$$| $$
194+
|__/ |__/|________/|________/|__/
195+
196+
---------------------------------------------------------------------------------
197+
198+
# Source 1 stats - updated once a day from John Hopkins University
199+
https://corona-stats.online
200+
201+
---------------------------------------------------------------------------------
202+
203+
# Source 2 stats - updated every 15 minutes from worldometers.info
204+
https://corona-stats.online?source=2
205+
206+
---------------------------------------------------------------------------------
207+
208+
# Country wise stats
209+
210+
## Format:
211+
https://corona-stats.online/<countryCode>
212+
https://corona-stats.online/<countryName>
213+
214+
## Example: From source 1
215+
https://corona-stats.online/Italy
216+
https://corona-stats.online/UK
217+
218+
## Example: From source 2
219+
https://corona-stats.online/italy?source=2
220+
https://corona-stats.online/UK?source=2
221+
222+
---------------------------------------------------------------------------------
223+
224+
# Minimal Mode - remove the borders and padding from table
225+
226+
## Example:
227+
https://corona-stats.online?minimal=true
228+
https://corona-stats.online/Italy?minimal=true (with country filter)
229+
https://corona-stats.online?minimal=true&source=2 (with source)
230+
https://corona-stats.online/uk?source=2&minimal=true (with source and country)
231+
232+
---------------------------------------------------------------------------------
233+
234+
# Get data as JSON - Add ?json=true
235+
236+
## Example:
237+
https://corona-stats.online?json=true
238+
https://corona-stats.online/Italy?json=true (with country filter)
239+
https://corona-stats.online/?source=2&json=true (with source)
240+
https://corona-stats.online/uk?source=2&json=true (with source and country)
241+
242+
---------------------------------------------------------------------------------
243+
244+
# Get top N countries - Add ?top=N
245+
246+
## Example:
247+
https://corona-stats.online?top=25
248+
https://corona-stats.online?source=2&top=10 (with source)
249+
https://corona-stats.online/uk?minimal=true&top=20 (with minimal)
250+
251+
252+
---------------------------------------------------------------------------------
253+
254+
# Confirmed Cases Graph (WIP)
255+
256+
## Format:
257+
https://corona-stats.online/<countryName>/graph
258+
https://corona-stats.online/<countryCode>/graph
259+
260+
## Example:
261+
https://corona-stats.online/italy/graph
262+
https://corona-stats.online/china/graph
263+
264+
265+
------------- Any issues or feedback - Hit me up on twitter @ekrysis --------------
266+
267+
`;

lib/corona.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const Table = require('cli-table3');
22
const _ = require('lodash');
33
const helpers = require('./helpers');
4+
const asciichart = require('asciichart');
45
const api = require('./api');
6+
const chalk = require('chalk');
57

68
const {
79
extraStats,
@@ -66,6 +68,38 @@ function getDataByCountry(confirmed, deaths, recovered) {
6668
return _.sortBy(countryArr, (o) => -o.confirmed);
6769
}
6870

71+
exports.getGraph = async ({ countryCode = 'ALL', isCurl = true}) => {
72+
const data = await api.getCoronaData();
73+
const { confirmed, deaths, recovered } = data;
74+
const countryData = getDataByCountry(confirmed, deaths, recovered);
75+
const worldStats = getTotalStats(countryData);
76+
worldStats.countryCode = 'ALL';
77+
worldStats.countryName = 'World';
78+
countryData.push(worldStats);
79+
80+
const singleCountryData = countryData.filter(obj => obj.countryCode === countryCode);
81+
// const graphLength = ' ';
82+
const graphLength = ' Confirmed Cases Graph ';
83+
const padding = ' ';
84+
const graphConfig = {
85+
height: 30,
86+
offset: 2,
87+
padding,
88+
};
89+
const { confirmedByDay, } = singleCountryData[0];
90+
91+
const confirmedGraph = asciichart.plot(confirmedByDay, graphConfig);
92+
const body = chalk.greenBright(confirmedGraph)
93+
+ chalk.cyanBright('\n\n' +padding + '22 Feb' + graphLength + '22 Mar') + '\n';
94+
95+
if (!isCurl) {
96+
return htmlTemplate(body);
97+
}
98+
return body;
99+
100+
101+
};
102+
69103
exports.getCompleteTable = async ({
70104
isCurl = true,
71105
emojis = false,

lib/helpers.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ e.lookupCountry = country => {
172172

173173
e.footer = (lastUpdated) => `
174174
175-
⚠️ ${chalk.cyanBright('Stay safe. Stay inside.')}
175+
${chalk.cyanBright('Stay safe. Stay inside.')}
176+
${chalk.greenBright('Code')}: ${chalk.blueBright('https://github.com/sagarkarira/coronavirus-tracker-cli')}
177+
${chalk.greenBright('Twitter')}: ${chalk.blueBright('https://twitter.com/ekrysis')}
176178
177-
💻 ${chalk.greenBright('Code')}: ${chalk.blueBright('https://github.com/sagarkarira/coronavirus-tracker-cli')}
178-
➡️ ${chalk.greenBright('Twitter')}: ${chalk.blueBright('https://twitter.com/ekrysis')}
179+
${chalk.magentaBright('Last Updated on:')} ${moment(lastUpdated).utc().format('DD-MMM-YYYY HH:MM')} UTC
179180
180-
${chalk.magentaBright('Last Updated on:')} ${moment(lastUpdated).utc().format('DD-MMM-YYYY HH:MM')} UTC
181-
182-
${chalk.red.bold.underline('NEW UPDATE (REALTIME STATS)')}: ${chalk.blueBright('curl https://corona-stats.online?source=2')}
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')}
183183
`;
184184

185185
e.getTableBorders = minimal => {
@@ -214,7 +214,7 @@ e.getTableHeaders = (emojis, secondColumnName) => {
214214
`Recovered${emojis ? ' 😀' : ''}`,
215215
`Deaths${emojis ? ' 😞' : ''}`,
216216
`Active${emojis ? ' 😷' : ''}`,
217-
'Mortality %',
217+
'CFR %',
218218
'Recovered %',
219219
'1 Day ▲',
220220
'1 Week ▲',

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coronavirus-tracker-cli",
3-
"version": "0.7.0",
3+
"version": "0.8.0",
44
"description": "track conronavirus cases from cli",
55
"repository": {
66
"type": "git",
@@ -21,12 +21,17 @@
2121
"start": "node app.js",
2222
"test": "npm run lint"
2323
},
24+
"files": [
25+
"{bin,lib}/*.js",
26+
"app.js"
27+
],
2428
"engines": {
2529
"node": ">=12.x"
2630
},
2731
"author": "[email protected]",
2832
"license": "ISC",
2933
"dependencies": {
34+
"asciichart": "^1.5.11",
3035
"axios": "^0.19.2",
3136
"chalk": "^3.0.0",
3237
"cli-table3": "^0.5.1",

readme.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,11 @@ corona --color=false
146146
* [Japan](https://covid19japan.com/)
147147
* [Philippines](https://ncovtracker.doh.gov.ph/)
148148

149-
## Thanks to
149+
## Data Sources
150150

151-
* [CSSEGISandData](https://github.com/CSSEGISandData/COVID-19) for the data.
152-
* [ExpDev07](https://github.com/ExpDev07/coronavirus-tracker-api) for the API.
153-
* [Zeit Now](https://github.com/zeit/now) for hosting.
154-
* [https://github.com/NovelCOVID/API/](https://github.com/NovelCOVID/API/) for realtime stats API.
151+
* [John Hopkins Data](https://github.com/CSSEGISandData/COVID-19) updated once a day at 11:59 UTC
152+
* [John Hopkins Data API](https://github.com/ExpDev07/coronavirus-tracker-api)
153+
* [WorldoMeters Data API](https://github.com/NovelCOVID/API/) updated very frequently.
155154

156155
## Related Projects
157156

@@ -161,6 +160,11 @@ corona --color=false
161160
* <https://github.com/warengonzaga/covid19-tracker-cli>
162161
* <https://github.com/ahmadawais/corona-cli>
163162

163+
## Hosting
164+
165+
* Big shoutout to [Zeit Now](https://github.com/zeit/now).
166+
164167
## License
165168

166169
[WTFPL](http://www.wtfpl.net/)
170+

0 commit comments

Comments
 (0)