Skip to content

Commit e243cf1

Browse files
committed
moved from npm to curl, added country/state wise filter
1 parent 20c3787 commit e243cf1

File tree

8 files changed

+619
-21
lines changed

8 files changed

+619
-21
lines changed

app.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const express = require('express');
2+
const app = express();
3+
4+
const port = process.env.PORT || 3001;
5+
6+
const { getCountryTable } = require('./lib/byCountry');
7+
const { getCompleteTable } = require('./lib/corona');
8+
9+
app.get('/', (req, res) => {
10+
return getCompleteTable().then(result => {
11+
return res.send(result);
12+
}).catch(error => res.send(error));
13+
});
14+
15+
app.get('/:country', (req, res) => {
16+
let { country } = req.params;
17+
if (!country || country === 'all') {
18+
return getCompleteTable().then(result => {
19+
return res.send(result);
20+
}).catch(error => res.send(error));
21+
}
22+
return getCountryTable(country).then(result => {
23+
return res.send(result);
24+
}).catch(error => res.send(error));
25+
});
26+
27+
app.listen(port, () => console.log(`Running on ${port}`));

lib/byCountry.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
const Table = require('cli-table3');
2+
const axios = require('axios');
3+
const _ = require('lodash');
4+
const chalk = require('chalk');
5+
const helpers = require('./helpers');
6+
const {
7+
getCountry,
8+
getConfirmed,
9+
getActive,
10+
getDeaths,
11+
getRecovered,
12+
getMortalityPer,
13+
getRecoveredPer,
14+
getEmoji,
15+
calActive,
16+
calMortalityPer,
17+
calRecoveredPer,
18+
getOneDayChange,
19+
getOneWeekChange,
20+
getRateOfGrowth,
21+
getTotalStats,
22+
footer,
23+
} = require('./helpers');
24+
25+
function getDataByState(confirmed, deaths, recovered) {
26+
const countryMap = {};
27+
const confirmedMap = _.keyBy(confirmed.locations, (i) => i.country + i.province);
28+
const recoveredMap = _.keyBy(recovered.locations, (i) => i.country + i.province);
29+
const deathsMap = _.keyBy(deaths.locations, (i) => i.country + i.province);
30+
confirmed.locations.forEach(obj => {
31+
const countryName = obj.country;
32+
const provinceName = obj.province;
33+
const mapKey = countryName + provinceName;
34+
if (!countryMap[mapKey] && confirmedMap[mapKey].latest > 0) {
35+
countryMap[mapKey] = {
36+
country: countryName,
37+
province: provinceName,
38+
countryCode: obj.country_code,
39+
confirmed: confirmedMap[mapKey].latest,
40+
recovered: recoveredMap[mapKey].latest,
41+
deaths: deathsMap[mapKey].latest,
42+
confirmedByDay: helpers.historyObjToArr(confirmedMap[mapKey].history),
43+
recoveredByDay: helpers.historyObjToArr(recoveredMap[mapKey].history),
44+
deathsByDay: helpers.historyObjToArr(recoveredMap[mapKey].history),
45+
};
46+
}
47+
});
48+
const countryArr = extraStats(
49+
Object.keys(countryMap).map(key => countryMap[key])
50+
);
51+
return _.sortBy(countryArr, (o) => -o.confirmed)
52+
}
53+
54+
function extraStats(dataArr) {
55+
return dataArr.map(obj => Object.assign({}, obj,
56+
{
57+
active: calActive(obj),
58+
mortalityPer: calMortalityPer(obj),
59+
recoveredPer: calRecoveredPer(obj),
60+
})
61+
);
62+
}
63+
64+
exports.getCountryTable = async (country) => {
65+
const head = [
66+
'',
67+
'State',
68+
'Confirmed ✅',
69+
'Recovered 😃',
70+
'Deaths 😞',
71+
'Active 😷',
72+
'Mortality %',
73+
'Recovered %',
74+
'1 Day 🔺',
75+
'1 Week 🔺',
76+
// 'RoG',
77+
'🏳',
78+
];
79+
const table = new Table({
80+
head,
81+
chars: { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
82+
, 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
83+
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
84+
, 'right': ' ║' , 'right-mid': '╢' , 'middle': '│' }
85+
});
86+
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
87+
const { latest, confirmed, deaths, recovered } = result.data;
88+
const countryData = getDataByState(confirmed, deaths, recovered, country)
89+
.filter(obj => obj.country === country);
90+
const totalStats = getTotalStats(countryData);
91+
table.push({
92+
[country]: [
93+
'Total',
94+
getConfirmed(totalStats.confirmed),
95+
getRecovered(totalStats.recovered),
96+
getDeaths(totalStats.deaths),
97+
getActive(totalStats.active),
98+
getMortalityPer(totalStats.mortalityPer),
99+
getRecoveredPer(totalStats.recoveredPer),
100+
getOneDayChange(totalStats),
101+
getOneWeekChange(totalStats),
102+
// '',
103+
getEmoji(countryData[0].countryCode),
104+
]
105+
})
106+
if (countryData.length > 1) {
107+
let rank = 1;
108+
countryData.forEach(cd => {
109+
const countryEmoji = getEmoji(cd.countryCode);
110+
const values = [
111+
cd.province,
112+
getConfirmed(cd.confirmed),
113+
getRecovered(cd.recovered),
114+
getDeaths(cd.deaths),
115+
getActive(cd.confirmed, cd.recovered, cd.deaths),
116+
getMortalityPer(cd.mortalityPer),
117+
getRecoveredPer(cd.recoveredPer),
118+
getOneDayChange(cd),
119+
getOneWeekChange(cd),
120+
// getRateOfGrowth(cd),
121+
getEmoji(cd.countryCode),
122+
]
123+
table.push({ [rank++]: values })
124+
});
125+
}
126+
return table.toString() + footer;
127+
}

