This repository has been archived by the owner on Nov 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathappointments.ts
219 lines (193 loc) · 6.66 KB
/
appointments.ts
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { Page } from "puppeteer-core";
import Debug from "debug";
const debug = Debug("impftermin:appointments");
/**
* Sit in the waiting room.
*
* @param page Page to use.
* @param maxWaitingTime
* @returns True, if the waiting was successful. False if we waited longer than the maximum wait time.
*/
async function sitInWaitingRoom(page: Page, maxWaitingTime: number) {
const isInWaitingRoom = async () => {
const title$ = await page.$("h1");
if (title$) {
const titleText = await (
await title$.getProperty("innerText")
)?.jsonValue<string>();
if (titleText && titleText.includes("Warteraum")) {
return true;
}
}
return false;
};
const inWaitingRoomTimestamp = Date.now();
while (true) {
// the page will refresh automatically when we are in the waiting room
// we throw an error if the waiting room does not refresh after 10 minutes
await page.waitForNavigation({
waitUntil: "networkidle0",
timeout: 1000 * 60 * 10, // 10 minutes
});
if (!(await isInWaitingRoom())) {
debug("We are no longer in the waiting room");
break;
}
const waitingTime = Date.now() - inWaitingRoomTimestamp;
debug(
`We have been waiting for ${Math.round(waitingTime / 1000)} seconds.`
);
if (waitingTime > maxWaitingTime) {
return false;
}
}
}
async function acceptNecessaryCookies(page: Page) {
try {
await page.click(".cookies-info-close");
debug("Accepted necessary cookies");
} catch {}
await page.waitForTimeout(1000);
}
async function proceedWithACode(page: Page, impfCode: string) {
debug("Checking with existing code " + impfCode);
// yes, we have a code
for (const listElement of await page.$$("span")) {
const idName = await (
await listElement.getProperty("innerText")
)?.jsonValue<string>();
if (idName && idName.includes("Ja")) {
await listElement.click({ delay: 200 });
break;
}
}
await page.waitForTimeout(1000);
// write code
await page.type("input[name='ets-input-code-0']", impfCode);
await page.waitForTimeout(3000);
// submit
const submitButton = await page.$("button[type='submit']");
await submitButton?.click({ delay: 200 });
await page.waitForNavigation({
waitUntil: "networkidle0",
});
await page.waitForTimeout(3000);
// inserted a code that has already a booked appointment
const codeAlreadyInUseHeader = await page.$("h2.ets-booking-headline");
const codeAlreadyInUseText = await (
await codeAlreadyInUseHeader?.getProperty("innerText")
)?.jsonValue<string>();
if (
codeAlreadyInUseText &&
codeAlreadyInUseText.toLowerCase().includes("ihr termin")
) {
debug("There is already a booked appointment with code %s", impfCode);
return false;
}
// search appointment
const searchButton = await page.$("button.search-filter-button");
await searchButton?.click({ delay: 500 });
await page.waitForTimeout(2000);
// info page
const appointmentWarning = await page.$(
"span.its-slot-pair-search-no-results"
);
const appointmentText = await (
await appointmentWarning?.getProperty("innerText")
)?.jsonValue<string>();
if (await areWeOffline(page)) {
debug("We are offline or on some different page");
return false;
}
// it is better to determine the appointment availability by actually checking that there is an appointment shown, since from time to time there
// is no window (with or without available dates) shown at all after hitting the search appointment button. In this case, the previous "if"
// gives a false positive. When there is a real appointment shown, it always gives dates with a time. Therefore checking for presence of string
// "Uhr" seems failsafe, since it does not appear on the 'stuck' search for appointments page
for (const listElementRealOffer of await page.$$("span")) {
const elementText = await (
await listElementRealOffer.getProperty("innerText")
)?.jsonValue<string>();
if (elementText && elementText.includes("Uhr")) {
// appointments available!!!
debug("Appointments available!!");
debug("DEBUG: appointmentText=%s", appointmentText);
return true;
}
}
debug("No appointments");
debug("DEBUG: appointmentText=%s", appointmentText);
return false;
}
export async function proceedWithoutACode(page: Page) {
debug("Checking without a code");
// no, we have no code
for (const listElement of await page.$$("span")) {
const idName = await (
await listElement.getProperty("innerText")
)?.jsonValue<string>();
if (idName && idName.includes("Nein")) {
await listElement.click({ delay: 200 });
break;
}
}
debug("Checking for loading message");
while (await isLoadingAvailableCodes(page)) {
debug("Waiting 1s for loading message to be gone");
await page.waitForTimeout(1000);
}
debug("Loading message is gone");
const appointmentWarning = await page.$("div.alert.alert-danger");
if (await areWeOffline(page)) {
debug("We are offline or on some different page");
return false;
}
if (!appointmentWarning) {
// code available
debug("Appointments available!!");
return true;
}
debug("No appointments");
return false;
}
async function areWeOffline(page: Page) {
return !(await page.$("div.footer-copyright"));
}
export async function checkForAppointments(
page: Page,
impfLocationUrl: string,
maxWaitingRoomTimeBeforeRefresh: number,
impfCode: string | undefined
) {
do {
await page.goto(impfLocationUrl);
debug("Checking impf location " + impfLocationUrl);
} while (await sitInWaitingRoom(page, maxWaitingRoomTimeBeforeRefresh));
// cookies - only accept necessary
await acceptNecessaryCookies(page);
if (impfCode) {
return await proceedWithACode(page, impfCode);
} else {
return await proceedWithoutACode(page);
}
}
async function isLoadingAvailableCodes(page: Page): Promise<boolean> {
// Check for search icon on page
const searchIcon = await page.$(".icon.icon-search");
if (searchIcon !== null) {
return true;
}
// Check for search text on page
const appointmentLoadingForm = await page.$("div.ets-login-form-section.in");
if (appointmentLoadingForm == null) {
// Something went wrong if we can't find the form. Let's trust the search icon check from above
debug(
"The page element 'div.ets-login-form-section.in' was not found. If there is a notification below, it might be a false positive."
);
return false;
}
const text = await appointmentLoadingForm.evaluate((el) => el.textContent);
const textMatches = text.match(
/Bitte warten, wir suchen verfügbare Termine in Ihrer Region/
);
return textMatches != null;
}