-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule_class.py
145 lines (119 loc) · 5.67 KB
/
schedule_class.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
# from random import random
from datetime import datetime, timedelta
import time
from selenium import webdriver
from selenium.webdriver.common import keys
from selenium.webdriver.common.action_chains import ActionChains
_SHORT_TIMEOUT = 1.5
_LONG_TIMEOUT = 5
password = ''
course_name = 'ITEC-621-020'
start_date = datetime(2019, 4, 1)
target_hour = '7'
target_min = '30'
target_pm = 'pm'
def format_date(date_str):
temp = re.sub(' 0([\d])', ' \g<1>', date_str)
temp = re.sub('(?<=[\d])(?:st|nd|rd|th)', '', temp)
return temp
def match_time(time_box, target_hour, target_min, target_pm):
"""Click time box and find active element"""
time_box.click()
def find_active_element():
# time_box.click()
current_hour, current_min, current_pm = time_box.get_attribute('aria-label').split('Start Time of meeting: ')[1].split(' ')
time_box.send_keys(keys.Keys.ARROW_DOWN)
next_hour, next_min, next_pm = time_box.get_attribute('aria-label').split('Start Time of meeting: ')[1].split(' ')
time_box.send_keys(keys.Keys.ARROW_UP)
time_box.send_keys(keys.Keys.ARROW_UP)
prev_hour, prev_min, prev_pm = time_box.get_attribute('aria-label').split('Start Time of meeting: ')[1].split(' ')
time_box.send_keys(keys.Keys.ARROW_DOWN)
# which one is diff??
if current_hour != next_hour or current_hour != prev_hour:
return 'hour'
elif current_min != next_min or current_min != prev_min:
return 'min'
# elif current_pm != next_pm or current_pm != prev_pm:
# return 'pm'
else:
# return None
return 'pm'
elements = ['hour', 'min', 'pm']
target_vals = [target_hour, target_min, target_pm]
for n in range(3):
idx = elements.index(find_active_element())
print(idx)
current_val = time_box.get_attribute('aria-label').split('Start Time of meeting: ')[1].split(' ')[idx]
target_val = target_vals[idx]
num_iter = 0
while current_val != target_val and num_iter <= 24:
time_box.send_keys(keys.Keys.ARROW_DOWN)
current_val = time_box.get_attribute('aria-label').split('Start Time of meeting: ')[1].split(' ')[idx]
num_iter += 1
time.sleep(0.2)
time_box.send_keys(keys.Keys.ARROW_RIGHT)
time.sleep(0.2)
# Login - Can do manually, too
driver = webdriver.Chrome('/Users/gfiddyment/bin/chromedriver')
driver.get('https://2ksb.onlinebusiness.american.edu')
time.sleep(_SHORT_TIMEOUT)
driver.find_element_by_xpath('//input[@id="username"]').click()
actions_user = ActionChains(driver)
actions_user.move_to_element(driver.find_element_by_xpath('//input[@id="username"]'))\
.send_keys('gfiddy@american.edu').perform()
driver.find_element_by_xpath('//button[@id="login-submit"]').click()
time.sleep(_SHORT_TIMEOUT)
actions_pwd = ActionChains(driver)
actions_pwd.move_to_element(driver.find_element_by_xpath('//input[@id="password"]'))\
.send_keys(password).perform()
driver.find_element_by_xpath('//button[@id="login-submit"]').click()
time.sleep(_LONG_TIMEOUT)
# Go to course url
course_url = 'https://2ksb.onlinebusiness.american.edu/course/view.php?id=398&group=1138'
driver.get(course_url)
# Loop!
# for week_number in range(1, 11):
for week_number in range(2, 11):
print('Week', week_number)
class_date = start_date + timedelta(weeks=week_number-1)
target_date = format_date(class_date.strftime('%A %B %d %Y'))
# Click create meeting
driver.find_element_by_xpath('//button[@id="open-create-modal"]').click()
time.sleep(_SHORT_TIMEOUT)
# Select invitees
driver.find_element_by_xpath('//input[@id="invitee_value"]').send_keys(course_name)
time.sleep(_SHORT_TIMEOUT)
class_matches = driver.find_elements_by_xpath('//div[@class="angucomplete-title ng-binding ng-scope"]')
for m in class_matches:
try:
if m.text == course_name:
m.click()
except:
pass
# Select meeting type = class discussion
driver.find_element_by_xpath('//option[@label="class discussion"]').click()
# Name session
driver.find_element_by_xpath('//input[@name="name"]').send_keys('W' + str(week_number))
# Attendance tracking click and go (week_number / 1) entries down
driver.find_element_by_xpath('//option[@label="Week {} Live Session"]'.format(str(week_number))).click()
# Set duration = 110
driver.find_element_by_xpath('//input[@id="meetingduration"]').send_keys(keys.Keys.BACKSPACE)
driver.find_element_by_xpath('//input[@id="meetingduration"]').send_keys(keys.Keys.BACKSPACE)
driver.find_element_by_xpath('//input[@id="meetingduration"]').send_keys('110')
# Set recording = invitees
driver.find_element_by_xpath('//option[@label="Invitees"]'.format(str(week_number))).click()
# Set date and time
date_box = driver.find_element_by_xpath('//input[@id="meetingdate"]')
time_box = driver.find_element_by_xpath('//input[@id="meetingtime"]')
date_box.click()
current_input_date = format_date(date_box.get_attribute('aria-label').split('Meeting Date: ')[1])
while current_input_date != target_date:
date_box.send_keys(keys.Keys.ARROW_RIGHT)
current_input_date = format_date(date_box.get_attribute('aria-label').split('Meeting Date: ')[1])
driver.find_element_by_xpath('//textarea[@id="meetingdescription"]').click() # click out
match_time(time_box, target_hour, target_min, target_pm)
driver.find_element_by_xpath('//textarea[@id="meetingdescription"]').click() # click out
# Click create
driver.find_element_by_xpath('//button[@ng-click="meetingFormCtrl.submit($event)"]').click()
print('Success!')
time.sleep(_SHORT_TIMEOUT)