-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
343 lines (260 loc) · 14.3 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
var request = require('request');
var cheerio = require('cheerio');
var rp = require('request-promise');
const path = require('path');
var options = {
uri: "https://bnonews.com/index.php/2020/01/the-latest-coronavirus-cases/",
transform: function (body) {
return cheerio.load(body);
}
};
var options2 = {
uri: "https://www.worldometers.info/coronavirus/usa-coronavirus/",
transform: function (body) {
return cheerio.load(body);
}
};
var holder = []
function scrapeEvery10Minutes() {
// rp(options2).then(function($){
// // var usaData = {
// // states: []
// // }
// const states = $("body > div.container > div:nth-child(2) > div.col-md-8 > div > div.table-responsive > table > tbody > tr")
// .toArray()
// .map(element => $(element).text());
// console.log(states)
// }).catch(function (err) {
// console.log("There has been an error web scraping the USA link")
// });
rp(options)
.then(function ($) {
var data = {
chineseData: [
],
chineseSources: [
],
chineseRegionsData: [
],
chineseRegionsSources: [
],
internationalData: [
]
}
const timelineData = $("#mvp-content-main > ul")
.toArray()
.map(element => $(element).text());
day1 = $("#mvp-content-main > ul:nth-child(17) > li > a")
.toArray()
.map(element => $(element).attr("href"));
const timelineDates = $("#mvp-content-main > h4")
.toArray()
.map(element => $(element).text());
timelineDates.pop();
var timelineDataSplitArray = []
for (let index = 0; index < timelineData.length; index++) {
timelineDataSplitArray.push(timelineData[index].split(" (Source)"));
//timelineDataSplitArray[index][index].trim()
}
for (let index = 0; index < timelineDataSplitArray.length; index++) {
timelineDataSplitArray[index].pop()
}
var timelineArray = []
var timelineArrayDates = []
var timelineArraySources = []
for (let index = 0; index < timelineDataSplitArray.length; index++) {
timelineArray[index] = timelineDataSplitArray[index].map(str => str.trim().slice(7));
timelineArrayDates[index] = timelineDataSplitArray[index].map(str => str.trim().slice(0, 5));
}
timelineArray.shift();
timelineArray.shift();
timelineArrayDates.shift();
//Chinese data
const chineseDataStats = $("#mvp-content-main > table.wp-block-table > tbody > tr > td:not(:last-child)")
.toArray()
.map(element => $(element).text());
const quickFacts = $("#mvp-content-main > table.wp-block-table.aligncenter.is-style-stripes > tbody > tr:last-child > td:nth-child(4)")
.toArray()
.map(element => $(element).text());
const quickFactsData = quickFacts[0].split(/[^\d,]/)
.filter(str => str.trim() !== '');
const chineseSourcesData = $("#mvp-content-main > table.wp-block-table > tbody > tr > td:nth-child(5) > a")
.toArray()
.map(element => $(element).attr("href"));
//console.table(chineseDataStats);
// for (i = 0; i < chineseDataStats.length * (chineseDataStats.length + 10); i++) {
// if(chineseDataStats[i] == "Italy") {
// console.log(chineseDataStats[i]);
// console.log("included");
// console.log(i)
// }
// }
//chineseDataStats.splice(1,2, "");
//console.table(chineseDataStats[100])
for (i = 0; i < chineseDataStats.length * (chineseDataStats.length + 10); i++) {
data.chineseData.push(chineseDataStats.splice(0, 4));
const victims = data.chineseData[i][3].trim()
.split(', ')
.map(num => {
return num;
});
// if(victims[i].includes("serious")){
// console.log(victims[i][0])
// }
data.chineseData[i][3] = victims;
}
console.log(data.chineseData)
// for (let index = 0; index < data.chineseData.length; index++) {
// if(data.chineseData[index][3].contain){
// console.log(data.chineseData[index][3]);
// }
// }
//console.table(data.chineseData)
const mainlandChinaQuery = (element) => element[0] === 'MAINLAND CHINA';
var mainlandChinaIndex = data.chineseData.findIndex(mainlandChinaQuery);
// const regionsQuery = (element) => element[0] === 'OTHER PLACES';
// var regionsIndex = data.chineseData.findIndex(regionsQuery);
const internationalQuery = (element) => element[0] === 'OTHER PLACES';
var internationalIndex = data.chineseData.findIndex(internationalQuery);
var mainlandChinaData = data.chineseData.slice(Number(mainlandChinaIndex + 1), Number(internationalIndex - 1));
var mainlandChinaSources = chineseSourcesData.slice(Number(mainlandChinaIndex), Number(internationalIndex - 2));
// var regionData = data.chineseData.slice(Number(regionsIndex + 1), Number(internationalIndex - 1));
// var regionsSources = chineseSourcesData.slice(Number(regionsIndex - 1), Number(internationalIndex - 2));
var internationalData = data.chineseData.slice(Number(internationalIndex + 1), data.chineseData.length - 1);
var internationalSources = chineseSourcesData.slice(Number(internationalIndex - 4), data.chineseData.length);
internationalSources.shift();
console.log(internationalData)
//console.log(mainlandChinaData)
//console.log(internationalData)
//console.log(regionData[1]);
//internationalData.push(regionData[1])
//var fixedRegions = [regionData[0], regionData[2]]
//console.table(fixedRegions)
// var fixedRegions = {
// one: regionData[0],
// two: regionData[2]
// };
// // fixedRegions.push(regionsData);
// console.log(fixedRegions);
// console.table(internationalSources)
// var internationalAndRegions = []
// var regionsHolder = regionsData
// internationalAndRegions.push(internationalData);
// internationalAndRegions.concat(regionsData);
// console.log(internationalAndRegions)
var totalCases = 0;
var internationalCases = 0;
//console.table(data.chineseData)
for (let index = 0; index < mainlandChinaData.length; index++) {
totalCases += Number(mainlandChinaData[index][1].replace(/\D/g, ''));
}
// for (let index = 0; index < fixedRegions.length; index++) {
// totalCases += Number(fixedRegions[index][1].replace(/\D/g, ''));
// }
for (let index = 0; index < internationalData.length; index++) {
totalCases += Number(internationalData[index][1].replace(/\D/g, ''));
}
// for (let index = 0; index < fixedRegions.length; index++) {
// internationalCases += Number(fixedRegions[index][1].replace(/\D/g, ''));
// }
for (let index = 0; index < internationalData.length; index++) {
internationalCases += Number(internationalData[index][1].replace(/\D/g, ''));
}
var totalDead = 0;
for (let index = 0; index < mainlandChinaData.length; index++) {
totalDead += Number(mainlandChinaData[index][2].replace(/\D/g, ''));
}
// for (let index = 0; index < fixedRegions.length; index++) {
// totalDead += Number(fixedRegions[index][2].replace(/\D/g, ''));
// }
for (let index = 0; index < internationalData.length; index++) {
totalDead += Number(internationalData[index][2].replace(/\D/g, ''));
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
totalDead = numberWithCommas(totalDead);
totalCases = numberWithCommas(totalCases);
internationalCases = numberWithCommas(internationalCases);
//console.table(totalDead)
var totalCountries = internationalData.length + 1;
//internationalData.sort((a, b) => b[1] - a[1])
// const mongoose = require('mongoose');
// const uri = inserthere
// mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// const mainlandChinaStatsModel = require("./models/mainlandChina");
// const mainlandChinaStats = new mainlandChinaStatsModel({
// maindlandChinaArray: mainlandChinaData,
// chinaRegionsArray: regionData,
// internationalArray: internationalData,
// sources: mainlandChinaSources,
// regionsSources: regionsSources,
// internationalSources: internationalSources,
// quickFactsData: quickFactsData,
// totalConfirmed: totalCases,
// totalDead: totalDead,
// totalCountries: internationalData.length + 1,
// timelineDataSplitArray: timelineArray,
// timelineDates: timelineDates,
// timelineArrayDates: timelineArrayDates,
// timelineDataSources: timelineDataSources
// })
// mainlandChinaStats.save().then(result => {
// console.log(result)
// console.log("saved to database")
// }).catch(error => console.log(error));
// console.table(regionData);
// console.table(internationalData);
holder = [data, mainlandChinaData, "blank", internationalData, mainlandChinaSources, internationalSources, "blank", totalCases, totalDead, totalCountries, timelineArray, timelineDates, timelineArrayDates, day1, quickFactsData, internationalCases];
})
.catch(function (err) {
console.log("There has been an error web scraping, default to the database.")
});
setTimeout(scrapeEvery10Minutes, 600000);
}
scrapeEvery10Minutes();
const mongoose = require('mongoose');
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.set("view engine", "ejs");
// const uri = `[REDACTED]`
// mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// const mainlandChinaStatsModel = require("./models/mainlandChina");
app.get('/data', async (req, res) => {
// mainlandChinaStatsModel.findOne().sort({_id: -1}).exec(function(err, result) {
// if (err) { console.log(err) }
// })
res.render('data', { mainlandData: holder[1], mainlandChinaSources: holder[4], internationalData: holder[3], internationalSources: holder[6], quickFactsData: holder[14], totalConfirmed: holder[7], totalDead: holder[8], totalCountries: holder[9], internationalCases: holder[15] });
});
app.get('/', async (req, res) => {
res.render('data', { mainlandData: holder[1], mainlandChinaSources: holder[4], internationalData: holder[3], internationalSources: holder[6], quickFactsData: holder[14], totalConfirmed: holder[7], totalDead: holder[8], totalCountries: holder[9], internationalCases: holder[15] });
});
app.get('/timeline', async (req, res) => {
// console.log(holder[13]);
// console.log(holder[12]);
res.render('timeline', { timelineDataSplitArray: holder[10], timelineDates: holder[11], timelineArrayDates: holder[12], timelineDataSources: holder[13] });
});
app.get('/prevention', async (req, res) => {
res.render('prevention');
});
app.get('/about', async (req, res) => {
res.render('about');
});
app.get('/map', async (req, res) => {
res.render('map');
});
app.get('/wiki', async (req, res) => {
res.render('wiki');
});
app.get('/faq', async (req, res) => {
res.render('faq');
});
app.get('/tweets', async (req, res) => {
res.render('tweets');
});
app.get('/preparation', async (req, res) => {
res.render('prepping');
});
app.listen(process.env.PORT || 3000);
console.log("Listening on port: " + 3000);