forked from sagarkarira/coronavirus-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
68 lines (59 loc) · 1.93 KB
/
api.js
File metadata and controls
68 lines (59 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const NodeCache = require('node-cache');
const axios = require('axios');
const { countryNameMap } = require('./constants');
const myCache = new NodeCache({ stdTTL: 100, checkperiod: 600 });
exports.getCoronaData = async () => {
const CORONA_ALL_KEY = 'coronaAll';
const cache = myCache.get(CORONA_ALL_KEY);
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;
};