forked from pomber/covid19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
67 lines (56 loc) · 2.12 KB
/
update.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
const fs = require("fs");
const path = require("path");
const parse = require("csv-parse/lib/sync");
const FILENAME_CONFIRMED = "time_series_covid19_confirmed_global.csv";
const FILENAME_DEATHS = "time_series_covid19_deaths_global.csv";
const FILENAME_RECOVERED = "time_series_covid19_recovered_global.csv";
function extract(filepath) {
const csv = fs.readFileSync(filepath);
const [headers, ...rows] = parse(csv);
const [province, country, lat, long, ...dates] = headers;
const countList = {};
// HACK: CSVs have different date formats
const normalDates = dates.map(date => {
const [month, day] = date.split("/");
return `2020-${month}-${day}`;
});
rows.forEach(([province, country, lat, long, ...counts]) => {
countList[country] = countList[country] || {};
normalDates.forEach((date, i) => {
countList[country][date] = countList[country][date] || 0;
countList[country][date] += +counts[i];
});
});
return [countList, normalDates];
}
// HACK: Now all the names are the same, but leaving this just in case
const patchCountryNames = {};
function update(dataPath, outputPath) {
const [confirmed, dates] = extract(
path.resolve(dataPath, FILENAME_CONFIRMED)
);
const [deaths] = extract(path.resolve(dataPath, FILENAME_DEATHS));
const [recovered] = extract(path.resolve(dataPath, FILENAME_RECOVERED));
const countries = Object.keys(confirmed);
const results = {};
countries.forEach(country => {
// Some country names are different in the recovered dataset
const recoverdCountry = patchCountryNames[country] || country;
if (!recovered[recoverdCountry]) {
console.warn(`${recoverdCountry} is missing from the recovered dataset`);
}
results[country] = dates.map(date => {
return {
date,
confirmed: confirmed[country][date],
deaths: deaths[country][date],
recovered:
recovered[recoverdCountry] && recovered[recoverdCountry][date] != null
? recovered[recoverdCountry][date]
: null
};
});
});
fs.writeFileSync(outputPath, JSON.stringify(results, null, 2));
}
module.exports = update;