This repository was archived by the owner on Dec 9, 2025. It is now read-only.
forked from capital-G/berlin-auslanderbehorde-termin-bot
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathberlin_bot.py
More file actions
188 lines (149 loc) · 6.12 KB
/
berlin_bot.py
File metadata and controls
188 lines (149 loc) · 6.12 KB
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
import time
import os
import logging
from platform import system
import random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
system = system()
default_time_sleep = 2
max_time_resubmit = 60 * 15
sound_file = "./alarm.wav"
logging.basicConfig(
format='%(asctime)s\t%(levelname)s\t%(message)s',
level=logging.INFO,
)
class WebDriver:
def __init__(self):
self._driver: webdriver.Chrome
def __enter__(self) -> webdriver.Chrome:
logging.info("Open browser")
# some stuff that prevents us from being locked out
options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')
self._driver = webdriver.Chrome(os.path.join(os.getcwd(), "chromedriver"), options=options)
self._driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
self._driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})
return self._driver
def __exit__(self, exc_type, exc_value, exc_tb):
logging.info("Close browser")
self._driver.quit()
class BerlinBot:
def __init__(self, driver: webdriver.Chrome):
self.driver = driver
self._sound_file = os.path.join(os.getcwd(), "alarm.wav")
self._error_message = """Für die gewählte Dienstleistung sind aktuell keine Termine frei! Bitte"""
self.start_time = time.time()
def get_wait_time(self, init:list = [10, 20]):
options = range(init[0], init[1], 1)
chose_time = random.choice(options)
return chose_time
def clickPATH(self, path: str):
time.sleep(default_time_sleep)
try:
WebDriverWait(self.driver, self.get_wait_time()).until(EC.element_to_be_clickable((By.XPATH, path)))
self.driver.find_element(By.XPATH, path).click()
except:
self.clickPATH(path)
def clickID(self, id: str):
time.sleep(default_time_sleep)
try:
WebDriverWait(self.driver, self.get_wait_time()).until(EC.element_to_be_clickable((By.ID, id)))
self.driver.find_element(By.ID, id).click()
except:
time.sleep(3)
self.clickID(id)
def select(self, id: str, text: str):
time.sleep(default_time_sleep)
try:
element = self.driver.find_element(By.ID, id)
s = Select(element)
s.select_by_visible_text(text)
except:
time.sleep(3)
self.select(id, text)
def enter_start_page(self):
logging.info("Visit start page")
self.driver.get("https://otv.verwalt-berlin.de/ams/TerminBuchen")
self.clickPATH('//*[@id="mainForm"]/div/div/div/div/div/div/div/div/div/div[1]/div[1]/div[2]/a')
def tick_off_some_bullshit(self):
logging.info("Ticking off agreement")
self.clickPATH('//*[@id="xi-div-1"]/div[4]/label[2]/p')
self.clickID('applicationForm:managedForm:proceed')
time.sleep(5)
def enter_form(self):
# select china
self.wait_for_text("Staatsangehörigkeit")
self.select('xi-sel-400', 'China')
# eine person
self.wait_for_text("Anzahl der Personen")
self.select('xi-sel-422', 'eine Person')
# no family
self.wait_for_text("Leben Sie in Berlin zusammen mit einem Familienangehörigen (z.B. Ehepartner, Kind)")
self.select('xi-sel-427', 'nein')
# extend stay
self.clickPATH('//*[@id="xi-div-30"]/div[2]/label/p')
# click employment
self.clickPATH('//*[@id="inner-479-0-2"]/div/div[3]/label/p')
# on blue card
self.clickPATH('//*[@id="SERVICEWAHL_DE479-0-2-1-324659"]')
logging.info("Fill out form")
def submit(self):
wait_time = self.get_wait_time([default_time_sleep, default_time_sleep*2])
time.sleep(wait_time)
try:
self.clickPATH('//*[@id="applicationForm:managedForm:proceed"]')
except:
logging.info("Retry fill out form")
self.enter_form()
def _success(self):
logging.info("!!!SUCCESS - do not close the window!!!!")
while True:
self._play_sound(self._sound_file, 10)
time.sleep(5)
def wait_for_text(self, text: str, timeout: int = 30):
while text not in self.driver.page_source:
time.sleep(default_time_sleep)
timeout -= 1
if timeout == 0:
raise TimeoutException("Timeout while waiting for text")
time.sleep(default_time_sleep)
@staticmethod
def run_once():
with WebDriver() as driver:
bot = BerlinBot(driver)
bot.enter_start_page()
bot.tick_off_some_bullshit()
bot.enter_form()
time.sleep(default_time_sleep)
# retry submit
while time.time() - bot.start_time < max_time_resubmit:
bot.submit()
if "Auswahl Uhrzeit" in bot.driver.page_source:
bot._success()
elif bot._error_message in bot.driver.page_source:
logging.info("Retry submission")
else:
pass
@staticmethod
def run_loop():
# play sound to check if it works
BerlinBot._play_sound(sound_file)
while True:
logging.info("One more round")
BerlinBot.run_once()
time.sleep(5)
# stolen from https://github.com/JaDogg/pydoro/blob/develop/pydoro/pydoro_core/sound.py
@staticmethod
def _play_sound(sound, t = 0):
logging.info("Play sound")
from playsound import playsound
playsound(sound_file)
time.sleep(t)
if __name__ == "__main__":
BerlinBot.run_loop()