-
Notifications
You must be signed in to change notification settings - Fork 1
/
booking.js
48 lines (40 loc) · 1.44 KB
/
booking.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
const fs = require('fs');
const cron = require('node-cron');
const moment = require('moment');
const Web = require('./tools/web.js');
const Util = require('./tools/util.js');
const FetchMachine = require('./tools/fetch-machine.js');
if (!fs.existsSync('./dist')) {
fs.mkdirSync('./dist');
}
if (!fs.existsSync('./dist/log.txt')) {
fs.writeFileSync('./dist/log.txt', '');
}
if (process.env.runWithoutCron === 'true') {
main();
} else {
cron.schedule('55 59 15 * * *', main);
}
async function main(){
try {
fs.appendFileSync('./dist/log.txt','start\r\n');
const urlRawData = fs.readFileSync('./config/urls.json');
const secretsRawData = fs.readFileSync('./config/secrets.json');
const urls = JSON.parse(urlRawData);
const secrets = JSON.parse(secretsRawData);
const now = moment().add(14,'days');
const shortDateString = now.toISOString().split('T')[0].split('-').join('/');
if(!urls[now.day().toString()]){
return;
}
const sessionId = await Web.login(secrets.id,secrets.pwd);
Util.log(`sessionId: ${sessionId}`);
const fetchMachine = new FetchMachine(urls[now.day().toString()], 1000, sessionId, secrets.sendgridApiKey);
fetchMachine.run({
date: shortDateString,
dayIndex: now.day()
});
} catch (error) {
Util.log(`sessionId: ${JSON.stringify(error)}`);
}
}