forked from joshuajung/116117bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
229 lines (216 loc) · 7.3 KB
/
index.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
220
221
222
223
224
225
226
227
228
229
import { default as axios, default as Axios } from "axios";
import express from "express";
import objectHash from "object-hash";
import puppeteer from "puppeteer-extra";
import StealthPlugin from "puppeteer-extra-plugin-stealth";
import { Browser } from "puppeteer-extra/dist/puppeteer";
import getConfig, { getUrls, getZipFromUrl } from "./config";
class Impfbot {
private config = getConfig();
private browser?: Browser;
private errorCount: number = 0;
private lastHash: Record<string, string> = {};
private lastAppointmentsAvailable: Record<string, number> = {};
private queue: string[] = getUrls();
public boot = async () => {
console.log("Booting 116117bot");
const app = express();
app.get("/", (req, res) => res.send());
app.listen(this.config.port);
await this.startBrowser();
this.alertPushover(`Now monitoring ${this.queue.length} URL(s).`, -2);
this.runLoopStep();
};
private startBrowser = async () => {
this.browser?.close();
puppeteer.use(StealthPlugin());
const defaultArgs = ["--window-size=1600,1200"];
this.browser = await puppeteer.launch({
executablePath: this.config.chromiumExecutablePath,
headless: this.config.headless,
defaultViewport: null,
args: [
...defaultArgs,
...(this.config.noPuppeteerSandbox
? ["--no-sandbox", "--disable-setuid-sandbox"]
: []),
],
});
};
private limboLoop = () => {
console.error("Caught in limbo loop, as fatal error was discovered.");
setTimeout(this.limboLoop, 1000 * 60 * 60);
};
private runLoopStep = async () => {
const url = this.queue.shift();
try {
if (!url) throw "No URL in queue.";
const [
appointmentsAvailable,
htmlHash,
html,
] = await this.findAppointmentsInUrl(url);
this.announceNewResults(htmlHash, url, appointmentsAvailable, html);
this.lastHash[url] = htmlHash;
this.lastAppointmentsAvailable[url] = appointmentsAvailable;
setTimeout(this.runLoopStep, this.nextTimeout * 1000);
this.errorCount = 0;
} catch (e) {
this.errorCount++;
console.error(e);
this.startBrowser();
if (this.errorCount < 10) {
console.error(`Error ${this.errorCount}, delaying next request.`);
setTimeout(this.runLoopStep, this.config.timeout.error * 1000);
} else {
console.error("Too many errors, bailing out.");
this.alertPushover("Too many errors, bailing out.", 0);
this.limboLoop();
}
} finally {
if (url) this.queue.push(url);
}
};
private findAppointmentsInUrl = async (
url: string
): Promise<[number, string, string]> => {
if (url.indexOf("impftermine/service") !== -1) {
// the entered URL is a pre-code URL (finding appointments before having a booking code)
const zip = getZipFromUrl(url);
const vaccinations = (
await axios.get(
"https://001-iz.impfterminservice.de/assets/static/its/vaccination-list.json"
)
).data
.map((v: any) => v.qualification)
.join(",");
const checkUrl =
url.substr(0, 36) +
"rest/suche/termincheck?plz=" +
zip +
"&leistungsmerkmale=" +
"L922" +
"&cachebuster=" +
Date.now();
const availableResponse = (await axios.get(checkUrl)).data;
return [
availableResponse["termineVorhanden"] ? 1 : 0,
objectHash(availableResponse),
JSON.stringify(availableResponse),
];
} else if (url.indexOf("impftermine/suche") !== -1) {
// the entered URL is an post-code URL (finding appointment after having a booking code)
console.warn(
"It is not recommended to monitor this kind of URL, as it immediately results in appointment reservations, reducing the number of appointments available to the public. Please see Readme."
);
const page = await this.browser!.defaultBrowserContext().newPage();
// We intercept requests to a specific URL that would sometimes result in a 429
await page.setRequestInterception(true);
page.on("request", (request) => {
if (request.url().indexOf("terminpaare") !== -1) {
}
if (request.url().indexOf("buchung") !== -1) {
request.respond({ status: 404, body: "{}" });
} else request.continue();
});
//
await page.goto(url);
const terminSuchenButtonSelector =
".its-search-step-content .btn-magenta";
try {
await page.waitForSelector(terminSuchenButtonSelector, {
timeout: 2 * 60 * 1000,
});
} catch (e) {
throw "Termin suchen button not found, likely error 429.";
}
const terminSuchenButton = await page.$(terminSuchenButtonSelector);
await terminSuchenButton?.click();
await page.waitForResponse(
(res) => res.url().indexOf("terminpaare") !== -1,
{ timeout: 10 * 1000 }
);
const appointmentsAvailable =
(await page.$$(".its-slot-pair-search-radio-wrapper")).length - 1;
const html = await page.content();
await page.close();
return [
appointmentsAvailable,
objectHash(html.split("<body").slice(-1)),
html,
];
} else {
throw "The URL type could not be identified.";
}
};
private alertPushover = async (message: string, priority: number) => {
if (this.config.pushover.token && this.config.pushover.user) {
try {
Axios.post("https://api.pushover.net/1/messages.json", {
token: this.config.pushover.token,
user: this.config.pushover.user,
message,
priority,
});
} catch (error) {
console.error("Error when pushing to pushover, will retry.");
console.error(error);
setTimeout(() => this.alertPushover(message, priority), 10000);
}
}
};
private announceNewResults = (
hash: string,
url: string,
appointmentsAvailable: number,
html: string
) => {
const hashChanged = this.lastHash[url] !== hash;
console.info(
"📅",
new Date(),
" 🌍",
getZipFromUrl(url),
hashChanged ? " 🔵" : " ⚪️",
hash.substr(0, 8),
appointmentsAvailable > 0 ? " ✳️" : " ⛔️",
`${appointmentsAvailable} appointment(s) available`
);
if (hashChanged) {
if (this.config.logHtml) {
console.log("---------------------------");
console.log(html.replace(/(\r\n|\n|\r)/gm, ""));
console.log("---------------------------");
}
if (
(this.lastAppointmentsAvailable[url] ?? 0) === 0 &&
appointmentsAvailable > 0
) {
this.alertPushover(
`There are ${appointmentsAvailable} available appointment(s) for ${getZipFromUrl(
url
)}!`,
1
);
}
if (
(this.lastAppointmentsAvailable[url] ?? 0) > 0 &&
appointmentsAvailable === 0
) {
this.alertPushover(
`There are no more available appointments for ${getZipFromUrl(url)}.`,
-1
);
}
}
};
private get nextTimeout(): number {
let timeout: number = this.config.timeout.regular;
return timeout;
}
}
function awaitTimeout(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const impfbot = new Impfbot();
impfbot.boot();