This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
daemon.js
executable file
·307 lines (267 loc) · 8.67 KB
/
daemon.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
#!/usr/bin/env node
//TODO: Only a single merge to master pls
require('dotenv').config();
const schedule = require('node-schedule');
var shell = require('shelljs');
const fetch = require('node-fetch');
const fs = require('fs');
const Pusher = require('pusher');
const scrapSesaram = require('./automation/sesaram');
const convertCases = require('./automation/convert-csv:cases');
const scrapRt = require('./automation/convert-xls');
var argv = require('minimist')(process.argv.slice(2));
console.log(process.env.HARDWARE);
let json = require('./data/last-update');
const convertVaccines = require('./automation/convert-csv:vaccines');
const { isAfter } = require('date-fns');
const { exit } = require('process');
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git');
shell.exit(1);
}
let options = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
};
let f = new Intl.DateTimeFormat('pt', options);
let formatted = f.format(new Date());
const pusher = new Pusher({
appId: process.env.PUSHER_APP_ID,
key: process.env.PUSHER_APP_KEY,
secret: process.env.PUSHER_APP_SECRET,
cluster: 'eu',
useTLS: true,
});
function gitCommit(name, merge = true) {
shell.exec('git add *');
if (shell.exec(`git commit -m "covid update - ${name} - ${formatted}"`).code !== 0) {
shell.echo('Error: Git commit failed');
return;
} else {
shell.echo('Success: Git commit success');
}
shell.exec('git push');
if (merge) {
shell.exec('git checkout master');
shell.exec('git pull --rebase');
shell.exec('git merge develop --no-ff --no-edit');
shell.exec('git push');
shell.exec('git checkout develop');
}
}
function updateOWID() {
shell.exec('python3 ./automation/owid_parser.py');
gitCommit('owid');
}
// Called .py directly from the workflows
/* function updateEDCD() {
shell.exec('git checkout develop');
shell.exec('git pull --rebase');
shell.exec('python3 ./automation/ecdc_parser.py');
gitCommit('ecdc');
} */
function updatedCasesMadeira() {
shell.exec('yarn convert:csv'); //update aos acores
gitCommit('casos açores');
shell.exec('yarn casos:ram'); //update aos casos da madeira
}
async function updateRT() {
console.log('update rt');
await scrapRt(function () {
gitCommit('rt');
});
}
/*
function publishEvent(type, data) {
pusher.trigger('covid19', 'update', {
type,
data,
});
}
async function updateJSON() {
//update the repo
shell.exec('git checkout develop');
shell.exec('git pull --rebase');
let date = new Date();
date.setMinutes(0);
date.setMilliseconds(0);
date.setSeconds(0);
date.setHours(0);
let dataLocalVacinas = JSON.parse(fs.readFileSync('./data/vaccines.json'));
let dataLocalVacinasV2 = JSON.parse(fs.readFileSync('./data/vaccines_v2.json'));
let dataLocalCases = JSON.parse(fs.readFileSync('./data/cases.json'));
if (date.getTime() > dataLocalVacinas[dataLocalVacinas.length - 1].Data) {
let dataVacinas = await fetch(
'https://services5.arcgis.com/eoFbezv6KiXqcnKq/arcgis/rest/services/Covid19_Total_Vacinados/FeatureServer/0/query?f=json&where=FID=1&outFields=*&resultType=standard&cacheHint=true'
).then((res) => res.json());
var sourceData = dataVacinas.features[0].attributes;
if (parseInt(sourceData.Vacinados_Ac) > dataLocalVacinas[dataLocalVacinas.length - 1].Vacinados_Ac) {
console.log('updating vaccines');
sourceData.Data = date.getTime();
dataLocalVacinas.push(sourceData);
fs.writeFileSync('./data/vaccines.json', JSON.stringify(dataLocalVacinas));
let item = dataLocalVacinas[dataLocalVacinas.length - 1];
item.Inoculacao1 = item.Inoculacao1_Ac - dataLocalVacinas[dataLocalVacinas.length - 2].Inoculacao1_Ac;
item.Inoculacao2 = item.Inoculacao2_Ac - dataLocalVacinas[dataLocalVacinas.length - 2].Inoculacao2_Ac;
item.Vacinados = item.Vacinados_Ac - dataLocalVacinas[dataLocalVacinas.length - 2].Vacinados_Ac;
dataLocalVacinasV2.push(item);
fs.writeFileSync('./data/vaccines_v2.json', JSON.stringify(dataLocalVacinasV2));
updatedVaccines = true;
json.date = new Date();
json.dateVaccines = sourceData.Data;
fs.writeFileSync('./data/last-update.json', JSON.stringify(json));
gitCommit('vaccines');
//Update twitter
if (process.env.HARDWARE == 'raspberry') {
shell.exec('sleep 180');
publishEvent('vacinas', dataVacinas.features[0].attributes);
shell.exec('yarn twitter');
shell.exec('yarn notification:push');
// bot runs on a raspberry pi
shell.exec('sleep 180');
shell.exec('sudo poweroff');
}
} else {
console.log(
new Date(),
'not updating',
'vaccines',
parseInt(sourceData.Vacinados_Ac),
dataLocalVacinas[dataLocalVacinas.length - 1].Vacinados_Ac,
new Date(sourceData.Data),
new Date(date.getTime()),
sourceData.Data > date.getTime()
);
console.log(new Date(), 'not updating', 'vaccines', date.getTime(), dataLocalVacinas[dataLocalVacinas.length - 1].Data);
}
} else {
console.log('Not fetching new vaccines');
}
}
*/
async function updateVaccinesDssg(cb = null) {
let vac_local = JSON.parse(fs.readFileSync('./data/vaccines_dssg.json'));
const local_date = new Date(vac_local.reverse()[0].data_vac_iso);
await convertVaccines(
(vac_remote) => {
const vac_reverse = vac_remote.reverse();
const remote_date = new Date(vac_reverse[0].data_vac_iso);
const updated = remote_date.getTime() > local_date.getTime();
if (updated) {
fs.writeFileSync('./data/vaccines_dssg.json', JSON.stringify(vac_remote.reverse()));
json.date = new Date();
json.dateVaccines = remote_date;
fs.writeFileSync('./data/last-update.json', JSON.stringify(json));
gitCommit('vaccines-dssgpt');
//Update twitter
if (
vac_reverse[0].doses !== null &&
vac_reverse[0].doses2 !== null &&
(process.env.HARDWARE == 'raspberry' || process.env.HARDWARE == 'ci')
) {
//shell.exec('sleep 180');
shell.exec('yarn twitter');
shell.exec('yarn notification:push');
shell.exec('git checkout develop');
shell.exec('git pull --rebase');
gitCommit('update-dates-locks', false);
}
if (process.env.HARDWARE == 'raspberry') {
shell.exec('sleep 180');
shell.exec('sudo poweroff');
}
if (cb) cb();
} else {
console.log('not updated');
console.log(vac_remote[0]);
}
},
true,
undefined,
true
);
}
async function updateCasesDssg(cb = null) {
const jsonArrayObj = await convertCases();
fs.writeFileSync('./data/cases_v2.json', JSON.stringify(jsonArrayObj));
json.date = new Date();
json.dateCases = jsonArrayObj.reverse()[0].data_cases;
fs.writeFileSync('./data/last-update.json', JSON.stringify(json));
gitCommit('cases-dssgpt');
}
console.log(new Date().toLocaleString(), 'daemon running');
// ““At every 5th minute from 0 through 59 past hour 13.”
// https://crontab.guru/#0-59/5_13_*_*_*
(async () => {
if (argv.scrap) {
//Run particular commands
shell.exec('git checkout develop');
shell.exec('git pull --rebase');
switch (argv.scrap) {
case 'sesaram':
await scrapSesaram(function () {
gitCommit('sesaram');
});
break;
/* case 'vaccines-sns':
await updateJSON();
break; */
case 'cases':
await updateCasesDssg();
break;
case 'vaccines':
await updateVaccinesDssg();
break;
case 'owid':
updateOWID();
await updateRT();
updatedCasesMadeira();
break;
}
process.exit(0);
} else {
//Set the schedule
//Try publish after 14h
schedule.scheduleJob('0-59/5 14-15 * * *', function () {
updateVaccinesDssg();
});
//If its running after 16h is because the data wasn't updated
schedule.scheduleJob('*/20 16-18 * * *', function () {
updateVaccinesDssg();
});
if (process.env.HARDWARE == 'raspberry') {
schedule.scheduleJob('10 16 * * *', function () {
shell.exec('sleep 180');
shell.exec('sudo poweroff');
});
}
/*
// This is done by docker
//Update SESARAM at midnight again
schedule.scheduleJob('50 23 * * *', function () {
shell.exec('git checkout develop');
shell.exec('git pull --rebase');
scrapSesaram(function () {
gitCommit('sesaram');
});
});
schedule.scheduleJob('20 21 * * *', function () {
console.log('Saving to web archive');
shell.exec('waybackpy --save --url "https://www.sns.gov.pt/monitorizacao-do-sns/vacinas-covid-19/"');
});
schedule.scheduleJob('30 21 * * *', function () {
console.log('Saving to web archive');
shell.exec('waybackpy --save --url "https://vacinacao-covid19.azores.gov.pt/" ');
});
schedule.scheduleJob('40 21 * * *', function () {
console.log('Saving to web archive');
shell.exec('waybackpy --save --url "https://web.sesaram.pt/COVID19_INFO" ');
}); */
/* schedule.scheduleJob('00 12-30 * * *', function () {
updateOWID();
updateRT();
updatedCasesMadeira();
}); */
}
})();