-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
156 lines (130 loc) · 5.35 KB
/
agent.py
File metadata and controls
156 lines (130 loc) · 5.35 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
from selenium import webdriver
from selenium.webdriver.edge.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
import json
import tempfile
import shutil
import os
import logging
logger = logging.getLogger("worker")
import requests
#funtion to call
def selenium_login():
try:
logger.info("Started Selenium login")
#Creds
with open(os.path.join("creds", "creds.json"), "r") as f:
creds = json.load(f)
username = creds['username']
password = creds['password']
duo_webhook_url = creds['duo_webhook_url']
#Options
profile_dir = tempfile.mkdtemp()
options = Options()
options.add_argument(f"--user-data-dir={profile_dir}")
options.add_argument("--log-level=3")
options.add_argument("--disable-cache")
options.add_argument("--disk-cache-size=0")
options.add_argument("--headless=new") ###Adjust to see what's happening###
options.add_argument("--blink-settings=imagesEnabled=false")
#Driver
driver = webdriver.Edge(options=options)
#CAS Login
try:
driver.get("https://login.uconn.edu/cas/login?service=https://student.studentadmin.uconn.edu:443/psp/CSPR/?cmd=start")
driver.find_element("id", "username").send_keys(username)
driver.find_element("id", "password").send_keys(password)
driver.find_element("id", "submitBtn").click()
except Exception as e:
logger.error(f"Error during CAS login: {e}")
raise
#DUO Login
try:
number = WebDriverWait(driver, 30).until(
lambda d: d.find_element(By.CLASS_NAME, "verification-code")
).text
data = {
"content": number
}
try:
requests.post(duo_webhook_url, json=data)
except Exception as e:
logger.error(f"Exception occurred while sending Discord alert: {e}")
raise
duo_elem = WebDriverWait(driver, 30).until(
lambda d: d.find_element(By.ID, "dont-trust-browser-button")
)
duo_elem.click()
except Exception as e:
logger.error(f"Error during DUO login: {e}")
raise
#Student Admin
try:
WebDriverWait(driver, 10).until(
lambda d: d.title == "Homepage"
)
student_elem = WebDriverWait(driver, 10).until(
lambda d: d.find_element(By.ID, "win0divPTNUI_LAND_REC_GROUPLET$7")
)
driver.execute_script("arguments[0].scrollIntoView(true);", student_elem)
driver.execute_script("arguments[0].click();", student_elem)
except Exception as e:
logger.error(f"Error navigating to Student Admin: {e}")
raise
#Shopping Cart
try:
shopping_cart_div = WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.XPATH, "//div[@steplabel='Shopping Cart']"))
)
shopping_cart_url = shopping_cart_div.get_attribute("href")
driver.get(shopping_cart_url)
except Exception as e:
logger.error(f"Error navigating to Shopping Cart: {e}")
raise
#Grab Data
try:
#Cookies
cookies = {c['name']: c['value'] for c in driver.get_cookies() if c['name']}
#Current URL
current_url = driver.current_url
#State tokens
state_tokens = {}
hidden_inputs = driver.find_elements(By.XPATH, "//input[@type='hidden']")
for inp in hidden_inputs:
name = inp.get_attribute("name")
value = inp.get_attribute("value")
if name and value:
state_tokens[name] = value
#Headers for requests
headers = {
"User-Agent": driver.execute_script("return navigator.userAgent;"),
"Referer": current_url,
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}
#Persist essential session data
session_data = {
"url": current_url,
"method": "GET",
"cookies": cookies,
"headers": headers,
"state_tokens": state_tokens,
}
except Exception as e:
logger.error(f"Error gathering session data: {e}")
raise
#Save Session Data
try:
with open(os.path.join("creds", "session.json"), "w") as f:
json.dump(session_data, f, indent=2)
except Exception as e:
logger.error(f"Error saving session data: {e}")
raise
except Exception as e:
logger.error(f"Selenium login failed due to an unexpected error: {e}")
raise
finally:
driver.quit()
shutil.rmtree(profile_dir, ignore_errors=True) #Deletes temp file
logger.info("Selenium login completed and session data saved.")