-
Notifications
You must be signed in to change notification settings - Fork 2
/
pullclasses.py
178 lines (159 loc) · 5.47 KB
/
pullclasses.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
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
import time
from bs4 import BeautifulSoup
import lxml
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import json
## send post with user/pw
## get post back with json object
def pullClasses(username, password):
assignmentResults = {}
assignmentResults['Schedule'] = getClassTimes(username, password, assignmentResults, driver)
assignmentResults['Classes'] = getSakaiAssignments(username, password, assignmentResults, driver)
## post back
#jsonResult = jsonify(assignnmentResults)
jsonResult = json.dumps(assignmentResults)
return jsonResult
def getClassTimes(username , password, assignmentResults, driver):
driver.get('https://connectcarolina.unc.edu/')
#time.sleep(0.75)
# get to login page
login = driver.find_element_by_class_name("button-header")
login.click()
# login
time.sleep(3)
email = driver.find_element_by_id("username")
passwordLogin = driver.find_element_by_id("password")
## your login
email.send_keys(username)
## your password
passwordLogin.send_keys(password)
login = driver.find_element_by_name("_eventId_proceed")
login.click()
time.sleep(3)
# go to student center
login = driver.find_element_by_id("win0divPTNUI_LAND_REC_GROUPLET$2")
login.click()
time.sleep(3)
#driver.quit()
# switch to actual content
iframe = driver.find_element_by_name("TargetContent")
driver.switch_to_frame(iframe)
#open = driver.find_element_by_name("DERIVED_SSS_SCL_SS_WEEKLY_SCHEDULE")
#open.click()
html = driver.page_source
sec_soup = BeautifulSoup(html)
times = []
classes = []
classTuples = []
for span in sec_soup.select(".PSLONGEDITBOX"):
if "PM" in span.text or "AM" in span.text:
s = span.text
s = s.split("\n")
newS = ""
for i in s:
if "Room" not in i:
newS = newS + " " + i
#print(newS)
times.append(newS)
for span in sec_soup.select(".PSHYPERLINKDISABLED"):
s = span.text
s = s.split("\n")[0]
classes.append(s)
for i in range(len(times)):
tuple = {}
tuple['class'] = classes[i]
tuple['time'] = times[i]
classTuples.append(tuple)
return classTuples
def getSakaiAssignments(username, password, assignmentResults, driver):
driver.get('https://sakai.unc.edu/')
loginClick = driver.find_element_by_id("login_btn1")
loginClick.click()
time.sleep(3)
email = driver.find_element_by_id("username")
passwordLogin = driver.find_element_by_id("password")
## your login
email.send_keys(username)
## your password
passwordLogin.send_keys(password)
login = driver.find_element_by_name("_eventId_proceed")
login.click()
#time.sleep(5)
## need to grab all classes
#comp311 = driver.find_element_by_link_text("COMP311.001.FA20")
#comp311.click()
#
classes = driver.find_elements_by_class_name("link-container")
classLinks = {}
for i in classes:
c = i.get_attribute('title')
if 'FA20' in c:
temp = c.split(".")[0]
#print(temp)
classLinks[temp] = i.get_attribute('href')
assignmentResultsT = {}
for i in classLinks:
assignmentResultsT[i] = getAssignmentsForClass(classLinks[i], driver)
return assignmentResultsT
def getAssignmentsForClass(url, driver):
driver.get(url)
time.sleep(3)
links = driver.find_elements_by_class_name("Mrphs-toolsNav__menuitem--link ")
assignmentPage = False
for link in links:
if "assignment" in link.get_attribute("title"):
assignmentPage = True
link.click()
break
if not assignmentPage:
return
assignments = []
html = driver.page_source
sec_soup = BeautifulSoup(html)
links = []
for span in sec_soup.select('a[name="asnActionLink"]'):
links.append(span.get("href"))
titles = []
for span in sec_soup.select('a[name="asnActionLink"]'):
titles.append(span.get("title"))
status = []
for span in sec_soup.select('td[headers="status"]'):
s = span.text
s = s.replace("\t","")
s = s.replace("\n","")
status.append(s)
opens = []
for span in sec_soup.select('td[headers="openDate"]'):
s = span.text
s = s.replace("\t","")
s = s.replace("\n","")
s = s.replace(",","")
s = s.replace(" AM","AM")
s = s.replace(" PM","PM")
date_time_str = 'Jun 28 2018 7:40AM'
date_time_obj = datetime.datetime.strptime(s, '%b %d %Y %I:%M%p')
#print(date_time_obj)
opens.append(date_time_obj)
due = []
for span in sec_soup.select('td[headers="dueDate"]'):
try:
s = span.text
s = s.replace("\t","")
s = s.replace("\n","")
s = s.replace(",","")
s = s.replace(" AM","AM")
s = s.replace(" PM","PM")
date_time_obj = datetime.datetime.strptime(s, '%b %d %Y %I:%M%p')
due.append(date_time_obj)
except:
due.append("")
for i in range(len(status)):
assignmentObj = {}
assignmentObj['Status'] = status[i]
assignmentObj['Title'] = titles[i]
assignmentObj['Open'] = opens[i]
assignmentObj['Due'] = due[i]
assignmentObj['Link'] = links[i]
assignments.append(assignmentObj)
return assignments