This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
137 lines (111 loc) · 4.23 KB
/
Copy pathmain.py
File metadata and controls
137 lines (111 loc) · 4.23 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
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import select
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import os
import time
from mail import send_email
def handle_captcha(driver: WebDriver, uname: str, pw: str, otp_secret: str):
import pyotp
un = driver.find_element_by_id("okta-signin-username")
un.send_keys(uname)
pwf = driver.find_element_by_id("okta-signin-password")
pwf.send_keys(pw)
driver.find_element_by_id("okta-signin-submit").click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "input4")))
if (
driver.find_element(By.CLASS_NAME, "button-primary").get_attribute("value")
== "Send Push"
):
# change to gauth
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "link-button"))
)
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "link-button"))
)
time.sleep(1)
driver.find_element(By.CLASS_NAME, "link-button").click()
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "mfa-google-auth-30"))
)
driver.find_element(By.CLASS_NAME, "mfa-google-auth-30").click()
time.sleep(1)
# assert (
# driver.find_element(By.CLASS_NAME, "o-form-head").text
# == "Google Authenticator"
# )
totp = pyotp.TOTP(otp_secret)
code = totp.now()
codef = driver.find_element(By.NAME, "answer")
codef.send_keys(code)
time.sleep(1)
driver.find_element_by_class_name("button-primary").submit()
driver.find_element(By.XPATH, "//input[@type='submit']").click()
def daily_dec():
# does temperature + daily declaration
uname = os.environ.get("USERNAME")
pw = os.environ.get("PASSWD")
otp_secret = os.environ.get("OTP_SECRET")
CHROMEDRIVER_PATH = "/app/.chromedriver/bin/chromedriver"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_bin = os.environ.get("GOOGLE_CHROME_BIN", "chromedriver")
chrome_options.binary_location = chrome_bin
driver = webdriver.Chrome(
executable_path=CHROMEDRIVER_PATH, chrome_options=chrome_options
)
# Login phase
driver.get("https://tts.sutd.edu.sg")
time.sleep(1)
handle_captcha(driver, uname, pw, otp_secret)
time.sleep(0.5)
try:
# Move into directory
WebDriverWait(driver, 30).until(
EC.visibility_of_element_located(
(By.XPATH, '//a[text()="Daily Declaration"]')
)
)
driver.find_element_by_xpath('//a[text()="Daily Declaration"]').click()
time.sleep(0.1)
except:
handle_captcha(driver, uname, pw, otp_secret)
driver.find_element_by_name("ctl00$pgContent1$btnLogin").click()
time.sleep(0.1)
# Insert details
driver.switch_to.window(driver.window_handles[1])
WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.NAME, "ctl00$pgContent1$cbSetToNo"))
)
driver.find_element_by_name("ctl00$pgContent1$cbSetToNo").click()
time.sleep(1)
driver.find_element_by_name("ctl00$pgContent1$btnSave").click()
time.sleep(0.1)
driver.quit()
def handle_stale(func):
"""Used to rerun specific function for 5 times if element has gone stale / cannot be found"""
isStale = True
while isStale:
i = 1
try:
func()
isStale = False
except Exception as e:
i += 1
print(f"Failed to declare / record temperature!, {e}")
print(f"Retrying... Attempt {i}")
if i >= 5:
print(f"Failed to declare / record temperature!, {e}. Attempt {i}")
send_email(isSuccessful=False)
isStale = False
def dec():
handle_stale(daily_dec)
send_email()
if __name__ == "__main__":
daily_dec()