-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupfuncs.py
85 lines (69 loc) · 2.47 KB
/
groupfuncs.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
"""
groupfuncs.py
Support functions for groups
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
def get_group_link_elements(driver, wait):
'''
gets a tags inside the divs for groups
'''
try:
group_div_xpath = '//div[@data-visualcompletion="ignore-dynamic"]/a'
wait.until(EC.presence_of_element_located((By.XPATH, group_div_xpath)))
groups = driver.find_elements(By.XPATH, group_div_xpath)
return groups
except:
return None
def scroll_into_view(driver, element):
'''
scrolls untils the element is visible on the screen
'''
try:
driver.execute_script("arguments[0].scrollIntoView(true);", element)
except:
print("[*] => (func: scroll_into_view) - Error while scrolling to the element")
def leave_group(wait):
try:
wait.until(EC.presence_of_element_located((By.XPATH, '//span[text()="Joined"]'))).click()
wait.until(EC.presence_of_element_located((By.XPATH, '//span[text()="Leave group"]'))).click()
wait.until(EC.presence_of_element_located((By.XPATH, '//span[text()="Leave Group"]'))).click()
return True
except:
return False
def switch_tab(driver, window_handle):
try:
driver.switch_to.window(window_handle)
return True
except:
return False
def open_new_tab(driver):
'''
opens a new tab and returns the handle
'''
try:
driver.execute_script("window.open('');")
return driver.window_handles[len(driver.window_handles)-1]
except:
return None
def get_excluded_group_names(fname):
'''
reads group names from a file
'''
with open(fname,'rb') as fh:
content = fh.read()
group_names_split = content.decode('utf-8').split('\n')
group_names = [name.lower() for name in group_names_split if name != '']
group_names = list(map(lambda name: name.replace('\r',''), group_names))
group_names = [name for name in group_names if name != '']
return group_names
def dump_groups(groups, fname):
'''
dumps the group names into a file
'''
with open(fname, 'wb') as fh:
for group in groups:
group_name = group.text.split('\n')[0]
if group_name.lower() not in ['your feed', 'discover', 'your notifications']:
fh.write(group_name.encode('utf-8'))
fh.write("\n".encode('utf-8'))