Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/b
const { getCompleteTable } = require('./lib/corona');
const { lookupCountry } = require('./lib/helpers');
const { getLiveUpdates } = require('./lib/reddit.js');
const { getWorldoMetersTable } = require('./lib/worldoMeters.js');

const app = express();
const port = process.env.PORT || 3001;
Expand Down Expand Up @@ -32,6 +33,14 @@ app.get('/', (req, res) => {
const minimal = req.query.minimal === 'true';
const emojis = req.query.emojis === 'true';
const top = req.query.top ? Number(req.query.top) : 1000;
const source = req.query.source ? Number(req.query.source) : 1;

if (source === 2) {
return getWorldoMetersTable({ isCurl, emojis, minimal, top })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
}

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

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


const { iso2 } = lookupObj;

if (source === 2) {
return getWorldoMetersTable({ countryCode: iso2, isCurl, emojis, minimal })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
}

if (format.toLowerCase() === 'json') {
return getJSONDataForCountry(iso2).then(result => {
return res.json(result);
Expand Down
29 changes: 21 additions & 8 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const yargs = require('yargs');
const chalk = require('chalk');
const { getCompleteTable } = require('../lib/corona');
const { getCountryTable } = require('../lib/byCountry');
const { getWorldoMetersTable } = require('../lib/worldoMeters');
const { lookupCountry } = require('../lib/helpers');

const { argv } = yargs
Expand Down Expand Up @@ -34,6 +35,12 @@ const { argv } = yargs
})
)
.options({
s: {
alias: 'source',
describe: 'fetch data from other source',
default: 1,
type: 'int'
},
e: {
alias: 'emojis',
describe: 'Show emojis in table',
Expand All @@ -60,11 +67,17 @@ const { argv } = yargs
.strict()
.help('help');

const { emojis, country, minimal, top } = argv;
(
country === 'ALL'
? getCompleteTable({ emojis, minimal, top })
: getCountryTable({ countryCode: country, emojis, minimal })
)
.then(console.log)
.catch(console.error);
argv.countryCode = argv.country;
if (argv.source === 2) {
getWorldoMetersTable(argv)
.then(console.log)
.catch(console.error);
} else {
(
argv.country === 'ALL'
? getCompleteTable(argv)
: getCountryTable(argv)
)
.then(console.log)
.catch(console.error);
}
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Version 0.7.0

* Added new source to fetch realtime data. ``corona --source=2``
* Code refactored and some bug fixes.

## Version 0.6.0

* Added filter to show top N countries. ``corona --top=20``
Expand Down
59 changes: 52 additions & 7 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,68 @@
const NodeCache = require('node-cache');
const axios = require('axios');
const { countryNameMap } = require('./constants');

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

exports.getCoronaData = async () => {
const coronaCache = myCache.get(CORONA_ALL_KEY);
const CORONA_ALL_KEY = 'coronaAll';
const cache = myCache.get(CORONA_ALL_KEY);

if (coronaCache) {
return coronaCache;
if (cache) {
return cache;
}

const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');

if (!result || !result.data) {
throw new Error('Source API failure.');
}

myCache.set(CORONA_ALL_KEY, result.data, 60 * 15);

return result.data;
};

/** Fetch Worldometers Data */
exports.getWorldoMetersData = async (countryCode = 'ALL') => {
const key = `worldMetersData_${countryCode}`;
const cache = myCache.get(key);

if (cache) {
console.log('cache', key);
return cache;
}
const result = await axios('https://corona.lmao.ninja/countries');
if (!result || !result.data) {
throw new Error('WorldoMeters Source API failure');
}

const worldStats = result.data.reduce((acc, countryObj) => {
acc.cases += countryObj.cases;
acc.todayCases += countryObj.todayCases;
acc.deaths += countryObj.deaths;
acc.todayDeaths += countryObj.todayDeaths;
acc.recovered += countryObj.recovered;
acc.active += countryObj.active;
acc.critical += countryObj.critical;
return acc;
}, {
countryName: 'World',
cases: 0,
todayCases: 0,
deaths: 0,
todayDeaths: 0,
recovered: 0,
active: 0,
critical: 0,
});

result.data.forEach(obj => obj.countryCode = countryNameMap[obj.country]);
worldStats.casesPerOneMillion = (worldStats.cases / 7794).toFixed(2);
let finalData = result.data;
console.log(countryCode);
if (countryCode && countryCode !== 'ALL') {
finalData = finalData.filter(obj => obj.countryCode === countryCode);
}
const returnObj = { data: finalData, worldStats };

myCache.set(key, returnObj, 60 * 15);
return returnObj;
};
183 changes: 183 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
exports.countryNameMap = {
China: 'CN',
UK: 'GB',
Martinique: 'MQ',
Liechtenstein: 'LI',
'Réunion': 'RE',
Ukraine: 'UA',
Honduras: 'HN',
Afghanistan: 'AF',
Bangladesh: 'BD',
Macao: 'MO',
Bolivia: 'BO',
Cuba: 'CU',
Netherlands: 'NL',
Jamaica: 'JM',
'French Guiana': 'GF',
DRC: 'CD',
Cameroon: 'CM',
Maldives: 'MV',
Montenegro: 'ME',
Paraguay: 'PY',
Nigeria: 'NG',
Guam: 'GU',
'French Polynesia': 'PF',
Austria: 'AT',
Ghana: 'GH',
Rwanda: 'RW',
Monaco: 'MC',
Gibraltar: 'GI',
Guatemala: 'GT',
'Ivory Coast': 'CI',
Ethiopia: 'ET',
Togo: 'TG',
'Trinidad and Tobago': 'TT',
Kenya: 'KE',
Belgium: 'BE',
Mauritius: 'MU',
'Equatorial Guinea': 'GQ',
Kyrgyzstan: 'KG',
Mongolia: 'MN',
'Puerto Rico': 'PR',
Seychelles: 'SC',
Tanzania: 'TZ',
Guyana: 'GY',
Aruba: 'AW',
Barbados: 'BB',
Norway: 'NO',
Mayotte: 'YT',
'Cayman Islands': 'KY',
'Curaçao': 'CW',
Bahamas: 'BS',
Congo: 'CD',
Gabon: 'GA',
Namibia: 'NA',
'St. Barth': 'BL',
'Saint Martin': 'MF',
'U.S. Virgin Islands': 'VI',
Sweden: 'SE',
Sudan: 'SD',
Benin: 'BJ',
Bermuda: 'BM',
Bhutan: 'BT',
CAR: 'CF',
Greenland: 'GL',
Haiti: 'HT',
Liberia: 'LR',
Mauritania: 'MR',
'New Caledonia': 'NC',
Denmark: 'DK',
'Saint Lucia': 'LC',
Zambia: 'ZM',
Nepal: 'NP',
Angola: 'AO',
'Antigua and Barbuda': 'AG',
'Cabo Verde': 'CV',
Chad: 'TD',
Djibouti: 'DJ',
'El Salvador': 'SV',
Fiji: 'FJ',
Japan: 'JP',
Gambia: 'GM',
Guinea: 'GN',
'Vatican City': 'VA',
'Isle of Man': 'IM',
Montserrat: 'MS',
Nicaragua: 'NI',
Niger: 'NE',
'St. Vincent Grenadines': 'VC',
'Sint Maarten': 'SX',
Somalia: 'SO',
Malaysia: 'MY',
Suriname: 'SR',
Eswatini: 'SZ',
Australia: 'AU',
Italy: 'IT',
Canada: 'CA',
Portugal: 'PT',
Czechia: 'CZ',
Israel: 'IL',
Brazil: 'BR',
Luxembourg: 'LU',
Ireland: 'IE',
Greece: 'GR',
Qatar: 'QA',
Pakistan: 'PK',
Iran: 'IR',
Finland: 'FI',
Poland: 'PL',
Turkey: 'TR',
Singapore: 'SG',
Chile: 'CL',
Iceland: 'IS',
Thailand: 'TH',
Slovenia: 'SI',
Indonesia: 'ID',
Bahrain: 'BH',
Spain: 'ES',
Romania: 'RO',
'Saudi Arabia': 'SA',
Estonia: 'EE',
Ecuador: 'EC',
Egypt: 'EG',
Peru: 'PE',
Philippines: 'PH',
'Hong Kong': 'HK',
India: 'IN',
Russia: 'RU',
Germany: 'DE',
Iraq: 'IQ',
Mexico: 'MX',
Lebanon: 'LB',
'South Africa': 'ZA',
Kuwait: 'KW',
'San Marino': 'SM',
UAE: 'AE',
Panama: 'PA',
Armenia: 'AM',
Taiwan: 'TW',
USA: 'US',
Argentina: 'AR',
Colombia: 'CO',
Slovakia: 'SK',
Serbia: 'RS',
Croatia: 'HR',
Bulgaria: 'BG',
Uruguay: 'UY',
Algeria: 'DZ',
'Costa Rica': 'CR',
Latvia: 'LV',
France: 'FR',
Hungary: 'HU',
Vietnam: 'VN',
'Faeroe Islands': 'FO',
Andorra: 'AD',
Brunei: 'BN',
Belarus: 'BY',
Jordan: 'JO',
Cyprus: 'CY',
'Sri Lanka': 'LK',
Albania: 'AL',
'S. Korea': 'KR',
'Bosnia and Herzegovina': 'BA',
Morocco: 'MA',
Malta: 'MT',
'North Macedonia': 'MK',
Moldova: 'MD',
Kazakhstan: 'KZ',
Lithuania: 'LT',
Oman: 'OM',
Cambodia: 'KH',
Palestine: 'PS',
Switzerland: 'CH',
Guadeloupe: 'GP',
Azerbaijan: 'AZ',
Georgia: 'GE',
Venezuela: 'VE',
Tunisia: 'TN',
'New Zealand': 'NZ',
Senegal: 'SN',
'Dominican Republic': 'DO',
'Burkina Faso': 'BF',
Uzbekistan: 'UZ',
};
Loading