lib/corona.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
getOneWeekChange,
2020
getRateOfGrowth,
2121
getTotalStats,
22+
footer,
2223
} = require('./helpers');
2324

2425

@@ -67,10 +68,6 @@ function getDataByCountry(confirmed, deaths, recovered) {
6768
return _.sortBy(countryArr, (o) => -o.confirmed)
6869
}
6970

70-
function getDateByState(confirmed, deaths, recovered) {
71-
72-
}
73-
7471
function extraStats(dataArr) {
7572
return dataArr.map(obj => Object.assign({}, obj,
7673
{
@@ -140,5 +137,5 @@ exports.getCompleteTable = async () => {
140137
]
141138
table.push({ [rank++]: values })
142139
});
143-
return table.toString();
140+
return table.toString() + footer;
144141
}

lib/helpers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,10 @@ e.getTotalStats = (countryData) => {
143143
worldStats.recoveredPer = e.calRecoveredPer(worldStats);
144144
worldStats.mortalityPer = e.calMortalityPer(worldStats);
145145
return worldStats;
146-
}
146+
};
147+
148+
e.footer = `
149+
150+
Source Code: https://github.com/sagarkarira/coronavirus-tracker-cli/
151+
Twitter: http://twitter.com/ekrysis
152+
`;

now.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": 2,
3+
"builds": [
4+
{
5+
"src": "*.js",
6+
"use": "@now/node-server"
7+
}
8+
],
9+
"routes": [
10+
{
11+
"headers": {
12+
"Access-Control-Allow-Origin": "*",
13+
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
14+
"Access-Control-Allow-Headers": "X-Requested-With, Content-Type, Accept"
15+
},
16+
"src": "/.*",
17+
"dest": "/app.js"
18+
}
19+
],
20+
"env": {
21+
"VERSION": "1.0.0"
22+
}
23+
}

0 commit comments

Comments
 (0)