-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
123 lines (105 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
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
const puppeteer = require('puppeteer');
const slugify = require('slugify');
const nodemailer = require('nodemailer');
const {
GMAIL_EMAIL,
GMAIL_PASSWORD,
MUNI_PASSWORD,
MUNI_USERNAME,
sendEmailIfSleepingInBoardingHouse,
headless
} = require('./config');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: GMAIL_EMAIL,
pass: GMAIL_PASSWORD
},
});
const send = (mailOptions) => {
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err);
} else {
console.log(info);
}
});
};
let sleepingInBoardingHouse = false;
(async () => {
const browser = await puppeteer.launch({
headless,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
/**
* Login to iskam
*/
await page.goto('https://iskam.skm.muni.cz');
await page.click('#logonButt');
await page.waitForSelector('#username');
await page.type('#username', MUNI_USERNAME, { delay: 100 });
await page.type('#password', MUNI_PASSWORD, { delay: 100 });
await page.click('.btn-wrap button');
await page.waitForSelector('.loginout');
/**
* Change language and search for blocks
*/
await page.goto('https://iskam.skm.muni.cz/Localize/ChangeLang?returnUrl=%2FNovaRezervace&lang=en-US');
await page.waitForSelector('#buttSeek');
const elements = (await page.$$('#selBlok option')).length;
for (let index = 1; index < elements; index++) {
const name = await page.evaluate((index) => {
return document.querySelector(`#selBlok > option:nth-child(${index})`).textContent
}, index);
const value = await page.evaluate((index) => {
return document.querySelector(`#selBlok > option:nth-child(${index})`).value
}, index);
const fileName = `./type-${slugify(name)}.png`;
await page.select('#selBlok', `${value}`);
await page.click('#buttSeek');
await page.waitForSelector('h3.text-centering');
await page.screenshot({
path: fileName
});
const text = await page.evaluate(() => document.querySelector('h3.text-centering').textContent);
if (text !== 'No available beds on this block in the specified date range.') {
const mailOptions = {
from: GMAIL_EMAIL,
to: GMAIL_EMAIL,
subject: 'YES Intrak',
html: 'Intrak',
attachments: [
{
filename: fileName,
path: fileName,
cid: fileName
}
]
};
sleepingInBoardingHouse = true;
send(mailOptions);
} else {
console.log('nope')
}
await page.waitForSelector('#buttSeek');
await page.goto('https://iskam.skm.muni.cz/NovaRezervace');
}
if (!sleepingInBoardingHouse && sendEmailIfSleepingInBoardingHouse) {
const mailOptions = {
from: GMAIL_EMAIL,
to: GMAIL_EMAIL,
subject: 'NO Intrak',
html: 'Spis pod mostom',
attachments: [
{
filename: './bridge.jpg',
path: './bridge.jpg',
cid: './bridge.jpg'
}
]
};
send(mailOptions);
}
await browser.close();
})();