Skip to content

Commit 1ef6806

Browse files
author
Waren Gonzaga
committed
Add by country route
1 parent 1864ef3 commit 1ef6806

File tree

4 files changed

+111
-325
lines changed

4 files changed

+111
-325
lines changed

app.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
// node modules
12
const express = require('express'),
23
app = express(),
34
util = require('./bin/util'),
45
fs = require('fs'),
6+
axios = require('axios'),
57
covid19 = require('./lib/cli');
68

79
// set port
@@ -10,16 +12,48 @@ const port = process.env.port || 7070;
1012
// package.json info
1113
const pkg = JSON.parse(fs.readFileSync('package.json'));
1214

15+
// api base url
16+
const apiBaseURL = "https://corona.lmao.ninja";
17+
1318
// global route for covid19 tracker
1419
app.get('/', async (req, res, next) => {
15-
const userAgent = req.headers['user-agent'];
20+
const userAgent = req.headers['user-agent'],
21+
api = await axios.get(`${apiBaseURL}/all`),
22+
data = api.data;
1623
if (util.isCommandline(userAgent)) {
17-
await res.send(covid19.covid19globaltracker());
24+
await res.send(covid19.covid19globaltracker(
25+
data.cases, data.deaths,
26+
data.recovered, data.updated
27+
));
1828
return null;
1929
}
2030
return next();
2131
});
2232

23-
app.get('*', (req, res) => res.send(`Something went wrong? Contact the developer!\n`));
33+
// by country route for covid19 tracker
34+
app.get('/:country', async (req, res, next) => {
35+
const userAgent = req.headers['user-agent'],
36+
countryData = req.params.country,
37+
api = await axios.get(`${apiBaseURL}/countries/${countryData}`),
38+
all = await axios.get(`${apiBaseURL}/all`),
39+
u = all.data,
40+
d = api.data;
41+
if (util.isCommandline(userAgent)) {
42+
await res.send(covid19.covid19countrytracker(
43+
d.country, d.cases, d.todayCases,
44+
d.deaths, d.todayDeaths, d.recovered,
45+
d.active, d.critical, d.casesPerOneMillion,
46+
u.updated
47+
));
48+
return null;
49+
}
50+
return next();
51+
});
52+
53+
app.get('*', (req, res) => res.send(`
54+
Sorry, CLI version is only available at the moment...
55+
\n
56+
Try curl https://covid19tracker.xyz
57+
\n`));
2458

2559
app.listen(port, () => console.log(`COVID-19 Tracker v${pkg.version} is listening on port ${port}!`));

lib/cli/index.js

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/* eslint-disable no-await-in-loop */
22

33
const style = require('ansi-styles'),
4-
request = require("request"),
54
fs = require('fs'),
65
table3 = require('cli-table3');
76

7+
const axios = require('axios');
8+
89
const tblopn = style.cyan.open,
910
tblcls = style.cyan.close;
1011

12+
const apiBaseURL = "https://corona.lmao.ninja";
13+
1114
// package.json information
1215
const pkg = JSON.parse(fs.readFileSync('package.json'));
1316

@@ -24,20 +27,9 @@ let ts = Date.now(),
2427
year = date_ob.getFullYear(),
2528
currentdate = month + "/" + date + "/" + year;
2629

