forked from katacek/covid-philippines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
114 lines (87 loc) · 3.43 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const Apify = require('apify');
const sourceUrl = 'https://ncovtracker.doh.gov.ph/';
const LATEST = 'LATEST';
let check = false;
Apify.main(async () =>
{
const kvStore = await Apify.openKeyValueStore('COVID-19-PH');
const dataset = await Apify.openDataset('COVID-19-PH-HISTORY');
//const { email } = await Apify.getValue('INPUT');
try
{
const now = new Date();
const sheetsInput = {
mode: 'read',
publicSpreadsheet: true,
spreadsheetId: '1BLbrvgjkBWxr9g73xX9DLOqmbmuYyKc-_b8jIxCX1uo', // update to your ID
range:"Case Information!H:H"
};
const myData = (await Apify.call('lukaskrivka/google-sheets', sheetsInput)).output.body;
const confirmed = myData.length;
const recovered = myData.filter(x => x.RemovalType =='Recovered').length;
const deceased = myData.filter(x => x.RemovalType == 'Died').length;
const result = {
infected: confirmed,
tested: "N/A",
recovered: recovered,
deceased: deceased,
//PUIs: getInt(PUIs),
//PUMs: getInt(PUMs),
country: "Philippines",
historyData: "https://api.apify.com/v2/datasets/sFSef5gfYg3soj8mb/items?format=json&clean=1",
sourceUrl:'https://ncovtracker.doh.gov.ph/',
lastUpdatedAtApify: new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes())).toISOString(),
lastUpdatedAtSource: "N/A",
readMe: 'https://apify.com/katerinahronik/covid-philippines',
};
console.log(result)
if ( !result.infected || !result.deceased|| !result.recovered) {
throw "One of the output is null";
}
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 Philippines from ${env.startedAt} failed `,
// html: `Hi, ${'<br/>'}
// <a href="https://my.apify.com/actors/${env.actorId}#/runs/${env.actorRunId}">this</a>
// run had 0 in one of the variables, check it out.`,
// },
// { waitSecs: 0 },
// );
// };
}
catch(err){
console.log(err)
let latest = await kvStore.getValue(LATEST);
var latestKvs = latest.lastUpdatedAtApify;
var latestKvsDate = new Date(latestKvs)
var d = new Date();
// adding two hours to d
d.setHours(d.getHours() - 2);
if (latestKvsDate < d) {
throw (err)
}
}
});