-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
99 lines (79 loc) · 3.5 KB
/
main.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
const Apify = require('apify');
const sourceUrl = 'https://covid19.isciii.es/';
const LATEST = 'LATEST';
let check = false;
Apify.main(async () => {
const kvStore = await Apify.openKeyValueStore('COVID-19-ES');
const dataset = await Apify.openDataset('COVID-19-ES-HISTORY');
const { email } = await Apify.getValue('INPUT');
console.log('Launching Puppeteer...');
const browser = await Apify.launchPuppeteer();
const page = await browser.newPage();
await Apify.utils.puppeteer.injectJQuery(page);
console.log('Going to the website...');
await page.goto(sourceUrl, { waitUntil: 'networkidle0', timeout: 60000 });
console.log('Getting data...');
// page.evaluate(pageFunction[, ...args]), pageFunction <function|string> Function to be evaluated in the page context, returns: <Promise<Serializable>> Promise which resolves to the return value of pageFunction
const result = await page.evaluate(() => {
const now = new Date();
// eq() selector selects an element with a specific index number, text() method sets or returns the text content of the selected elements
const infected = $('#casos').text();
const deceased = $('#defunciones').text();
const recovered = $("#recuperados").text();
const hospitalised = $("#hospitalizados").text();
const regionsTableRows = Array.from(document.querySelectorAll("table tbody tr"));
const regionData = [];
for(const row of regionsTableRows){
const cells = Array.from(row.querySelectorAll("td")).map(td=> td.textContent);
regionData.push({region: cells[0], total: cells[1], lastDay: cells[2], inc14d: cells[3]});
}
const data = {
infected: infected,
deceased: deceased,
recovered: recovered,
hospitalised: hospitalised,
sourceUrl: 'https://covid19.isciii.es/',
lastUpdatedAtApify: new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes())).toISOString(),
readMe: 'https://github.com/zpelechova/covid-es/blob/master/README.md',
regions: regionData,
};
return data;
});
console.log(result)
if (!result.infected || !result.deceased || !result.recovered) {
check = true;
}
else {
let latest = await kvStore.getValue(LATEST);
if (!latest) {
await kvStore.setValue('LATEST', result);
latest = result;
}
delete latest.lastUpdatedAtApify;
const actual = Object.assign({}, result);
delete actual.lastUpdatedAtApify;
if (JSON.stringify(latest) !== JSON.stringify(actual)) {
await dataset.pushData(result);
}
await kvStore.setValue('LATEST', result);
await Apify.pushData(result);
}
console.log('Closing Puppeteer...');
await browser.close();
console.log('Done.');
// if there are no data for TotalInfected, send email, because that means something is wrong
const env = await Apify.getEnv();
if (check) {
await Apify.call(
'apify/send-mail',
{
to: email,
subject: `Covid-19 ES from ${env.startedAt} failed `,
html: `Hi, ${'<br/>'}
<a href="https://my.apify.com/actors/${env.actorId}#/runs/${env.actorRunId}">this</a>
run had 0 TotalInfected, check it out.`,
},
{ waitSecs: 0 },
);
};
});