-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (134 loc) · 4.93 KB
/
index.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
import dotenv from 'dotenv';
dotenv.config();
import puppeteer from 'puppeteer';
//library credentials
const LIBRARY_CARD_NUMBER = process.env.LIBRARY_CARD_NUMBER;
const LIBRARY_CARD_PIN = process.env.LIBRARY_CARD_PIN;
//zoho credentials
const ZOHO_EMAIL = process.env.ZOHO_EMAIL
const ZOHO_PASSWORD = process.env.ZOHO_PASSWORD
//recipient email (email that notification will be mailed)
const RECIPIENT_EMAIL= process.env.RECIPIENT_EMAIL
//.env settings
const CHECK_FOR_OVERDUE = process.env.CHECK_FOR_OVERDUE
const CHECK_FOR_DUE_SOON = process.env.CHECK_FOR_DUE_SOON
const CHECK_FOR_READY_FOR_PICKUP = process.env.CHECK_FOR_READY_FOR_PICKUP
const CHECK_FOR_IN_TRANSIT = process.env.CHECK_FOR_IN_TRANSIT
//launch the browser
const browser = await puppeteer.launch({ headless: false, args: ['--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'] });
//open a new page
const page = await browser.newPage();
//go to the library website
page.goto("https://account.torontopubliclibrary.ca/signin")
// Enter username
const userID = '#userID';
await page.waitForSelector(userID);
await page.type(userID, LIBRARY_CARD_NUMBER);
//enter password
const passwordInput = '#password';
await page.waitForSelector(passwordInput);
await page.type(passwordInput, LIBRARY_CARD_PIN);
// Submit the form
await page.keyboard.press('Enter');
//wait for page to load
await waitForMultipleElements(page, ".sub-menu-content", 7)
// Get the account information
const values = await page.$$('.sub-menu-content');
const data = []
for (let i = 0; i < values.length; i++) {
// Get the text content of title and value
const valueText = await page.evaluate(value => value.textContent, values[i]);
data.push(valueText)
}
//create an object with data
const accountInfo = {
overdue: parseInt(data[0]),
dueSoon: parseInt(data[1]),
dueLater: parseInt(data[2]),
readyForPickup: parseInt(data[3]),
inTransit: parseInt(data[4]),
activeHolds: parseInt(data[5]),
inactiveHolds: parseInt(data[6])
}
let notify = false
let message = ""
// check for overdue books
if (CHECK_FOR_OVERDUE === "true") {
if (accountInfo.overdue > 0) {
notify = true
message += `You have ${accountInfo.overdue} books overdue. `
}
}
//check for books due soon
if (CHECK_FOR_DUE_SOON === "true") {
if (accountInfo.dueSoon > 0) {
notify = true
message += `You have ${accountInfo.dueSoon} books due soon. `
}
}
//check for ready for pickup
if (CHECK_FOR_READY_FOR_PICKUP === "true") {
if (accountInfo.readyForPickup > 0) {
notify = true
message += `You have ${accountInfo.readyForPickup} holds ready for pickup. `
}
}
//check for in transit
if (CHECK_FOR_IN_TRANSIT === "true") {
if (accountInfo.inTransit > 0) {
notify = true
message += `You have ${accountInfo.inTransit} holds in transit. `
}
}
if (notify) {
//go to zoho login page
page.goto("https://accounts.zoho.in/signin?servicename=ZohoHome&signupurl=https://www.zoho.com/signup.html")
await page.waitForNavigation({ waitUntil: 'networkidle0' });
//enter email
const zoho_email_input = '#login_id';
await page.waitForSelector(zoho_email_input);
await page.type(zoho_email_input, ZOHO_EMAIL);
// Submit the email
await page.keyboard.press('Enter');
await page.waitForNavigation({ waitUntil: 'networkidle0' });
//enter password
const zoho_password_input = '#password';
await page.waitForSelector(zoho_password_input);
await page.type(zoho_password_input, ZOHO_PASSWORD);
// Submit the password
await page.keyboard.press('Enter');
await page.waitForNavigation({ waitUntil: 'networkidle0' });
//compose message
page.goto("https://mail.zohocloud.ca/zm/#compose")
await page.waitForNavigation({ waitUntil: 'networkidle0' });
//get all input components
await page.$$("input[type='text']")
//add email recipient
await page.keyboard.type(RECIPIENT_EMAIL);
await page.keyboard.press('Tab');
await page.keyboard.press('Tab');
//add email subject
await page.keyboard.type("Toronto Library Notification");
await page.keyboard.press('Tab');
//add message
await page.keyboard.type(message);
// Press the 'Control + Shift' to send email
await page.keyboard.down('Control');
await page.keyboard.down('Enter');
await page.keyboard.up('Enter');
await page.keyboard.up('Control');
}
//close the browser
await browser.close()
async function waitForMultipleElements(page, className, count) {
const elements = await page.$$(className);
if (elements.length >= count) {
return elements;
} else {
await Promise.all([
page.waitForSelector(className, { state: 'attached', timeout: 0 }),
new Promise(resolve => setTimeout(resolve, 1000)) // Pause for 1000 milliseconds
]);
return waitForMultipleElements(page, className, count);
}
}