-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (40 loc) · 1.77 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
const MongoClient = require('mongodb').MongoClient;
const scrapy = require('node-scrapy');
const fetch = require('node-fetch');
const uri = process.env.DB_URL;
let data = [];
console.log("Process started")
const url = 'https://korona.gov.sk/koronavirus-na-slovensku-v-cislach/';
const model = {
infos: [
'.app-pane-gray',
{
data: '.govuk-body',
},
]
};
fetch(url).then((res) => res.text()).catch(console.error)
.then((body) => {
data = scrapy.extract(body, model).infos
console.log("Data extracted")
MongoClient.connect(uri, {useUnifiedTopology: true})
.then(client => {
const collection = client.db("covid-data").collection("daily-data");
const now = new Date();
const numOfTests = parseInt(data[0].data.substring(data[0].data.indexOf(":") + 2).replace(/\s/g, ""))
const confirmed = parseInt(data[1].data.substring(data[1].data.indexOf(":") + 2).replace(/\s/g, ""))
const deaths = parseInt(data[16].data.substring(data[16].data.indexOf(":") + 2).replace(/\s/g, ""))
now.setUTCHours(0, 0, 0, 0);
collection.findOneAndUpdate({"date": now}, {
$set: {
"numberOfTests": numOfTests,
"confirmed": confirmed,
"deaths": deaths,
"date": now
}
}, {upsert: true}).then(() => {
console.log("Data saved to database");
client.close().then().catch(console.error);
}).catch(err => console.error("Entry was not saved", err));
}).catch(err => console.error("Database unreachable:", err))
}).catch(console.error)