This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
163 lines (146 loc) · 5.45 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
require('dotenv').config();
process.env.NTBA_FIX_319 = 1; // disable telegram deprecated warnings on start up
const { chromium } = require('playwright'),
notifier = require('node-notifier'),
TelegramBot = require('node-telegram-bot-api'),
MatrixBot = require('matrix-bot-sdk');
const MAX_32_BIT_SIGNED_INTEGER = Math.pow(2, 31) - 1;
const username = process.env.USER_NAME;
const password = process.env.PASSWORD;
const centresString = process.env.CENTRES;
if (!username) throw new Exception('USER_NAME variable missing');
if (!password) throw new Exception('PASSWORD variable missing');
if (!centresString) throw new Exception('CENTRES variable missing');
const partnerUsername = process.env.PARTNER_USER_NAME;
const partnerPassword = process.env.PARTNER_PASSWORD;
const centres = centresString.split(',').map((x) => x.trim());
const delay = parseInt(process.env.DELAY || 300000);
const browserOptions = {
headless: false,
...(process.env.BROWSER_OPTIONS
? JSON.parse(process.env.BROWSER_OPTIONS)
: {}),
};
let notifyTelegramBot = null;
if (process.env.TELEGRAM_TOKEN && process.env.TELEGRAM_CHAT_ID) {
notifyTelegramBot = (text) => {
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN);
return bot.sendMessage(process.env.TELEGRAM_CHAT_ID, text);
};
console.log('Telegram connection enabled');
}
let notifyMatrixBot = null;
if (process.env.MATRIX_HOMESERVER && process.env.MATRIX_TOKEN && process.env.MATRIX_ROOM_ID) {
const storage = new MatrixBot.SimpleFsStorageProvider(".matrix.json");
let matrixClient = new MatrixBot.MatrixClient(process.env.MATRIX_HOMESERVER, process.env.MATRIX_TOKEN, storage);
MatrixBot.AutojoinRoomsMixin.setupOnClient(matrixClient);
matrixClient.start().then(() => console.log('Matrix connection enabled'));
notifyMatrixBot = (text) => {
return matrixClient.sendMessage(process.env.MATRIX_ROOM_ID, {
"msgtype": "m.text",
"body": text,
});
}
}
(async () => {
const browser = await chromium.launch(browserOptions);
let page;
while (true) {
if (browser.isConnected() === false) {
console.error('Browser not connected, cannot continue :(');
return;
}
try {
page = await browser.newPage();
page.setDefaultTimeout(90000);
return await findAppointment(page);
} catch (err) {
console.log(`There was an error: ${err}.`);
console.log(`Restarting from scratch...`);
if (page && page.isClosed() === false) {
page.close();
}
}
}
})();
async function findAppointment(page) {
console.log(`Starting with ${username}`);
await page.goto(
'https://sachsen.impfterminvergabe.de/civ.public/start.html?oe=00.00.IM&mode=cc&cc_key=IOAktion',
);
await page.click('button:has(span:is(:text("Weiter")))', {
timeout: MAX_32_BIT_SIGNED_INTEGER, // Wait in queue
});
await page.waitForSelector('h3:is(:text("Zugangsdaten"))');
await page.fill('text=Vorgangskennung', username);
await page.fill('text=Passwort', password);
await page.click('button:has(span:is(:text("Weiter")))');
await page.click(
'text=Termin zur Coronaschutzimpfung vereinbaren oder ändern',
);
await page.click('button:has(span:is(:text("Weiter")))');
let partnerEntered = false;
while (true) {
for (let currentCentre of centres) {
await page.waitForSelector('h3:is(:text("Vorgaben für Terminsuche"))');
const centreSelectCombobox = await page.$(
'[role=combobox]:near(label:is(:text("Ihr gewünschtes Impfcenter*")))',
);
await centreSelectCombobox.click();
await page.click(`[role=listbox] li:is(:text("${currentCentre}"))`);
if (!partnerEntered && partnerUsername && partnerPassword) {
await page.click('label >> text=/Partnertermin/i');
await page.fill(
'text="Vorgangskennung Ihres Partners*"',
partnerUsername,
);
await page.fill('text="Passwort Ihres Partners"', partnerPassword);
partnerEntered = true;
}
await page.click('button:has(span:is(:text("Weiter")))');
await Promise.race([
page.waitForSelector('h2:is(:text("Terminauswahl"))'),
page.waitForSelector('h2:is(:text("Terminvergabe"))'),
page.waitForSelector('h2:is(:text("Fehlermeldung"))'),
]);
const isErrorPage = await page.$('h2:is(:text("Fehlermeldung"))');
if (isErrorPage) {
// we may get here if partner login is wrong
console.error(
'An error occured, please see the browser and check you config.',
);
return;
}
const noSuccessElement = await page.$('text=/keinen\\s*Termin/i');
if (noSuccessElement === null) {
console.log(`SUCCESS FOR ${currentCentre}`);
await notifySuccess(currentCentre);
return;
}
console.log(
`No success for ${currentCentre} (${new Date().toLocaleTimeString()})`,
);
await page.click('button:has(span:is(:text("Zurück")))');
}
console.log(`Waiting for ${delay}ms`);
await waitFor(delay);
}
}
async function waitFor(duration) {
return new Promise((resolve) => {
setTimeout(resolve, duration);
});
}
async function notifySuccess(successCentre) {
const message = `Ein Termin für ${successCentre} wurde gefunden!`;
notifier.notify({
title: 'Impftermin gefunden!',
message,
});
if (notifyTelegramBot) {
await notifyTelegramBot(`${process.env.USER_NAME}: ${message}`);
}
if (notifyMatrixBot) {
await notifyMatrixBot(`${process.env.USER_NAME}: ${message}`);
}
}