-
Notifications
You must be signed in to change notification settings - Fork 0
/
alumni_former_people.py
105 lines (76 loc) · 3.76 KB
/
alumni_former_people.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
# Used to add alumni and former carleton people into WHMCS groups
# Should have the role written in WHMCS admin notes
# Matches by looking for the word 'Alum' or the word 'Former'
# in the WHMCS admin notes
# needs to install selenium and have a config file with the WHMCS login information
import config
import csv
from bs4 import BeautifulSoup
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
groups = ['Alum', 'Former']
id_grp = {'Alum' : 2, 'Former' : 3}
def add_to_group(driver):
while(len(driver.find_elements(By.CSS_SELECTOR, 'textarea')) == 0):
time.sleep(0.5)
text_box = driver.find_element(By.CSS_SELECTOR, 'textarea')
user_id = int(driver.find_element(By.CSS_SELECTOR, '#userId').text)
email = driver.find_element(By.CLASS_NAME, 'clientssummarystats').find_elements(By.CSS_SELECTOR, 'tr')[3].find_elements(By.CSS_SELECTOR, 'td')[1].text
role = text_box.text
print(email, role)
if role == "":
print("This person has no role, cannot categorize in a group")
return
original = role
role = role.split()[0]
if role not in '|'.join(groups):
print('This person is not of a role we want to categorize')
return
print("Found a group that matches!")
driver.find_element(By.ID, 'clientTab-2').click()
while(len(driver.find_elements(By.XPATH, '/html/body/div[8]/div[1]/div/div/form/table/tbody/tr[14]/td[4]/select')) == 0):
time.sleep(1)
choose_group = driver.find_element(By.XPATH, '/html/body/div[8]/div[1]/div/div/form/table/tbody/tr[14]/td[4]/select')
for grp in groups: # find the group name that matches my role and then add it to the group
if role in grp:
choose_group.find_element(By.CSS_SELECTOR, f'[value="{id_grp[grp]}"]').click()
driver.find_element(By.XPATH, '/html/body/div[8]/div[1]/div/div/form/div/input[1]').click()
print('Added user to group')
def launch_browser():
s=Service('./geckodriver')
driver = webdriver.Firefox(service=s)
return driver
def main():
# launches browser
with launch_browser() as driver:
driver.get('https://sites.carleton.edu/manage/whmcs-admin/login.php?logout=1')
driver.maximize_window()
# logs in
driver.find_element(By.NAME, 'username').send_keys(config.username)
driver.find_element(By.NAME,'password').send_keys(config.password)
driver.find_element(By.CSS_SELECTOR,'input[value=Login]').click()
# nagivate to clients page
driver.find_element(By.ID, 'Menu-Clients').click()
# enables the view of inactive accounts
driver.find_element(By.CLASS_NAME, 'bootstrap-switch').click()
time.sleep(1)
for i in range(100): # I've set 100 as a safety number in case the loop doesn't stop for some reason
time.sleep(3)
# selects every user and opens and then scraps their respective pages
for user in driver.find_element(By.ID, "sortabletbl0").find_elements(By.CSS_SELECTOR, "tr")[1:]:
# opens the page of the user in a new window
user.find_element(By.CSS_SELECTOR, "a").send_keys('\ue009' + '\ue007') # Control + Enter
# goes to the user window, scrapes it and comes back
driver.switch_to.window(driver.window_handles[1])
add_to_group(driver)
driver.close()
driver.switch_to.window(driver.window_handles[0])
# while there is a link to go to the next page I click, otherwise I break to stop
try:
driver.find_element(By.PARTIAL_LINK_TEXT, "Next Page").click()
except:
break
if __name__ == '__main__':
main()