This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·204 lines (170 loc) · 5.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
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
'use strict';
/*eslint-disable no-console*/
import path from 'path';
import co from 'co';
import jsdom from 'jsdom';
import moment from 'moment';
import debug from 'debug';
import { getMonthsStartDatesUnix, addQueryParam } from './lib';
import notifier from 'node-notifier';
const urlsDebug = debug('urls');
// entry point
const TERMIN_BOOKING_URL = 'https://service.berlin.de/dienstleistung/120686/';
/**
* Termin = {
* url: string,
* text: string
* };
*
* AvailableTermin = {
* // start of month date - moment.js object
* momentDate: object,
*
* // month url
* url: string,
* termins: Termin[]
* };
*
* // termin extracted from timetable
* TimeTableTermin = {
* time: string,
* title: string,
* address: string,
* url: string
* };
*/
function errorHandler(err) {
console.error(`[ERROR] ${err}`);
}
/**
* Extract some data from the url
* @param {string} url Url to extract data from
* @param {function} extractor Extractor function - (document) => data
* @return {Promise} [description]
*/
function query(url, extractor) {
return new Promise((resolve, reject) => {
jsdom.env({
url,
done: (err, window) => {
if (err) {
reject(err);
return;
}
resolve(extractor(window.document));
}
});
}).catch(errorHandler);
}
function log(message) {
const time = moment().format('DD.MM.YYYY HH:mm');
console.log(`[${time}] ${message}`);
}
// extract url from booking button
export function parseTerminUrl(terminBookingUrl) {
return co(function *() {
return yield query(terminBookingUrl, terminUrlExtractor);
}).catch(errorHandler);
}
/**
* Get available termins
* @param {string} url Termin Calendar Page url
* @return {Promise<AvailableTermin[]>} Promise that will be resolved with termin
* calendar page url that is available for booking
*/
function parseAvailableTermins(terminCalendarUrl) {
// months to check
const unixDates = getMonthsStartDatesUnix(new Date(), 60);
const requests = unixDates.map(unixDate => ({
momentDate: moment.unix(unixDate),
url: addQueryParam(terminCalendarUrl, { Datum: unixDate })
}));
return co(function *() {
const responses = yield Promise.all(
requests.map(request => parseAvailableTerminsForMonth(request.url))
);
// merge termins back
return responses.map((termins, index) => {
return Object.assign({}, requests[index], { termins });
});
}).catch(errorHandler);
}
// extract available termins for the month
// [{ label: string, url: string }] available labels and url to follow
export function parseAvailableTerminsForMonth(url) {
return co(function *() {
return yield query(url, availableCalendarTerminsExtractor);
}).catch(errorHandler);
}
// Extractors
function availableCalendarTerminsExtractor(document) {
const TERMIN_LINKS_SELECTOR = '.calendar-month-table:nth-child(1) td a';
const links = document.querySelectorAll(TERMIN_LINKS_SELECTOR);
return Array.from(links).map(link => {
return { url: link.href, text: link.textContent.trim() };
});
}
function terminUrlExtractor(document) {
const TERMIN_BUTTON_SELECTOR = 'a.btn[href^="https://service.berlin.de/terminvereinbarung/termin/tag.php"]';
const link = document.querySelector(TERMIN_BUTTON_SELECTOR);
return link && link.href;
}
export function getTimeTable(url) {
return co(function *() {
return yield query(url, timetableExtractor);
}).catch(errorHandler);
}
function timetableExtractor(document) {
const TIMETABLE_ROW_SELECTOR = '.timetable table tr';
const TIME_SELECTOR = 'th.buchbar';
const PLACE_SELECTOR = 'td.frei a';
return Array.from(document.querySelectorAll(TIMETABLE_ROW_SELECTOR))
.map(row => {
const time = row.querySelector(TIME_SELECTOR).textContent.trim();
const placeLink = row.querySelector(PLACE_SELECTOR);
const title = placeLink.textContent.trim();
const linkTitle = placeLink.getAttribute('title').trim();
const url = placeLink.href;
// get everything after "Adresse: "
const address = linkTitle.replace(/.*Adresse\:\s*/, '');
return { time, title, address, url };
});
}
function notify(count) {
notifier.notify({
message: `Appointments available: ${count}`,
icon: path.resolve('./buro.png'),
contentImage: false,
sound: 'Ping',
wait: true
});
}
export function availableTerminsCount(availableTermins) {
return availableTermins.reduce((acc, availableTermin) => {
return acc + availableTermin.termins.length;
}, 0);
}
function printAvailableTerminsInfo(availableTermins) {
availableTermins.forEach(({ momentDate, url, termins }) => {
log(`${momentDate.format('MMMM')} / ${termins.length} termins available.`);
urlsDebug(`Termin check url: ${url}`);
if (termins.length === 0) {
return;
}
log(`${'-'.repeat(10)} Available termins info: ${'-'.repeat(10)}`);
log(`Calendar page: ${url}`);
// print termins dates with links
termins.forEach(termin => log(`${termin.text}th:\n${termin.url}\n\n`));
log(`${'-'.repeat(30)}`);
});
}
export function check(terminBookingUrl = TERMIN_BOOKING_URL) {
return co(function *() {
const terminCalendarUrl = yield parseTerminUrl(terminBookingUrl);
const availableTermins = yield parseAvailableTermins(terminCalendarUrl);
printAvailableTerminsInfo(availableTermins);
if (availableTerminsCount(availableTermins) !== 0) {
notify(availableTerminsCount(availableTermins));
}
}).catch(errorHandler);
}