This repository was archived by the owner on Jan 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (118 loc) · 5.21 KB
/
main.py
File metadata and controls
151 lines (118 loc) · 5.21 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
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
from win10toast import ToastNotifier
from twilio.rest import Client
from datetime import datetime
import os
import time
import json
import configparser
if os.name == "nt":
from win10toast import ToastNotifier
config = configparser.ConfigParser()
config.read('./config.ini')
browser_options = Options()
browser_options.binary_location = "./Chromium/Application/chrome.exe"
def sleep_counter(total_time):
for remaining_time in range(total_time, 0, -1):
print(f"Next action in {remaining_time} seconds...", end='\r')
time.sleep(1)
def limiter(func):
last_call_time = 0
def wrapper(*args, **kwargs):
nonlocal last_call_time
current_time = time.time()
remaining_mins = (1200 - int(current_time - last_call_time)) // 60
# Check if one hour has passed since the last call
if current_time - last_call_time >= 1200:
# Call the original function
result = func(*args, **kwargs)
# Update the last call time
last_call_time = current_time
return result
else:
print(f"A new text message can be sent in {remaining_mins}m.")
return wrapper
@limiter
def send_notification_sms(msg):
account_sid = config["Twilio"]["account_sid"]
auth_token = config["Twilio"]["auth_token"]
client = Client(account_sid, auth_token)
message = client.messages.create(
from_ = config["Twilio"]["phone_from"],
body = msg,
to = config["Twilio"]["phone_to"]
)
def send_notification_mac(title, message):
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(message, title))
def send_notification_windows(title, message):
toaster = ToastNotifier()
toaster.show_toast(title, message, duration=2.5)
def authenticate():
# Load Auth Page
url = "https://online.nzta.govt.nz/licence-test/identification"
driver.get(url)
# Send Info
number = driver.find_element(By.XPATH, '/html/body/div[1]/app-root/block-ui/div/app-identification/div/div/form/div[1]/extended-input[1]/div/div/input')
number.send_keys(config["Licence"]["number"])
version = driver.find_element(By.XPATH, '/html/body/div[1]/app-root/block-ui/div/app-identification/div/div/form/div[1]/extended-input[2]/div/div/input')
version.send_keys(config["Licence"]["version"])
lastName = driver.find_element(By.XPATH, '//html/body/div[1]/app-root/block-ui/div/app-identification/div/div/form/div[1]/extended-input[3]/div/div/input')
lastName.send_keys(config["Licence"]["lastName"])
dob = driver.find_element(By.XPATH, '/html/body/div[1]/app-root/block-ui/div/app-identification/div/div/form/div[1]/extended-input[4]/div/div/input')
dob.send_keys(config["Licence"]["dob"])
while not EC.url_changes("https://online.nzta.govt.nz/licence-test/identification")(driver):
dob.send_keys(Keys.RETURN)
time.sleep(5)
if EC.url_changes("https://online.nzta.govt.nz/licence-test/identification")(driver):
print("Authentication completed! \n")
else:
print("Authentication failed. Retrying...")
def getAvailability():
date_from = datetime.now().strftime("%d/%m/%Y")
timeslots = []
for site, id in config["Sites"].items():
# Load Site Availability
url = f"https://online.nzta.govt.nz/api/licence-test/slots/availability/Class1R?siteId={id}&dateFrom={date_from}&dateTo={config['Scanner']['date_to']}"
driver.get(url)
# Process Information and Print to Console
available_dates = []
raw_available_times = driver.find_element(By.XPATH, '/html/body/pre').text
available_times = json.loads(raw_available_times)
for slot in available_times["slotAvailability"]:
date_object = datetime.strptime(slot["slotDate"][:10], "%Y-%m-%d")
formatted_date = date_object.strftime("%d-%m-%Y")
available_dates.append(formatted_date)
availability = f"{site}: {available_dates}"
if available_dates:
timeslots.append(availability)
print(availability)
if os.name == "nt":
send_notification_windows("Timeslot(s) Found!", availability)
elif os.name == "posix":
send_notification_mac("Timeslot(s) Found!", availability)
else:
print(f"No available timeslots from {date_from} to {config['Scanner']['date_to']} for {site}.")
if timeslots:
msg = f"Timeslots found! {timeslots}"
send_notification_sms(msg)
print("")
# Program
user_input = input("Would you like the browser to run in headless mode? (Y/N) ").lower()
if not user_input or user_input == "y":
browser_options.add_argument("--headless")
driver = webdriver.Chrome(options=browser_options)
driver.implicitly_wait(10)
authenticate()
while True:
try:
getAvailability()
except:
authenticate()
sleep_counter(5)