forked from Daky60/provbokning_trafikverket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
147 lines (133 loc) · 5.01 KB
/
bot.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
import config
import random
import time
from datetime import datetime, timedelta
from playsound import playsound
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
chrome_options = Options()
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])
class SeleniumDriver:
def __init__(self):
self.driver = webdriver.Chrome("chromedriver", options=chrome_options)
self.driver.get("https://fp.trafikverket.se/boka/#/licence")
self.driver.implicitly_wait(0.1)
self.continue_running = True
def select_exam(self):
try:
while self.driver.title == "Förarprov - Bokning":
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located(
(By.ID, "social-security-number-input")
)
).send_keys(config.social_security)
self.driver.find_element(
By.XPATH, f"//*[@title='{config.license_type}']"
).click()
except:
pass
def select_exam_type(self):
try:
exam_element = (
WebDriverWait(self.driver, 10)
.until(
EC.presence_of_element_located(
(
By.XPATH,
f"//select[@id='examination-type-select']/option[text()='{config.exam}']",
)
)
)
.click()
)
except:
pass
def select_rent_or_language(self):
try:
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located(
(
By.XPATH,
f"//select[@id='vehicle-select']/option[text()='{config.rent_or_language}']",
)
)
).click()
except:
pass
def select_location(self, location):
self.location = location
try:
location_element = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "id-control-searchText-1-1"))
)
location_element.clear()
location_element.send_keys(self.location, Keys.ENTER)
except:
pass
def select_time(self, first_date, last_date):
first_date = datetime.strptime(first_date, "%Y-%m-%d").date()
last_date = datetime.strptime(last_date, "%Y-%m-%d").date()
try:
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located(
(By.XPATH, f"//*[text()='Lediga provtider']")
)
)
while first_date < last_date:
try:
find_date = self.driver.find_element(
By.XPATH, f"//*[contains(text(), '{str(first_date)}')]"
)
if find_date:
find_date.find_element(By.XPATH, f"//*[text()='Välj']").click()
return False
except:
pass
first_date = first_date + timedelta(days=1)
except:
pass
return True
def refresh_page(self):
return self.driver.refresh()
def find_exam(driver, period=[30, 5 * 60, 1], speed=[5, 50, 0.1]):
periods = list(map(lambda value: period[-1] * value, range(*period[:2])))
speeds = list(map(lambda value: speed[-1] * value, range(*speed[:2])))
playsound("sounds/horn.wav")
driver.select_exam()
while driver.continue_running:
for i in config.locations:
try:
driver.select_exam_type()
time.sleep(random.choice(speeds))
driver.select_location(i)
time.sleep(random.choice(speeds))
driver.select_rent_or_language()
time.sleep(random.choice(speeds))
for j in range(0, len(config.dates), 2):
driver.continue_running = driver.select_time(
config.dates[j],
config.dates[j + 1],
)
if not driver.continue_running:
break
time.sleep(random.choice(speeds))
except KeyboardInterrupt:
return
except:
pass
if not driver.continue_running:
break
if not driver.continue_running:
break
timeout = random.choice(periods)
print(f"Sleeping for {timeout} seconds...")
time.sleep(timeout)
driver.refresh_page()
while True:
playsound("sounds/alert.mp3")
if __name__ == "__main__":
find_exam(SeleniumDriver())