-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtermin.py
338 lines (283 loc) · 15.7 KB
/
termin.py
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import requests
import logging
import bs4
import enum
from utils import is_date_within_n_days
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
class Location(enum.Enum):
Katschhof = "Bürgerservice Katschhof"
Bahnhofplatz = "Bürgerservice Bahnhofplatz"
def aachen_an(loc: Location, year: str, month: str):
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
url = ""
if loc == Location.Katschhof:
url = f"https://qtermin.de/api/timeslots?date={year}-{month}-01&serviceid=94948&rangesearch=1&caching=false&capacity=1&duration=10&cluster=false&slottype=0&fillcalendarstrategy=0&showavcap=false&appfuture=70&appdeadline=0&appdeadlinewm=0&oneoff=null&msdcm=0&calendarid=57095,57096,57097,74724,74725,133598"
elif loc == Location.Bahnhofplatz:
url = f"https://www.qtermin.de/api/timeslots?date={year}-{month}-01&serviceid=94948&rangesearch=1&caching=false&capacity=1&duration=10&cluster=false&slottype=0&fillcalendarstrategy=0&showavcap=false&appfuture=70&appdeadline=0&appdeadlinewm=0&oneoff=null&msdcm=0&calendarid=57003,57091,57092,57093,57094,71058,71059,71060,71061,71062,77257,77289,77291,77292,133608,133610,133607,133612,133614,133615,133616"
if not url:
logging.error(f'Invalid location: {loc.value}')
return False, f'Invalid location: {loc.value}'
headers = {"User-Agent": user_agent, "webid": 'bahnhofplatzkatschhof'}
res = requests.get(url, headers=headers).json()
message = ''
for t in res:
if t['start'][5:7] == month:
message += t['start'][:10]
message += '\n'
if message:
message = f'Available appointments at {loc.value} in {number_to_month(month)}:\n' + message[:-1]
logging.info(message)
return True, message
else:
message= f'No available appointment at {loc.value} in {number_to_month(month)}'
logging.info(message)
return False, message
def number_to_month(number):
month_dict = {
"01": "January",
"02": "February",
"03": "March",
"04": "April",
"05": "May",
"06": "June",
"07": "July",
"08": "August",
"09": "September",
"10": "October",
"11": "November",
"12": "December"
}
return month_dict.get(number, "Invalid Month")
def abholung_termin():
# The structure of the code is exactly the same as superc_termin, I'm just too lazy to refactor it.
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
headers = {"User-Agent": user_agent}
session = requests.Session()
session.headers.update(headers)
url_1 = 'https://termine.staedteregion-aachen.de/auslaenderamt/select2?md=1'
url_2 = ''
url_3 = 'https://termine.staedteregion-aachen.de/auslaenderamt/suggest'
res_1 = session.get(url_1)
# Get RWTH cnc id
soup = bs4.BeautifulSoup(res_1.content, 'html.parser')
header_element = soup.find('h3', string=lambda s: 'Abholung' in s if s else False)
if header_element:
next_sibling = header_element.find_next_sibling()
if next_sibling:
li_elements = next_sibling.find_all('li')
cnc_id = li_elements[0].get('id').split('-')[-1] if li_elements else None
url_2 = f'https://termine.staedteregion-aachen.de/auslaenderamt/location?mdt=89&select_cnc=1&cnc-{cnc_id}=1'
logging.info(f'{"Abholung cnc id: " + cnc_id}')
else:
logging.info("Element containing 'Abholung' not found.")
return False, "Element containing 'Abholung' not found."
res_2 = session.get(url_2)
soup = bs4.BeautifulSoup(res_2.content, 'html.parser')
loc = soup.find('input', {'name': 'loc'}).get('value')
logging.info(f'{"Abholung loc: " + loc}')
payload = {'loc':str(loc), 'gps_lat': '55.77858', 'gps_long': '65.07867', 'select_location': 'Ausländeramt Aachen - Aachen Arkaden, Trierer Straße 1, Aachen auswählen'}
res_3 = session.post(url_2, data=payload)
res_4 = session.get(url_3)
if "Kein freier Termin verfügbar" not in res_4.text:
# get exact termin date
soup = bs4.BeautifulSoup(res_4.text, 'html.parser')
div = soup.find("div", {"id": "sugg_accordion"})
summary_tag = soup.find('summary', id='suggest_details_summary')
if div:
logging.info(f'{"Appointment available now in Abholung Aufenthaltserlaubnis!"}')
h3 = div.find_all("h3")
res = 'New appointments are available now!\n'
flag = False
for h in h3:
if is_date_within_n_days(h.text, 50):
res += h.text + '\n'
flag = True
if not flag:
logging.info(f'{"There are appointments available, but they are not within 50 days from today."}')
return False, "There are appointments available, but they are not within 50 days from today."
return True, res[:-1]
elif summary_tag:
summary_text = summary_tag.get_text(strip=True)
if is_date_within_n_days(summary_text, 50):
logging.info(f'{"Appointment available now in Abholung Aufenthaltserlaubnis!"}')
logging.info(f'{summary_text}')
return True, 'New appointments are available now!\n' + summary_text
else:
logging.info(f'{"There are appointments available, but they are not within 50 days from today."}')
return False, "There are appointments available, but they are not within 50 days from today."
else:
logging.info(f'{"Cannot find sugg_accordion! Possible new appointments are available now in Abholung Aufenthaltserlaubnis!"}')
return False, "Cannot find sugg_accordion! Possible new appointments are available now!"
else:
logging.info(f'{"No appointment is available in Abholung Aufenthaltserlaubnis."}')
return False, "No appointment is available in Abholung Aufenthaltserlaubnis"
def superc_termin():
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
headers = {"User-Agent": user_agent}
session = requests.Session()
session.headers.update(headers)
url_1 = 'https://termine.staedteregion-aachen.de/auslaenderamt/select2?md=1'
url_2 = ''
url_3 = 'https://termine.staedteregion-aachen.de/auslaenderamt/suggest'
res_1 = session.get(url_1)
# Get RWTH cnc id
soup = bs4.BeautifulSoup(res_1.content, 'html.parser')
header_element = soup.find('h3', string=lambda s: 'Super C' in s if s else False)
if header_element:
next_sibling = header_element.find_next_sibling()
if next_sibling:
li_elements = next_sibling.find_all('li')
cnc_id = li_elements[0].get('id').split('-')[-1] if li_elements else None
url_2 = f'https://termine.staedteregion-aachen.de/auslaenderamt/location?mdt=89&select_cnc=1&cnc-{cnc_id}=1'
logging.info(f'{"Super C cnc id: " + cnc_id}')
else:
logging.info("Element containing 'Super C' not found.")
return False, "Element containing 'Super C' not found."
res_2 = session.get(url_2)
soup = bs4.BeautifulSoup(res_2.content, 'html.parser')
loc = soup.find('input', {'name': 'loc'}).get('value')
logging.info(f'{"Super C loc: " + loc}')
payload = {'loc':str(loc), 'gps_lat': '55.77858', 'gps_long': '65.07867', 'select_location': 'Ausländeramt Aachen - Außenstelle RWTH auswählen'}
res_3 = session.post(url_2, data=payload)
res_4 = session.get(url_3)
if "Kein freier Termin verfügbar" not in res_4.text:
# get exact termin date
soup = bs4.BeautifulSoup(res_4.text, 'html.parser')
div = soup.find("div", {"id": "sugg_accordion"})
summary_tag = soup.find('summary', id='suggest_details_summary')
if div:
logging.info(f'{"Appointment available now in SuperC!"}')
h3 = div.find_all("h3")
res = 'New appointments are available now!\n'
for h in h3:
res += h.text + '\n'
return True, res[:-1]
elif summary_tag:
summary_text = summary_tag.get_text(strip=True)
logging.info(f'{"Appointment available now in SuperC!"}')
logging.info(f'{summary_text}')
return True, 'New appointments are available now!\n' + summary_text
else:
logging.info(f'{"Cannot find sugg_accordion! Possible new appointments are available now in SuperC!"}')
return False, "Cannot find sugg_accordion! Possible new appointments are available now!"
else:
logging.info(f'{"No appointment is available in SuperC."}')
return False, "No appointment is available in SuperC"
def fh_termin():
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
headers = {"User-Agent": user_agent}
session = requests.Session()
session.headers.update(headers)
url_1 = 'https://termine.staedteregion-aachen.de/auslaenderamt/select2?md=1'
url_2 = ''
url_3 = 'https://termine.staedteregion-aachen.de/auslaenderamt/suggest'
res_1 = session.get(url_1)
# Get RWTH cnc id
soup = bs4.BeautifulSoup(res_1.content, 'html.parser')
header_element = soup.find('h3', string=lambda s: 'Fachhochschule Aachen' in s if s else False)
if header_element:
next_sibling = header_element.find_next_sibling()
if next_sibling:
li_elements = next_sibling.find_all('li')
cnc_id = li_elements[0].get('id').split('-')[-1] if li_elements else None
url_2 = f'https://termine.staedteregion-aachen.de/auslaenderamt/location?mdt=89&select_cnc=1&cnc-{cnc_id}=1'
logging.info(f'{"Fachhochschule Aachen cnc id: " + cnc_id}')
else:
logging.info("Element containing 'Fachhochschule Aachen' not found.")
return False, "Element containing 'Fachhochschule Aachen' not found."
res_2 = session.get(url_2)
soup = bs4.BeautifulSoup(res_2.content, 'html.parser')
loc = soup.find('input', {'name': 'loc'}).get('value')
logging.info(f'{"Fachhochschule Aachen loc: " + loc}')
payload = {'loc':str(loc), 'gps_lat': '55.77858', 'gps_long': '65.07867', 'select_location': 'Ausländeramt Aachen, 2. Etage auswählen'}
res_3 = session.post(url_2, data=payload)
res_4 = session.get(url_3)
if "Kein freier Termin verfügbar" not in res_4.text:
# get exact termin date
soup = bs4.BeautifulSoup(res_4.text, 'html.parser')
div = soup.find("div", {"id": "sugg_accordion"})
summary_tag = soup.find('summary', id='suggest_details_summary')
if div:
logging.info(f'{"Appointment available now in Fachhochschule Aachen!"}')
h3 = div.find_all("h3")
res = 'New appointments are available now!\n'
for h in h3:
res += h.text + '\n'
return True, res[:-1]
elif summary_tag:
summary_text = summary_tag.get_text(strip=True)
logging.info(f'{"Appointment available now in Fachhochschule Aachen!"}')
logging.info(f'{summary_text}')
return True, 'New appointments are available now!\n' + summary_text
else:
logging.info(f'{"Cannot find sugg_accordion! Possible new appointments are available now in Fachhochschule Aachen!"}')
return False, "Cannot find sugg_accordion! Possible new appointments are available now!"
else:
logging.info(f'{"No appointment is available in Fachhochschule Aachen."}')
return False, "No appointment is available in Fachhochschule Aachen"
# hbf_url = {
# 'Team 1': 'https://termine.staedteregion-aachen.de/auslaenderamt/location?mdt=89&select_cnc=1&cnc-293=1',
# 'Team 2': 'https://termine.staedteregion-aachen.de/auslaenderamt/location?mdt=89&select_cnc=1&cnc-296=1',
# 'Team 3': 'https://termine.staedteregion-aachen.de/auslaenderamt/location?mdt=89&select_cnc=1&cnc-297=1'
# }
def get_hbf_url(res_1, team_name):
soup = bs4.BeautifulSoup(res_1.content, 'html.parser')
header_element = soup.find('h3', string=lambda s: 'Aufenthalt' in s if s else False)
url_2 = ''
li_index = int(team_name.split(' ')[-1]) - 1
if header_element:
next_sibling = header_element.find_next_sibling()
if next_sibling:
li_elements = next_sibling.find_all('li')
cnc_id = li_elements[li_index].get('id').split('-')[-1] if li_elements else None
url_2 = f'https://termine.staedteregion-aachen.de/auslaenderamt/location?mdt=89&select_cnc=1&cnc-{cnc_id}=1'
logging.info(f"Aufenthalt {team_name} cnc id: {cnc_id}")
else:
logging.info("Element containing 'Aufenthalt' not found.")
return url_2
def aachen_hbf_termin(team_name):
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
headers = {"User-Agent": user_agent}
session = requests.Session()
session.headers.update(headers)
url_1 = 'https://termine.staedteregion-aachen.de/auslaenderamt/select2?md=1'
url_2 = ''
url_3 = 'https://termine.staedteregion-aachen.de/auslaenderamt/suggest'
res_1 = session.get(url_1)
url_2 = get_hbf_url(res_1, team_name)
res_2 = session.get(url_2)
soup = bs4.BeautifulSoup(res_2.content, 'html.parser')
loc = soup.find('input', {'name': 'loc'}).get('value')
logging.info(f"Aufenthalt {team_name} loc: {loc}")
payload = {'loc':str(loc), 'gps_lat': '55.77858', 'gps_long': '65.07867', 'select_location': 'Ausländeramt Aachen, 2. Etage auswählen'}
res_3 = session.post(url_2, data=payload)
res_4 = session.get(url_3)
if "Kein freier Termin verfügbar" not in res_4.text:
# get exact termin date
soup = bs4.BeautifulSoup(res_4.text, 'html.parser')
div = soup.find("div", {"id": "sugg_accordion"})
summary_tag = soup.find('summary', id='suggest_details_summary')
if div:
h3 = div.find_all("h3")
res = f'New appointments are available now at HBF {team_name}!\n'
for h in h3:
res += h.text + '\n'
logging.info(res[:-1])
return True, res[:-1]
elif summary_tag:
summary_text = summary_tag.get_text(strip=True)
logging.info(f'Appointment available now at HBF {team_name}!')
logging.info(f'{summary_text}')
return True, f'New appointments are available now at HBF {team_name}!\n' + summary_text
else:
logging.info(f'Cannot find sugg_accordion! Possible new appointments are available now at HBF {team_name}!')
return False, f"Cannot find sugg_accordion! Possible new appointments are available now at HBF {team_name}!"
else:
logging.info(f'No appointment is available at HBF {team_name}.')
return False, f'No appointment is available at HBF {team_name}.'
# superc_termin()
# aachen_hbf_termin('Team 1')
# aachen_hbf_termin('Team 2')
# aachen_hbf_termin('Team 3')
# for key, value in hbf_url.items():
# aachen_hbf_termin(key, value)