-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
198 lines (158 loc) · 6.71 KB
/
script.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const fs = require('fs');
const readline = require('readline');
const format = require('date-fns/format');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// these are all assumed to be sorted.
let updatesFile = JSON.parse(fs.readFileSync('src/data/updates/updates.json'));
let sexFile = JSON.parse(fs.readFileSync('src/data/sex/sex.json'));
let casesFile = JSON.parse(fs.readFileSync('src/data/cases/cases.json'));
let provincesFile = JSON.parse(fs.readFileSync('src/data/provinces/provinces.json'));
const run = async () => {
console.log('Starting...');
try {
await workOnUpdates();
//await workOnSex();
await workOnCases();
await workOnProvinces();
} catch (e) {
console.log(e);
} finally {
rl.close();
}
};
const workOnUpdates = async () => {
const updates = updatesFile.updates;
const lastUpdate = updates[updates.length - 1];
const lastDate = new Date(lastUpdate.date);
const newDate = new Date(lastDate.valueOf() + 86400000);
const newDateFormatted = format(newDate, 'yyyy/MM/dd');
const lastNumber = parseInt(lastUpdate.title.substr(-3)); // Imagine having a thousand updates
const newNumber = lastNumber + 1;
console.log(`Last update was #${lastNumber} on ${lastUpdate.date}, continuing with #${newNumber} on ${newDateFormatted}`);
const wouldBeUrl = `http://digepisalud.gob.do/docs/Vigilancia%20Epidemiologica/Alertas%20epidemiologicas/Coronavirus/Nacional/Boletin%20Especial%20COVID-19/Boletin%20especial%20${newNumber}%20-%20COVID-19.pdf`;
const title = `Salud Pública publica boletín #${newNumber}`;
const date = newDateFormatted;
const link = await question(`URL? [${wouldBeUrl}]:`) || wouldBeUrl;
const update = { title, date, link };
updates.push(update);
const newFile = JSON.stringify({ updates }, null, 2);
fs.writeFileSync('src/data/updates/updates.json', newFile);
updatesFile = JSON.parse(fs.readFileSync('src/data/updates/updates.json'));
};
const workOnSex = async () => {
const sexes = sexFile.sex;
const lastUpdate = sexes[sexes.length - 1];
const lastDate = new Date(lastUpdate.date);
const newDate = new Date(lastDate.valueOf() + 86400000);
const newDateFormatted = format(newDate, 'yyyy/MM/dd');
const lastPercentForMale = (lastUpdate.men * 100).toFixed(2);
console.log(`Last update was on ${lastUpdate.date}, continuing with on ${newDateFormatted}`);
const percentForMale = await question(`Insert percent for men [${lastPercentForMale}]: `) || lastPercentForMale;
const men = parseFloat((percentForMale / 100).toFixed(4));
const women = parseFloat((1 - men).toFixed(4));
const sex = { date: newDateFormatted, men, women };
sexes.push(sex);
const newFile = JSON.stringify({ sex: sexes }, null, 2);
fs.writeFileSync('src/data/sex/sex.json', newFile);
sexFile = JSON.parse(fs.readFileSync('src/data/sex/sex.json'));
};
const workOnCases = async () => {
const cases = casesFile.cases;
const lastUpdate = cases[cases.length - 1];
const lastDate = new Date(lastUpdate.date);
const newDate = new Date(lastDate.valueOf() + 86400000);
const total_cases = await question('Total cases: ');
const new_cases = await question('New Cases: ');
const total_deaths = await question('Total Deaths: ');
const new_deaths = await question('New Deaths: ');
const total_recovered = await question('Total Recovered: ');
const total_discarded = await question('Total Discarded: ');
const total_at_the_hospital = await question('Total at the hospital: ');
const total_at_home = await question('Total at home: ');
const allFull = (
total_cases &&
total_deaths &&
new_cases &&
new_deaths &&
total_recovered &&
total_discarded &&
total_at_the_hospital &&
total_at_home
);
if (! allFull) {
console.log('Fill all');
await workOnCases();
return;
}
const date = format(newDate, 'yyyy/MM/dd');
const update = {
date,
total_cases: parseInt(total_cases.replace(',', '')),
total_deaths: parseInt(total_deaths.replace(',', '')),
new_cases: parseInt(new_cases.replace(',', '')),
new_deaths: parseInt(new_deaths.replace(',', '')),
total_recovered: parseInt(total_recovered.replace(',', '')),
total_discarded: parseInt(total_discarded.replace(',', '')),
total_at_the_hospital: parseInt(total_at_the_hospital.replace(',', '')),
total_at_home: parseInt(total_at_home.replace(',', '')),
};
cases.push(update);
const newFile = JSON.stringify({ cases }, null, 2);
fs.writeFileSync('src/data/cases/cases.json', newFile);
casesFile = JSON.parse(fs.readFileSync('src/data/cases/cases.json'));
};
const workOnProvinces = async () => {
const provinces = provincesFile.provinces;
for (let i = 0; i < provinces.length; i++) {
const province = provinces[i];
console.log(`\nFilling ${province.name} ... \n`);
const lastUpdate = province.cases[province.cases.length - 1];
const lastDate = new Date(lastUpdate.date);
const newDate = new Date(lastDate.valueOf() + 86400000);
const date = format(newDate, 'yyyy/MM/dd');
const lastTotalTests = lastUpdate.total_tests;
const lastTotalCases = lastUpdate.total_cases;
const lastPositivity = lastUpdate.positivity;
const lastTotalRecovered = lastUpdate.total_recovered;
const lastTotalDeaths = lastUpdate.total_deaths;
const totalTests = await question(`Total Tests [${lastTotalTests}]: `) || lastTotalTests;
const totalCases = await question(`Total Cases [${lastTotalCases}]: `) || lastTotalCases;
const totalPositivity = await question(`Positviity [${lastPositivity}]: `) || lastPositivity * 100;
const totalRecovered = await question(`Total Recovered [${lastTotalRecovered}]: `) || lastTotalRecovered;
const totalDeaths = await question(`Total Deaths [${lastTotalDeaths}]: `) || lastTotalDeaths;
const allFull = (
totalTests ||
totalCases ||
totalPositivity ||
totalRecovered ||
totalDeaths
);
if (! allFull) {
i--;
console.log('Fill all');
return;
}
const total_tests = parseInt(totalTests);
const total_cases = parseInt(totalCases);
const positivity = parseFloat(parseFloat(totalPositivity / 100).toFixed(4));
const total_recovered = parseInt(totalRecovered);
const total_deaths = parseInt(totalDeaths);
const day = {
date,
total_tests,
total_cases,
positivity,
total_recovered,
total_deaths,
};
provinces[i].cases.push(day);
}
const newFile = JSON.stringify({ provinces }, null, 2);
fs.writeFileSync('src/data/provinces/provinces.json', newFile);
provincesFile = JSON.parse(fs.readFileSync('src/data/provinces/provinces.json'));
};
const question = (title) => new Promise(resolve => rl.question(title, resolve));
run();