Skip to content

Commit 4ea20d6

Browse files
author
Waren Gonzaga
committed
Initial commit
1 parent 167b41e commit 4ea20d6

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.jshintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"esversion": 8
3+
}

app.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const express = require('express'),
2+
app = express(),
3+
util = require('./src/util'),
4+
fs = require('fs'),
5+
covid19 = require('./src/tracker/covid19');
6+
7+
// set port
8+
const port = process.env.port || 7070;
9+
10+
// package.json info
11+
const pkg = JSON.parse(fs.readFileSync('package.json'));
12+
13+
// global route for covid19 tracker
14+
app.get('/', async (req, res, next) => {
15+
const userAgent = req.headers['user-agent'];
16+
if (util.isCommandline(userAgent)) {
17+
await res.send(covid19.covid19globaltracker());
18+
return null;
19+
}
20+
return next();
21+
});
22+
23+
app.get('*', (req, res) => res.send(`COVID-19 Tracker v${pkg.version}\n\nTry adding /covid19 for global tracking\nI'm still working on country specific data...`));
24+
25+
app.listen(port, () => console.log(`COVID-19 Tracker v${pkg.version} is listening on port ${port}!`));

bin/util/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { Readable } = require('stream');
2+
3+
const isCommandline = (userAgent) => {
4+
const check = (userAgent.search(/curl|wget/i) !== -1);
5+
return check;
6+
};
7+
8+
const getStream = (req, res) => {
9+
const stream = new Readable();
10+
stream.pipe(res);
11+
stream._read = () => {};
12+
req.on('close', () => {
13+
stream.destroy();
14+
});
15+
return stream;
16+
};
17+
18+
module.exports = {
19+
isCommandline,
20+
getStream,
21+
};

lib/cli/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-disable no-await-in-loop */
2+
3+
const style = require('ansi-styles'),
4+
request = require("request"),
5+
fs = require('fs');
6+
7+
// package.json information
8+
const pkg = JSON.parse(fs.readFileSync('package.json'));
9+
10+
// format data
11+
const formatNumber = (num) => {
12+
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
13+
};
14+
15+
// time data
16+
let ts = Date.now();
17+
let date_ob = new Date(ts);
18+
let date = date_ob.getDate();
19+
let month = date_ob.getMonth() + 1;
20+
let year = date_ob.getFullYear();
21+
let currentdate = month + "/" + date + "/" + year;
22+
23+
// api request for global data
24+
request.get("https://corona.lmao.ninja/all", (error, response, body) => {
25+
if(error) {
26+
return console.dir(error);
27+
}
28+
globalData = JSON.parse(body);
29+
});
30+
31+
// covid19 global tracker
32+
const covid19globaltracker = (
33+
trckrvrsn = pkg.version,
34+
cases = globalData.cases,
35+
deaths = globalData.deaths,
36+
recovered = globalData.recovered,
37+
asof = new Date(globalData.updated)
38+
) => `
39+
──────────────────────────────────────────────────
40+
${style.cyan.open}Waren Gonzaga's Tracker v${trckrvrsn}${style.cyan.close} | ${style.red.open}COVID19${style.red.close} ${style.yellow.open}[Global]${style.yellow.close}
41+
──────────────────────────────────────────────────
42+
As of ${asof.toLocaleString()}
43+
44+
${style.magenta.open}Cases:${style.magenta.close} ${formatNumber(cases)}
45+
${style.red.open}Deaths:${style.red.close} ${formatNumber(deaths)}
46+
${style.green.open}Recovered:${style.green.close} ${formatNumber(recovered)}
47+
48+
---
49+
${style.gray.open}Source: https://www.worldometers.info/coronavirus/${style.gray.close}
50+
${style.gray.open}Date: ${currentdate}${style.gray.close}
51+
──────────────────────────────────────────────────
52+
53+
Love this project? Please consider buying me a cup of coffee!
54+
${style.yellow.open}buymeacoff.ee/warengonzaga${style.yellow.close}
55+
---
56+
Follow ${style.bgCyan.open}${style.black.open}@warengonzaga${style.black.close}${style.bgCyan.close} for more updates!
57+
\n`;
58+
59+
module.exports = {
60+
covid19globaltracker
61+
};

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "covid19-tracker-cli",
3+
"version": "1.0.0",
4+
"description": "Track COVID-19 cases from command line interface",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Waren Gonzaga",
10+
"license": "MIT",
11+
"dependencies": {
12+
"ansi-styles": "^4.2.1",
13+
"express": "^4.17.1",
14+
"request": "^2.88.2"
15+
}
16+
}

0 commit comments

Comments
 (0)