27-
// api request for global data
28-
request.get("https://corona.lmao.ninja/all", (error, response, body) => {
29-
if(error) {
30-
return console.dir(error);
31-
}
32-
globalData = JSON.parse(body);
33-
});
34-
3530
// covid19 global tracker
36-
exports.covid19globaltracker = () => {
37-
const cases = globalData.cases,
38-
deaths = globalData.deaths,
39-
recovered = globalData.recovered,
40-
asof = new Date(globalData.updated),
31+
exports.covid19globaltracker = (c, d, r, u) => {
32+
const cases = c, deaths = d, recovered = r, asof = new Date(u),
4133
table = new table3({
4234
head: [{colSpan:3,content:`${style.white.open}COVID-19 Tracker CLI v`+pkg.version+` - Global Update${style.white.close}`}],
4335
chars: { 'top': `${tblopn}${tblcls}`,
@@ -67,12 +59,56 @@ exports.covid19globaltracker = () => {
6759
return table.toString()+'\n'+footer;
6860
};
6961

62+
const lastupdate = async () => {
63+
return await axios.get(`${apiBaseURL}/all`).then(function(res){
64+
const update = res.data.updated;
65+
return update.toLocaleString();
66+
});
67+
};
68+
69+
exports.covid19countrytracker = (n, c, tC, d, tD, r, a, cl, cPOM, u) => {
70+
const name = n, cases = c, todayCases = tC,
71+
deaths = d, todayDeaths = tD, recovered = r,
72+
active = a, critical = cl, casesPerOneMillion = cPOM,
73+
asof = new Date(u);
74+
table = new table3({
75+
head: [{colSpan:4,content:`${style.white.open}COVID-19 Tracker CLI v`+pkg.version+` - ${name} Update${style.white.close}`}],
76+
chars: { 'top': `${tblopn}${tblcls}`,
77+
'top-mid': `${tblopn}${tblcls}`,
78+
'top-left': `${tblopn}${tblcls}`,
79+
'top-right': `${tblopn}${tblcls}`,
80+
'bottom': `${tblopn}${tblcls}`,
81+
'bottom-mid': `${tblopn}${tblcls}`,
82+
'bottom-left': `${tblopn}${tblcls}`,
83+
'bottom-right': `${tblopn}${tblcls}`,
84+
'left': `${tblopn}${tblcls}` ,
85+
'left-mid': `${tblopn}${tblcls}` ,
86+
'mid': `${tblopn}${tblcls}` ,
87+
'mid-mid': `${tblopn}${tblcls}`,
88+
'right': `${tblopn}${tblcls}` ,
89+
'right-mid': `${tblopn}${tblcls}` ,
90+
'middle': `${tblopn}${tblcls}` }
91+
});
92+
93+
table.push(
94+
[{colSpan:4,content:style.gray.open+'As of '+asof.toLocaleString()+' [Date:'+currentdate+']'+style.gray.close}],
95+
[`${style.magenta.open}Cases${style.magenta.close}`, `${style.red.open}Deaths${style.red.close}`, `${style.green.open}Recovered${style.green.close}`,`${style.cyan.open}Active${style.cyan.close}`],
96+
[formatNumber(cases), formatNumber(deaths), formatNumber(recovered), formatNumber(active)],
97+
[`${style.magentaBright.open}Today Cases${style.magentaBright.close}`, `${style.redBright.open}Today Deaths${style.red.close}`, `${style.redBright.open}Critical${style.redBright.close}`,`${style.cyanBright.open}Cases Per Million${style.cyanBright.close}`],
98+
[formatNumber(todayCases), formatNumber(todayDeaths), formatNumber(critical), formatNumber(casesPerOneMillion)],
99+
[{colSpan:4,content:style.gray.open+`Source: https://www.worldometers.info/coronavirus/`+style.gray.close}],
100+
[{colSpan:4,content:style.gray.open+`Code: https://github.com/warengonzaga/covid19-tracker-cli`+style.gray.close}]
101+
);
102+
103+
return table.toString()+'\n'+footer;
104+
};
105+
70106
const footer = `
71107
Always wash your hands, stay safe...
72108
73109
---
74110
Love this project? Please consider buying me a cup of coffee!
75-
${style.yellow.open}buymeacoff.ee/warengonzaga${style.yellow.close}
111+
${style.yellow.open}warengonzaga.com/buymeacoffee${style.yellow.close}
76112
77113
---
78114
Follow ${style.bgCyan.open}${style.black.open}@warengonzaga${style.black.close}${style.bgCyan.close} for more updates!

0 commit comments

Comments
 (0)