forked from MaJingRui-SH/Coronavirus-Dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchData.js
116 lines (98 loc) · 3.5 KB
/
fetchData.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const globals = require("./globals");
const utilities = require("./utilities");
const bnoScraper = require("./micro-scrapers/bno");
const cnnScraper = require("./micro-scrapers/cnn");
const coronatrackerScraper = require("./micro-scrapers/coronatracker");
const fs = require("fs");
exports.fetchAllData = async () => {
const allData = {};
const bnoRegions = globals.allRegions
.filter(region => {
return region.scraper === "bno";
})
.map(region => bnoScraper.fetchData(region));
Promise.all(bnoRegions)
.then(data => {
// Gather BNO data as base.
data.map(
resolvedRegion => {
if(resolvedRegion === {}) {
return Promise.reject("Couldn't fetch data for a region.");
}
allData[resolvedRegion.regionName] = resolvedRegion
}
);
if(Object.keys(allData).indexOf('undefined') >= 0) {
return Promise.reject("Couldn't fetch data.");
}
console.log('[SYNC] Fetching all BNO data.');
allData["LatinAmerica"].regions,
allData["Global"].regions = utilities.syncTwoRegions(
allData["LatinAmerica"].regions,
allData["Global"].regions
);
})
.then(() => {
// Sync coronatracker data and BNO data.
coronatrackerScraper
.getSelectedCountries("Europe", globals.countryLists["Europe"])
.then(europeanData => {
allData["Europe"] = europeanData;
allData["Europe"].regions,
allData["Global"].regions = utilities.syncTwoRegions(
allData["Europe"].regions,
allData["Global"].regions
);
})
.then(() => {
// Sync USA data and CNN data.
cnnScraper.fetchData().then(cnnData => {
cnnData,
(allData["USA"].regions = utilities.syncTwoRegions(
cnnData,
allData["USA"].regions
));
// Sync with Overrides and write final finals.
gatherAllOverrides(allData);
});
});
});
};
const gatherAllOverrides = (allData) => {
return Promise.all(
Object.keys(allData).map(region =>
fs.promises.readFile(`${utilities.getOverridesJSONPath(region)}`))
).then(values => {
let data = {};
values.forEach(region => {
const regionData = JSON.parse(region);
data[regionData.regionName] = regionData;
});
Object.keys(data).map(region => {
data[region].regions,
(allData[region].regions = utilities.syncTwoRegions(
data[region].regions,
allData[region].regions
));
if(region!=="Global") {
allData[region].regionTotal = utilities.calculateRegionTotal(
data[region].regions
);
}
})
// Sync the Global United States value with the Region value.
// Region will be the correct one because it is has two sources.
allData["Global"].regions.map((region, index) => {
if(region.country === "United States") {
allData["Global"].regions[index].cases = allData["USA"].regionTotal.cases,
allData["Global"].regions[index].deaths = allData["USA"].regionTotal.deaths,
allData["Global"].regions[index].serious = allData["USA"].regionTotal.serious,
allData["Global"].regions[index].recovered = allData["USA"].regionTotal.recovered
}
})
Object.keys(data).map(region => {
console.log(`[SYNC] Successful: ${region} - Saved.`);
utilities.writeJSONFile(region, allData[region]);
})
});
};