-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
314 lines (270 loc) · 12.7 KB
/
main.py
File metadata and controls
314 lines (270 loc) · 12.7 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
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
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import json
import os
import platform
import subprocess
import time
from datetime import datetime, timedelta
import re
import pygame
def load_user_info():
with open('info/info.json', 'r') as file:
return json.load(file)
def setup_driver():
chrome_options = Options()
# Add any necessary options here
# chrome_options.add_argument('--headless') # Uncomment to run in headless mode
driver = webdriver.Chrome(options=chrome_options)
return driver
def fill_login_form(driver, user_info):
wait = WebDriverWait(driver, 10)
# Fill in last name
last_name_input = wait.until(EC.presence_of_element_located((By.ID, "mat-input-0")))
last_name_input.clear()
last_name_input.send_keys(user_info["lastName"])
# Fill in driver's license
dl_input = wait.until(EC.presence_of_element_located((By.ID, "mat-input-1")))
dl_input.clear()
dl_input.send_keys(user_info["dl"])
# Fill in PIN
pin_input = wait.until(EC.presence_of_element_located((By.ID, "mat-input-2")))
pin_input.clear()
pin_input.send_keys(user_info["pin"])
# Check the checkbox by clicking the span
checkbox_span = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "mat-checkbox-inner-container")))
checkbox_span.click()
print("Form filled successfully")
def parse_date(date_str):
# Remove ordinal indicators (st, nd, rd, th)
date_str = re.sub(r'(\d+)(st|nd|rd|th)', r'\1', date_str)
# Add current year to the date string
current_year = datetime.now().year
date_str = f"{date_str}, {current_year}"
# Parse the date string (format: "August 26, 2024")
return datetime.strptime(date_str, "%B %d, %Y")
def is_date_within_range(date, days=10):
today = datetime.now()
future_date = today + timedelta(days=days)
return today <= date <= future_date
def handle_no_thanks(driver):
wait = WebDriverWait(driver, 1) # Shorter timeout since we expect it might not exist
try:
# Look for a button containing the text "No thanks"
no_thanks_button = wait.until(EC.presence_of_element_located((By.XPATH, "//button[contains(text(), 'No thanks')]")))
no_thanks_button.click()
print("Clicked 'No thanks' button")
return True
except:
print("No 'No thanks' button found")
return False
def play_alert():
try:
# Initialize pygame mixer
pygame.mixer.init()
# Load the sound file
sound = pygame.mixer.Sound('alert.wav')
# Set volume to maximum
sound.set_volume(1.0)
# Play the sound
sound.play()
# Wait for the sound to finish playing
pygame.time.wait(int(sound.get_length() * 1000))
print("Alert sound played")
except Exception as e:
print(f"Error playing alert sound: {e}")
finally:
# Clean up pygame
pygame.mixer.quit()
def check_for_reschedule(driver):
wait = WebDriverWait(driver, 2) # Shorter timeout since we expect it might not exist
try:
# Look for "Reschedule appointment" button
reschedule_button = wait.until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(), 'Reschedule appointment')]")
))
print("Found Reschedule appointment button")
handle_no_thanks(driver)
reschedule_button.click()
# Wait for 5 seconds
time.sleep(5)
# Look for "Yes" button
yes_button = wait.until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(), 'Yes')]")
))
print("Found Yes button")
handle_no_thanks(driver)
yes_button.click()
return True
except:
print("No rescheduling options found")
return False
def search_appointment(driver, user_info):
wait = WebDriverWait(driver, 10)
time.sleep(1)
res = check_for_reschedule(driver)
# Click the tab element
try:
tab_element = wait.until(EC.presence_of_element_located((By.ID, "mat-tab-label-1-1" if not res else "mat-tab-label-2-1")))
tab_element.click()
except:
handle_no_thanks(driver)
tab_element = wait.until(EC.presence_of_element_located((By.ID, "mat-tab-label-1-1" if not res else "mat-tab-label-2-1")))
tab_element.click()
time.sleep(5)
venues = user_info["venues"]
index = 0
while True:
# Wait for the location input to be present and type 'vancouver'\
try:
location_input = wait.until(EC.presence_of_element_located((By.ID, "mat-input-4")))
location_input.clear()
location_input.click()
except:
handle_no_thanks(driver)
location_input = wait.until(EC.presence_of_element_located((By.ID, "mat-input-4")))
location_input.clear()
location_input.click()
time.sleep(1)
# Wait for the autocomplete dropdown and get all children
try:
autocomplete = wait.until(EC.presence_of_element_located((By.ID, "mat-autocomplete-0")))
options = autocomplete.find_elements(By.CSS_SELECTOR, "mat-option")
except:
handle_no_thanks(driver)
autocomplete = wait.until(EC.presence_of_element_located((By.ID, "mat-autocomplete-0")))
options = autocomplete.find_elements(By.CSS_SELECTOR, "mat-option")
# Check each option's span text
for option in options:
try:
span = option.find_element(By.TAG_NAME, "span")
exam_place = span.text
if exam_place == venues[index]:
print(f"Venue found: {exam_place}")
option.click()
time.sleep(3)
# Handle "No thanks" button if it appears
handle_no_thanks(driver)
# look for a div with class name "appointment-listings"
try:
appointment_listings = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "appointment-listings")))
date_title = appointment_listings.find_element(By.CLASS_NAME, "date-title")
except:
handle_no_thanks(driver)
appointment_listings = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "appointment-listings")))
date_title = appointment_listings.find_element(By.CLASS_NAME, "date-title")
print(f"Date Title: {date_title.text}") # will be in format "Tuesday, August 26th, 2025"
date_str = date_title.text.split(",")[1].strip()
print(f"Parsing date string: {date_str}")
time.sleep(5)
# Parse the date and check if it's within the next 10 days
try:
appointment_date = parse_date(date_str)
if is_date_within_range(appointment_date, days=user_info["within_days"]):
print(f"Found available appointment within {user_info['within_days']} days: {appointment_date.strftime('%B %d, %Y')}")
# Play alert sound
# click on the date
handle_no_thanks(driver)
try:
date_element = wait.until(EC.presence_of_element_located((By.ID, "mat-button-toggle-1-button")))
date_element.click()
except:
handle_no_thanks(driver)
date_element = wait.until(EC.presence_of_element_located((By.ID, "mat-button-toggle-1-button")))
date_element.click()
time.sleep(3)
# Find and click "Review Appointment" button
handle_no_thanks(driver)
try:
review_button = wait.until(EC.presence_of_element_located(
(By.XPATH, "//button[.//span[contains(text(), 'Review Appointment')]]")
))
review_button.click()
except:
handle_no_thanks(driver)
review_button = wait.until(EC.presence_of_element_located(
(By.XPATH, "//button[.//span[contains(text(), 'Review Appointment')]]")
))
review_button.click()
print("Found Review Appointment button")
time.sleep(3)
handle_no_thanks(driver)
# Find and click the "Next" button
try:
next_button = wait.until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(), 'Next')]")
))
next_button.click()
except:
handle_no_thanks(driver)
next_button = wait.until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(), 'Next')]")
))
next_button.click()
print("Found Next button")
time.sleep(3)
handle_no_thanks(driver)
# Find and click the "Send" button
try:
send_button = wait.until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(), 'Send')]")
))
send_button.click()
except:
handle_no_thanks(driver)
send_button = wait.until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(), 'Send')]")
))
send_button.click()
time.sleep(3)
handle_no_thanks(driver)
# Check for rescheduling options
check_for_reschedule(driver)
play_alert()
play_alert()
play_alert()
time.sleep(100)
break
else:
print(f"Appointment date {appointment_date.strftime('%B %d, %Y')} is not within the next {user_info['within_days']} days")
except ValueError as e:
print(f"Error parsing date: {e}")
handle_no_thanks(driver)
break
except:
handle_no_thanks(driver)
continue
index += 1
if index >= len(venues):
index = 0
print("Location search initiated")
def main():
# Load user information
user_info = load_user_info()
print("User information loaded successfully")
# Setup the web driver
driver = setup_driver()
print("Web driver initialized successfully")
try:
# Navigate to ICBC booking page
driver.get("https://onlinebusiness.icbc.com/webdeas-ui/login;type=driver")
# Wait for the page to load
wait = WebDriverWait(driver, 10)
# Fill in the login form
fill_login_form(driver, user_info)
# Click the submit button
submit_button = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "button[type='submit']")))
submit_button.click()
# Search for appointment
search_appointment(driver, user_info)
# Keep the browser open for now
input("Press Enter to close the browser...")
finally:
driver.quit()
if __name__ == "__main__":
main()