-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
92 lines (73 loc) · 3.49 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
const Apify = require('apify');
const sourceUrl = 'http://www.moh.gov.my/index.php/pages/view/2019-ncov-wuhan';
const LATEST = 'LATEST';
let check = false;
Apify.main(async () => {
const kvStore = await Apify.openKeyValueStore('COVID-19-MY');
const dataset = await Apify.openDataset('COVID-19-MY-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...');
const result = await page.evaluate(() => {
const now = new Date();
const testedPositive = $('#container_content > div.editable > center:nth-child(10) > table > tbody > tr:nth-child(1) > td:nth-child(2) > span').text();
const testedNegative = $("#container_content > div.editable > center:nth-child(10) > table > tbody > tr:nth-child(2) > td:nth-child(2) > span").text();
const recovered = $("#container_content > div.editable > center:nth-child(11) > table > tbody > tr:nth-child(1) > td:nth-child(2) > span").text();
const inICU = $("#container_content > div.editable > center:nth-child(11) > table > tbody > tr:nth-child(2) > td:nth-child(2) > span").text();
const deceased = $("#container_content > div.editable > center:nth-child(11) > table > tbody > tr:nth-child(3) > td:nth-child(2) > span").text();
const data = {
testedPositive: testedPositive,
testedNegative: testedNegative,
testedTotal: Number(testedPositive) + Number(testedNegative),
recovered: recovered,
inICU: inICU,
deceased: deceased,
sourceUrl: 'http://www.moh.gov.my/index.php/pages/view/2019-ncov-wuhan',
lastUpdatedAtApify: new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes())).toISOString(),
readMe: 'https://github.com/zpelechova/covid-my/blob/master/README.md',
};
return data;
});
console.log(result)
if (!result.testedTotal || !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 MY 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 },
);
};
});