This repository has been archived by the owner on Apr 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
273 lines (244 loc) · 8.15 KB
/
index.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
267
268
269
270
271
272
273
const express = require("express");
const app = express();
const axios = require("axios");
const cheerio = require("cheerio");
console.log("-- Starting --");
const cache = {
"all": {},
"countries": [],
"states": []
};
const updateAll = async () => {
console.log("Updating all...");
let response;
try {
response = await axios.get("https://www.worldometers.info/coronavirus/");
if (response.status !== 200) {
console.log("ERROR");
}
} catch (err) {
return null;
}
const result = {};
const html = cheerio.load(response.data);
html(".maincounter-number").filter((i, el) => {
let count = el.children[0].next.children[0].data || "0";
count = parseInt(count.replace(/,/g, "") || "0", 10);
// first one is
if (i === 0) {
result.cases = count;
} else if (i === 1) {
result.deaths = count;
} else {
result.recovered = count;
}
});
console.log("Updated The Cases");
cache["all"] = result;
};
const updateCountries = async () => {
console.log("Updating countries...");
let response;
try {
response = await axios.get("https://www.worldometers.info/coronavirus/");
if (response.status !== 200) {
console.log("Error", response.status);
}
} catch (err) {
return null;
}
// to store parsed data
const result = [];
// get HTML and parse death rates
const html = cheerio.load(response.data);
const countriesTable = html("table#main_table_countries_today");
const countriesTableCells = countriesTable
.children("tbody")
.children("tr")
.children("td");
// NOTE: this will change when table format change in website
const totalColumns = 10;
const countryColIndex = 0;
const casesColIndex = 1;
const todayCasesColIndex = 2;
const deathsColIndex = 3;
const todayDeathsColIndex = 4;
const curedColIndex = 5;
const activeColIndex = 6;
const criticalColIndex = 7;
const casesPerOneMillionColIndex = 8;
const deathsPerOneMillionColIndex = 9;
// minus totalColumns to skip last row, which is total
for (let i = 0; i < countriesTableCells.length - totalColumns; i += 1) {
const cell = countriesTableCells[i];
// get country
if (i % totalColumns === countryColIndex) {
let country =
cell.children[0].data ||
cell.children[0].children[0].data ||
// country name with link has another level
cell.children[0].children[0].children[0].data ||
cell.children[0].children[0].children[0].children[0].data ||
"";
country = country.trim();
if (country.length === 0) {
// parse with hyperlink
country = cell.children[0].next.children[0].data || "";
}
result.push({
country: country.trim(),
});
}
// get cases
if (i % totalColumns === casesColIndex) {
let cases = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].cases = parseInt(
cases.trim().replace(/,/g, "") || "0",
10
);
}
// get today cases
if (i % totalColumns === todayCasesColIndex) {
let cases = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].todayCases = parseInt(
cases.trim().replace(/,/g, "") || "0",
10
);
}
// get deaths
if (i % totalColumns === deathsColIndex) {
let deaths = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].deaths = parseInt(
deaths.trim().replace(/,/g, "") || "0",
10
);
}
// get today deaths
if (i % totalColumns === todayDeathsColIndex) {
let deaths = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].todayDeaths = parseInt(
deaths.trim().replace(/,/g, "") || "0",
10
);
}
// get cured
if (i % totalColumns === curedColIndex) {
let cured = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].recovered = parseInt(
cured.trim().replace(/,/g, "") || 0,
10
);
}
// get active
if (i % totalColumns === activeColIndex) {
let cured = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].active = parseInt(
cured.trim().replace(/,/g, "") || 0,
10
);
}
// get critical
if (i % totalColumns === criticalColIndex) {
let critical = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].critical = parseInt(
critical.trim().replace(/,/g, "") || "0",
10
);
}
// get total cases per one million population
if (i % totalColumns === casesPerOneMillionColIndex) {
let casesPerOneMillion = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].casesPerOneMillion = parseFloat(
casesPerOneMillion.trim().replace(/,/g, "") || "0"
);
}
// get total deaths per one million population
if (i % totalColumns === deathsPerOneMillionColIndex) {
let deathsPerOneMillion = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].deathsPerOneMillion = parseFloat(
deathsPerOneMillion.trim().replace(/,/g, "") || "0"
);
}
}
console.log("Updated The Countries. (" + (result.length) + ")");
cache["countries"] = result;
};
const updateStates = async () => {
let response;
try {
response = await axios.get("https://www.worldometers.info/coronavirus/country/us/");
if (response.status !== 200) {
console.log("Error", response.status);
}
} catch (err) {
console.log(err)
return null;
}
// to store parsed data
const result = [];
// get HTML and parse death rates
const html = cheerio.load(response.data);
const statesTable = html("table#usa_table_countries_today");
const tableRows = statesTable
.children("tbody")
.children("tr:not(.total_row)").toArray();
// NOTE: this will change when table format change in website
const stateColIndex = 0;
const dataColIndexes = {
cases: 1,
todayCases: 2,
deaths: 3,
todayDeaths: 4,
active: 5
};
for (const row of tableRows) {
const cells = row.children.filter(c => c.name === "td");
const stateData = { state: parseStateCell(cells[stateColIndex]) };
for (const property in dataColIndexes)
stateData[property] = parseNumberCell(cells[dataColIndexes[property]]);
result.push(stateData);
}
cache["states"] = result;
console.log("Updated states.");
};
updateCountries().then(() => updateAll().then(() => updateStates()));
updateStates();
setInterval(() => {
updateAll();
updateCountries();
updateStates();
}, 600000)
app.get("/api/all/", async (req, res) => {
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(cache["all"], null, 4));
});
app.get("/api/countries/", async (req, res) => {
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(cache["countries"], null, 4));
});
app.get("/api/states/", async (req, res) => {
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(cache["states"], null, 4));
});
app.listen(9669, function() {
console.log("Listening on: 9669");
});
function parseStateCell(cell) {
let state =
cell.children[0].data ||
cell.children[0].children[0].data ||
// state name with link has another level
cell.children[0].children[0].children[0].data ||
cell.children[0].children[0].children[0].children[0].data ||
"";
state = state.trim();
if (state.length === 0) {
// parse with hyperlink
state = cell.children[0].next.children[0].data || "";
}
return state.trim() || "";
}
function parseNumberCell (cell) {
const cellValue = cell.children.length != 0 ? cell.children[0].data : "";
return parseFloat(cellValue.replace(/[,\+\-\s]/g, ""), 10) || 0;
}