Skip to content

Commit 7e98b43

Browse files
authored
Merge pull request sagarkarira#60 from sagarkarira/feat-realtime-api
added realtime api fetching data from worldometers.info
2 parents c734616 + 93152c7 commit 7e98b43

File tree

9 files changed

+407
-50
lines changed

9 files changed

+407
-50
lines changed

app.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/b
55
const { getCompleteTable } = require('./lib/corona');
66
const { lookupCountry } = require('./lib/helpers');
77
const { getLiveUpdates } = require('./lib/reddit.js');
8+
const { getWorldoMetersTable } = require('./lib/worldoMeters.js');
89

910
const app = express();
1011
const port = process.env.PORT || 3001;
@@ -32,6 +33,14 @@ app.get('/', (req, res) => {
3233
const minimal = req.query.minimal === 'true';
3334
const emojis = req.query.emojis === 'true';
3435
const top = req.query.top ? Number(req.query.top) : 1000;
36+
const source = req.query.source ? Number(req.query.source) : 1;
37+
38+
if (source === 2) {
39+
return getWorldoMetersTable({ isCurl, emojis, minimal, top })
40+
.then(result => {
41+
return res.send(result);
42+
}).catch(error => errorHandler(error, res));
43+
}
3544

3645
if (format.toLowerCase() === 'json') {
3746
return getJSONData().then(result => {
@@ -66,6 +75,7 @@ app.get('/:country', (req, res) => {
6675
const format = req.query.format ? req.query.format : '';
6776
const minimal = req.query.minimal === 'true';
6877
const emojis = req.query.emojis === 'true';
78+
const source = req.query.source ? Number(req.query.source) : 1;
6979

7080
if (!country || country.toUpperCase() === 'ALL') {
7181
if (format.toLowerCase() === 'json') {
@@ -93,8 +103,16 @@ app.get('/:country', (req, res) => {
93103
`);
94104
}
95105

106+
96107
const { iso2 } = lookupObj;
97108

109+
if (source === 2) {
110+
return getWorldoMetersTable({ countryCode: iso2, isCurl, emojis, minimal })
111+
.then(result => {
112+
return res.send(result);
113+
}).catch(error => errorHandler(error, res));
114+
}
115+
98116
if (format.toLowerCase() === 'json') {
99117
return getJSONDataForCountry(iso2).then(result => {
100118
return res.json(result);

bin/index.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const yargs = require('yargs');
55
const chalk = require('chalk');
66
const { getCompleteTable } = require('../lib/corona');
77
const { getCountryTable } = require('../lib/byCountry');
8+
const { getWorldoMetersTable } = require('../lib/worldoMeters');
89
const { lookupCountry } = require('../lib/helpers');
910

1011
const { argv } = yargs
@@ -34,6 +35,12 @@ const { argv } = yargs
3435
})
3536
)
3637
.options({
38+
s: {
39+
alias: 'source',
40+
describe: 'fetch data from other source',
41+
default: 1,
42+
type: 'int'
43+
},
3744
e: {
3845
alias: 'emojis',
3946
describe: 'Show emojis in table',
@@ -60,11 +67,17 @@ const { argv } = yargs
6067
.strict()
6168
.help('help');
6269

63-
const { emojis, country, minimal, top } = argv;
64-
(
65-
country === 'ALL'
66-
? getCompleteTable({ emojis, minimal, top })
67-
: getCountryTable({ countryCode: country, emojis, minimal })
68-
)
69-
.then(console.log)
70-
.catch(console.error);
70+
argv.countryCode = argv.country;
71+
if (argv.source === 2) {
72+
getWorldoMetersTable(argv)
73+
.then(console.log)
74+
.catch(console.error);
75+
} else {
76+
(
77+
argv.country === 'ALL'
78+
? getCompleteTable(argv)
79+
: getCountryTable(argv)
80+
)
81+
.then(console.log)
82+
.catch(console.error);
83+
}

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Version 0.7.0
4+
5+
* Added new source to fetch realtime data. ``corona --source=2``
6+
* Code refactored and some bug fixes.
7+
38
## Version 0.6.0
49

510
* Added filter to show top N countries. ``corona --top=20``

lib/api.js

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,68 @@
11
const NodeCache = require('node-cache');
22
const axios = require('axios');
3+
const { countryNameMap } = require('./constants');
34

45
const myCache = new NodeCache({ stdTTL: 100, checkperiod: 600 });
5-
const CORONA_ALL_KEY = 'coronaAll';
66

77
exports.getCoronaData = async () => {
8-
const coronaCache = myCache.get(CORONA_ALL_KEY);
8+
const CORONA_ALL_KEY = 'coronaAll';
9+
const cache = myCache.get(CORONA_ALL_KEY);
910

10-
if (coronaCache) {
11-
return coronaCache;
11+
if (cache) {
12+
return cache;
1213
}
13-
1414
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
1515

1616
if (!result || !result.data) {
1717
throw new Error('Source API failure.');
1818
}
19-
2019
myCache.set(CORONA_ALL_KEY, result.data, 60 * 15);
21-
2220
return result.data;
2321
};
22+
23+
/** Fetch Worldometers Data */
24+
exports.getWorldoMetersData = async (countryCode = 'ALL') => {
25+
const key = `worldMetersData_${countryCode}`;
26+
const cache = myCache.get(key);
27+
28+
if (cache) {
29+
console.log('cache', key);
30+
return cache;
31+
}
32+
const result = await axios('https://corona.lmao.ninja/countries');
33+
if (!result || !result.data) {
34+
throw new Error('WorldoMeters Source API failure');
35+
}
36+
37+
const worldStats = result.data.reduce((acc, countryObj) => {
38+
acc.cases += countryObj.cases;
39+
acc.todayCases += countryObj.todayCases;
40+
acc.deaths += countryObj.deaths;
41+
acc.todayDeaths += countryObj.todayDeaths;
42+
acc.recovered += countryObj.recovered;
43+
acc.active += countryObj.active;
44+
acc.critical += countryObj.critical;
45+
return acc;
46+
}, {
47+
countryName: 'World',
48+
cases: 0,
49+
todayCases: 0,
50+
deaths: 0,
51+
todayDeaths: 0,
52+
recovered: 0,
53+
active: 0,
54+
critical: 0,
55+
});
56+
57+
result.data.forEach(obj => obj.countryCode = countryNameMap[obj.country]);
58+
worldStats.casesPerOneMillion = (worldStats.cases / 7794).toFixed(2);
59+
let finalData = result.data;
60+
console.log(countryCode);
61+
if (countryCode && countryCode !== 'ALL') {
62+
finalData = finalData.filter(obj => obj.countryCode === countryCode);
63+
}
64+
const returnObj = { data: finalData, worldStats };
65+
66+
myCache.set(key, returnObj, 60 * 15);
67+
return returnObj;
68+
};

lib/constants.js

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
exports.countryNameMap = {
2+
China: 'CN',
3+
UK: 'GB',
4+
Martinique: 'MQ',
5+
Liechtenstein: 'LI',
6+
'Réunion': 'RE',
7+
Ukraine: 'UA',
8+
Honduras: 'HN',
9+
Afghanistan: 'AF',
10+
Bangladesh: 'BD',
11+
Macao: 'MO',
12+
Bolivia: 'BO',
13+
Cuba: 'CU',
14+
Netherlands: 'NL',
15+
Jamaica: 'JM',
16+
'French Guiana': 'GF',
17+
DRC: 'CD',
18+
Cameroon: 'CM',
19+
Maldives: 'MV',
20+
Montenegro: 'ME',
21+
Paraguay: 'PY',
22+
Nigeria: 'NG',
23+
Guam: 'GU',
24+
'French Polynesia': 'PF',
25+
Austria: 'AT',
26+
Ghana: 'GH',
27+
Rwanda: 'RW',
28+
Monaco: 'MC',
29+
Gibraltar: 'GI',
30+
Guatemala: 'GT',
31+
'Ivory Coast': 'CI',
32+
Ethiopia: 'ET',
33+
Togo: 'TG',
34+
'Trinidad and Tobago': 'TT',
35+
Kenya: 'KE',
36+
Belgium: 'BE',
37+
Mauritius: 'MU',
38+
'Equatorial Guinea': 'GQ',
39+
Kyrgyzstan: 'KG',
40+
Mongolia: 'MN',
41+
'Puerto Rico': 'PR',
42+
Seychelles: 'SC',
43+
Tanzania: 'TZ',
44+
Guyana: 'GY',
45+
Aruba: 'AW',
46+
Barbados: 'BB',
47+
Norway: 'NO',
48+
Mayotte: 'YT',
49+
'Cayman Islands': 'KY',
50+
'Curaçao': 'CW',
51+
Bahamas: 'BS',
52+
Congo: 'CD',
53+
Gabon: 'GA',
54+
Namibia: 'NA',
55+
'St. Barth': 'BL',
56+
'Saint Martin': 'MF',
57+
'U.S. Virgin Islands': 'VI',
58+
Sweden: 'SE',
59+
Sudan: 'SD',
60+
Benin: 'BJ',
61+
Bermuda: 'BM',
62+
Bhutan: 'BT',
63+
CAR: 'CF',
64+
Greenland: 'GL',
65+
Haiti: 'HT',
66+
Liberia: 'LR',
67+
Mauritania: 'MR',
68+
'New Caledonia': 'NC',
69+
Denmark: 'DK',
70+
'Saint Lucia': 'LC',
71+
Zambia: 'ZM',
72+
Nepal: 'NP',
73+
Angola: 'AO',
74+
'Antigua and Barbuda': 'AG',
75+
'Cabo Verde': 'CV',
76+
Chad: 'TD',
77+
Djibouti: 'DJ',
78+
'El Salvador': 'SV',
79+
Fiji: 'FJ',
80+
Japan: 'JP',
81+
Gambia: 'GM',
82+
Guinea: 'GN',
83+
'Vatican City': 'VA',
84+
'Isle of Man': 'IM',
85+
Montserrat: 'MS',
86+
Nicaragua: 'NI',
87+
Niger: 'NE',
88+
'St. Vincent Grenadines': 'VC',
89+
'Sint Maarten': 'SX',
90+
Somalia: 'SO',
91+
Malaysia: 'MY',
92+
Suriname: 'SR',
93+
Eswatini: 'SZ',
94+
Australia: 'AU',
95+
Italy: 'IT',
96+
Canada: 'CA',
97+
Portugal: 'PT',
98+
Czechia: 'CZ',
99+
Israel: 'IL',
100+
Brazil: 'BR',
101+
Luxembourg: 'LU',
102+
Ireland: 'IE',
103+
Greece: 'GR',
104+
Qatar: 'QA',
105+
Pakistan: 'PK',
106+
Iran: 'IR',
107+
Finland: 'FI',
108+
Poland: 'PL',
109+
Turkey: 'TR',
110+
Singapore: 'SG',
111+
Chile: 'CL',
112+
Iceland: 'IS',
113+
Thailand: 'TH',
114+
Slovenia: 'SI',
115+
Indonesia: 'ID',
116+
Bahrain: 'BH',
117+
Spain: 'ES',
118+
Romania: 'RO',
119+
'Saudi Arabia': 'SA',
120+
Estonia: 'EE',
121+
Ecuador: 'EC',
122+
Egypt: 'EG',
123+
Peru: 'PE',
124+
Philippines: 'PH',
125+
'Hong Kong': 'HK',
126+
India: 'IN',
127+
Russia: 'RU',
128+
Germany: 'DE',
129+
Iraq: 'IQ',
130+
Mexico: 'MX',
131+
Lebanon: 'LB',
132+
'South Africa': 'ZA',
133+
Kuwait: 'KW',
134+
'San Marino': 'SM',
135+
UAE: 'AE',
136+
Panama: 'PA',
137+
Armenia: 'AM',
138+
Taiwan: 'TW',
139+
USA: 'US',
140+
Argentina: 'AR',
141+
Colombia: 'CO',
142+
Slovakia: 'SK',
143+
Serbia: 'RS',
144+
Croatia: 'HR',
145+
Bulgaria: 'BG',
146+
Uruguay: 'UY',
147+
Algeria: 'DZ',
148+
'Costa Rica': 'CR',
149+
Latvia: 'LV',
150+
France: 'FR',
151+
Hungary: 'HU',
152+
Vietnam: 'VN',
153+
'Faeroe Islands': 'FO',
154+
Andorra: 'AD',
155+
Brunei: 'BN',
156+
Belarus: 'BY',
157+
Jordan: 'JO',
158+
Cyprus: 'CY',
159+
'Sri Lanka': 'LK',
160+
Albania: 'AL',
161+
'S. Korea': 'KR',
162+
'Bosnia and Herzegovina': 'BA',
163+
Morocco: 'MA',
164+
Malta: 'MT',
165+
'North Macedonia': 'MK',
166+
Moldova: 'MD',
167+
Kazakhstan: 'KZ',
168+
Lithuania: 'LT',
169+
Oman: 'OM',
170+
Cambodia: 'KH',
171+
Palestine: 'PS',
172+
Switzerland: 'CH',
173+
Guadeloupe: 'GP',
174+
Azerbaijan: 'AZ',
175+
Georgia: 'GE',
176+
Venezuela: 'VE',
177+
Tunisia: 'TN',
178+
'New Zealand': 'NZ',
179+
Senegal: 'SN',
180+
'Dominican Republic': 'DO',
181+
'Burkina Faso': 'BF',
182+
Uzbekistan: 'UZ',
183+
};

0 commit comments

Comments
 (0)