This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-data.js
269 lines (227 loc) · 7.9 KB
/
get-data.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const axios = require('axios');
const moment = require("moment");
const cheerio = require("cheerio");
const fs = require('fs').promises;
const prettyCompactStringify = require("json-stringify-pretty-compact");
const BASE_URL = "https://services5.arcgis.com/Th0Tmkhiy5BQYoxP/arcgis/rest/services/Casos_DepartamentosROU_vista_2/FeatureServer/";
const REPORT_BASE_URL = "https://www.gub.uy/sistema-nacional-emergencias/comunicacion/comunicados/informe-situacion-sobre-coronavirus-covid-19-uruguay-";
async function request(url, params) {
if (params) {
url += "?" + new URLSearchParams(params).toString();
}
try {
let response;
response = await axios.get(url)
if (response.status !== 200) {
throw new Error('Unexpected HTTP code when downloading data: ' + response.status);
}
return response.data;
} catch (e) {
throw e;
}
}
async function queryIntValue(type, onStatisticField) {
const url = BASE_URL + type + "/query"
const where = (type == 1 ? "publicar_sino=1" : "1=1");
const outStatistics = [
{
statisticType: "sum",
onStatisticField: onStatisticField,
outStatisticFieldName: "value"
}
]
const params = {
f: "json",
where: where,
returnGeometry: false,
outStatistics: JSON.stringify(outStatistics),
resultType: "standard",
cacheHint: "false"
};
const data = await request(url, params);
let value = 0;
try {
value = data.features[0].attributes.value;
}
catch (e) {
console.error("Error in queryIntValue when getting " + onStatisticField + ": " + e.message);
}
const parsedValue = parseInt(value)
if (isNaN(parsedValue)) {
console.error("NaN value when getting " + onStatisticField);
value = 0;
}
return value;
}
async function getUpdatedDate() {
const data = await request(BASE_URL + '0/', { f: "json" });
let date = null;
try {
date = moment(data.editingInfo.lastEditDate);
}
catch (e) {
date = moment();
console.error("Error getting updated date: " + e.message);
}
return date.format("YYYY-MM-DD")
}
async function fetchMonitorData() {
const [date, tests, activeCases, cases, recovered, deaths, icu, hc, hcRecovered, hcDeaths, newCases] = await Promise.all([
getUpdatedDate(),
queryIntValue(1, "test_procesados"),
queryIntValue(0, "CasosActivos"),
queryIntValue(0, "Casos_Positivos"),
queryIntValue(0, "Casos_Recuperados"),
queryIntValue(0, "Fallecidos"),
queryIntValue(1, "casos_cuidados_intensivos"),
queryIntValue(1, "positivos_personal_salud"),
queryIntValue(1, "Pacientes_Recuperados"),
queryIntValue(1, "Fallecidos"),
queryIntValue(1, "Casos_Sospechosos")
]);
let data = {
date: date,
tests: tests,
cases: cases,
recovered: recovered,
deaths: deaths,
icu: icu,
hc: hc,
hcRecovered: hcRecovered,
hcDeaths: hcDeaths,
newCases: newCases
};
if (activeCases != (cases - recovered - deaths)) {
data.activeCases = activeCases;
}
return data;
}
function getTDValue(td) {
try {
let value = null;
if (td) {
let elem = td;
while (elem.children && elem.children.length > 0) {
elem = elem.children[0];
}
value = elem.data;
}
if (!value) {
value = "";
}
return value.trim();
}
catch (e) {
console.error("Error getting td value: " + e.message);
return 0;
}
}
async function fetchReportData() {
const todayDate = moment();
const todayDatStr = todayDate.format("YYYY-MM-DD");
const reportURL = REPORT_BASE_URL + todayDate.format("DDMMYYYY");
const reportData = await request(reportURL);
const day = {
date: todayDatStr,
deps: {}
};
const html = cheerio.load(reportData.replace(/[\n]/g, ''));
const rows = html('tbody tr');
let lastDep = null
let headersSkipped = false;
for (let i = 0; i < rows.length; ++i) {
const row = rows[i];
const tdSex = row.children[0];
const tdAge = row.children[1];
const tdDep = row.children[2];
const age = getTDValue(tdAge);
const sex = getTDValue(tdSex);
const depData = getTDValue(tdDep);
if (!headersSkipped && row.children.length == 3 && sex.length == 1 && !isNaN(parseInt(age)) && depData.length > 4) {
headersSkipped = true;
}
if (headersSkipped) {
if (row.children.length > 1) {
let dep = lastDep;
if (tdDep != undefined) {
const words = depData.split(" ");
dep = words.map((word) => {
if (word.length > 1) {
return word[0].toUpperCase() + word.substring(1).toLowerCase();
}
else {
return word.toLowerCase();
}
}).join(" ");
dep = dep.replace("Paysandu", "Paysandú");
dep = dep.replace("Rio Negro", "Río Negro");
dep = dep.replace("San Jose", "San José");
dep = dep.replace("Tacuarembo", "Tacuarembó");
lastDep = dep;
}
if (age != null && age.length > 0) {
if (!day.deps[dep]) {
day.deps[dep] = [];
}
day.deps[dep].push({
age: parseInt(age),
s: sex
});
}
}
else {
console.error("Unexpected row length of " + row.length);
}
}
}
let deleted = 0;
const text = html('.Page-document p').text();
const deletedRegExp = /(?<deleted>\d+)(\scasos fueron eliminados)/;
const res = text.match(deletedRegExp);
if (res != null) {
deleted = parseInt(res.groups.deleted);
}
return {
deaths: day,
deleted: deleted
};
}
const URUGUAY_FILE = 'assets/js/data/uruguay.json';
const DEATHS_FILE = 'assets/js/data/uruguayDeaths.json';
async function updateUruguayData(data) {
const uruguayFileData = await fs.readFile(URUGUAY_FILE);
const uruguayData = JSON.parse(uruguayFileData);
let lastIndex = uruguayData.data.length - 1;
if (lastIndex >= 0 && uruguayData.data[lastIndex].date == data.date) {
uruguayData.data.pop();
}
uruguayData.data.push(data);
await fs.writeFile(URUGUAY_FILE, JSON.stringify(uruguayData, null, 4));
}
async function updateUruguayDeathsData(deathsData) {
if (Object.keys(deathsData.deps).length > 0) {
const deathsFileData = await fs.readFile(DEATHS_FILE);
const uruguayDeathsData = JSON.parse(deathsFileData);
lastIndex = uruguayDeathsData.days.length - 1;
if (lastIndex >= 0 && uruguayDeathsData.days[lastIndex].date == deathsData.date) {
uruguayDeathsData.days.pop();
}
uruguayDeathsData.days.push(deathsData);
await fs.writeFile(DEATHS_FILE, prettyCompactStringify(uruguayDeathsData, { indent: 4, maxLength: 4096 }));
}
}
(async function () {
const fetchRes = await Promise.all([fetchMonitorData(), fetchReportData()]);
const data = fetchRes[0];
const reportData = fetchRes[1];
if (reportData.deleted > 0) {
data.lateDeletedCases = reportData.deleted;
}
const deathsData = reportData.deaths;
console.log(JSON.stringify(data));
console.log("\n" + JSON.stringify(deathsData));
await Promise.all([updateUruguayData(data), updateUruguayDeathsData(deathsData)]);
})().catch(e => {
console.error(e.stack);
process.exit(1);
});