forked from disease-sh/API
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathserver.js
231 lines (212 loc) · 6.98 KB
/
server.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
var express = require("express");
var app = express();
var request = require("request");
var axios = require("axios");
var cheerio = require("cheerio");
var db = require("quick.db");
var cors = require('cors')
app.use(cors());
app.use('/', express.static('www'));
var getall = setInterval(async () => {
let response;
try {
response = await axios.get("https://www.worldometers.info/coronavirus/");
if (response.status !== 200) {
console.log("ERROR");
}
} catch (err) {
return null;
}
// to store parsed data
const result = {};
// get HTML and parse death rates
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;
}
});
db.set("all", result);
console.log("Updated The Cases", result);
}, 150000);
var getcountries = setInterval(async () => {
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:not(.row_continent)")
.children("td");
// count worldometers table columns
const colCount = html('table#main_table_countries_today th').length;
const totalColumns = colCount;
const countryColIndex = 1;
const casesColIndex = 2;
const todayCasesColIndex = 3;
const deathsColIndex = 4;
const todayDeathsColIndex = 5;
const curedColIndex = 6;
const activeColIndex = 8;
const criticalColIndex = 9;
const casesPerOneMillionColIndex = 10;
const deathsPerOneMillionColIndex = 11;
const testsColIndex = 12;
const testsPerOneMillionColIndex = 13;
// 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] && 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 = "";
if(cell.children.length != 0) {
cured = cell.children[0].children ? cell.children[0].children[0].data : 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 = parseInt(
casesPerOneMillion.trim().replace(/,/g, "") || "0",
10
);
}
// 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 = parseInt(
deathsPerOneMillion.trim().replace(/,/g, "") || "0",
10
);
}
// get total tests
if (i % totalColumns === testsColIndex) {
let totalTests = cell.children.length != 0? cell.children[0].data : "";
result[result.length - 1].totalTests = parseInt(
totalTests.trim().replace(/,/g, "") || "0",
10
);
}
// get total tests per one million population
if (i % totalColumns === testsPerOneMillionColIndex) {
let testsPerOneMillion = cell.children.length != 0? cell.children[0].data : "";
result[result.length - 1].testsPerOneMillion = parseInt(
testsPerOneMillion.trim().replace(/,/g, "") || "0",
10
);
}
}
db.set("countries", result);
console.log("Updated The Countries", result);
}, 150000);
var listener = app.listen(process.env.PORT, function() {
console.log("Your app is listening on port " + listener.address().port);
});
app.get("/all/", async function(req, res) {
let all = await db.fetch("all");
res.send(all);
});
app.get("/countries/", async function(req, res) {
let countries = await db.fetch("countries");
res.send(countries);
});
app.get("/countries/:country", async function(req, res) {
let countries = await db.fetch("countries");
let country = countries.find(
e => {
if(e.country.toLowerCase().localeCompare(req.params.country.toLowerCase()) === 0)
{
return true;
}
});
if (!country) {
res.send("Country not found");
return;
}
res.send(country);
});