-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (101 loc) · 4.51 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
// Basic automation script to detect active vaccination schedules
require('dotenv').config()
const puppeteer = require('puppeteer');
const accountSid = process.env.TWILIO_ACCOUNT_SID || '';
const authToken = process.env.TWILIO_AUTH_TOKEN || '';
const client = require('twilio')(accountSid, authToken);
// Vaccines & vaccination center regions to monitor
const nameOfVaccine = "Pfizer";
const vaccinationCenterRegion = "Moka";
const raspberryPi = false;
const MonitorVaccines = (regionName, vaccineName, raspberryPi) => {
return new Promise(async (resolve, reject) => {
try {
let browser = await puppeteer.launch({
headless: false,
devtools: false,
executablePath: raspberryPi ? '/usr/bin/chromium-browser' : undefined,
args: ['--no-sandbox', '--incognito']
});
const page = await browser.newPage();
await page.goto("https://c-caremauritius.as.me/schedule.php", { waitUntil: 'networkidle2' });
let vaccines = [];
// Wait for vaccines to load
await page.waitForSelector('span.appointment-type-name');
// Get vaccines' data
let availableVaccines = await page.evaluate(() => {
let results = [];
let ids = [];
let items = document.querySelectorAll('span.appointment-type-name');
let vaccineIdNodes = document.querySelectorAll('label.babel-ignore');
vaccineIdNodes.forEach((htmlNode) => {
ids.push(htmlNode.getAttribute("for")?.split('-')[1]?.trim());
})
items.forEach((item, count) => {
let vaccineText = item?.innerText;
const vaccineName = vaccineText?.split("-")[1]?.trim();
const jabType = vaccineText?.split("-")[2]?.split('@')[0]?.trim();
const location = vaccineText?.split("@")[1]?.trim();
results.push({
id: ids[count],
appointment: item?.innerText,
vaccineName: vaccineName,
jabType: jabType,
location: location
});
});
return results;
});
vaccines = vaccines.concat(availableVaccines);
let filteredResults;
if (regionName && vaccineName) {
filteredResults = vaccines.filter(vaccine => vaccine?.location?.toLowerCase()?.includes(regionName?.toLowerCase()) && vaccine?.vaccineName?.toLowerCase()?.includes(vaccineName?.toLowerCase()));
console.log('filterered', filteredResults);
for (let i = 0; i < filteredResults.length; i++) {
let resultId = filteredResults[i].id;
await page.waitForSelector(`#appointmentType-${resultId}`);
await page.evaluate((resultId) => {
document.querySelector(`#appointmentType-${resultId}`).click();
}, resultId);
let message;
// Check availability for appointment scheduling
// If message is present, then no schedules are avaialble
try {
message = await page.waitForSelector('#no-times-available-message', { timeout: 3000 })
} catch (err) {
message = false;
}
if (message) {
console.log("NO VACCINES FOUND");
} else {
console.log("VACCINES AVAILABLE FOR SCHEDULING");
client.messages
.create({
body: `Vaccine ${filteredResults[i]?.vaccineName} is currently available for scheduling at ${filteredResults[i]?.location} as a ${filteredResults[i]?.jabType} jab`,
from: process.env.SENDER_NUMBER,
to: process.env.RECEPIENT_NUMBER
})
.then(message => console.log(message.sid));
}
await page.evaluate((resultId) => {
document.querySelector(`#appointmentType-${resultId}`).click();
}, resultId);
}
}
console.log("filteredResults", filteredResults);
browser.close();
// In case we don't care about regional vaccination centers
// let nonRegional = vaccines.filter(vaccine => !vaccine?.location?.toLowerCase()?.includes(regionName?.toLowerCase()));
// regional = vaccines.filter(vaccine => vaccine?.location?.toLowerCase()?.includes(regionName?.toLowerCase()));
// console.log(`Non-Regional Vaccines`, nonRegional)
// console.log(`Regional Vaccines Found (${regionName})`, regional)
return resolve(vaccines);
} catch (e) {
return reject(e);
}
})
}
// Monitor for active schedules every ${x} intervals
setInterval(() => {
MonitorVaccines(vaccinationCenterRegion, nameOfVaccine, raspberryPi).catch(console.error);
}, 300000)