-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_menuload.py
More file actions
176 lines (132 loc) · 6.99 KB
/
Copy pathedit_menuload.py
File metadata and controls
176 lines (132 loc) · 6.99 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
from random import betavariate
from bs4 import BeautifulSoup, NavigableString, Tag
from icecream import ic
import soupsieve as sv
def href_to_path(href: str) -> str:
href = href.removeprefix("https://")
href_path: str = href.replace(".covenantuniversity.edu.ng", "")
href_path = href_path.replace("../", "/Websies/")
ic(href_path)
os.makedirs(os.path.dirname(href_path), exist_ok=True)
if not os.path.exists(href_path):
create_new_file(path=href_path)
return href_path
else:return href_path
def create_new_file(path: str) -> None:
try:
with open(path, 'x') as file:
file.write("")
except FileExistsError:
pass
def check_menu_exists(menu: Tag, soup: BeautifulSoup) -> Tag | NavigableString | None:
if menu:
menu_collapse_tag: Tag = sv.select_one("[id^=collapseExample]", menu)
menu_collapse_id = str(menu_collapse_tag.get("id"))
menu_tag_exists: Tag | NavigableString | None = soup.find("div", {"id": menu_collapse_id})
return menu_tag_exists
def check_submenu_exists(menu: Tag, submenu: Tag) -> Tag | NavigableString | None:
if submenu:
submenu_href: str | list[str] | None = submenu.get("href")
submenu_tag_exists: Tag | NavigableString | None = menu.find("a", {"href": submenu_href})
return submenu_tag_exists
def add_submenu(menu: Tag, submenu: Tag, soup: BeautifulSoup, position: int=-1) -> BeautifulSoup: # type: ignore
"""IF menu and submenu are from different beautifulsoup objects
and are to be added to a parent soup object
Args:
menu (Tag): Tag
submenu (Tag): Tag
soup (BeautifulSoup): parent beautiful soup object to add menu and submenu
Returns:
BeautifulSoup: adds tags and submenus for cu portal side bar
"""
menu_tag_exists: Tag | NavigableString | None = check_menu_exists(menu=menu, soup=soup)
menu_collapse_id = str(menu_tag_exists.get("id")) # type: ignore
submenu['class'] = f"dropdown-content menu {menu_collapse_id}"
if menu_tag_exists:
menu_tag: Tag | NavigableString = menu_tag_exists
submenu_tag_exists = check_submenu_exists(menu_tag=menu_tag, submenu=submenu) # type: ignore
if not submenu_tag_exists:
div_tag: Tag | NavigableString | None | int = menu_tag.find("div", {"class": "panel-body"}) # type: ignore
div_tag.insert(position, submenu) # type: ignore
# return soup
# return soup
add_menu(menu=menu, soup=soup)
add_submenu(menu=menu, submenu=submenu, soup=soup)
def add_menu(menu: Tag, soup: BeautifulSoup, position: int=-1) -> None:
menu_exists = check_menu_exists(menu=menu, soup=soup)
if not menu_exists:
parent_div_tag = soup.find("div", {"class": "panel-group"})
parent_div_tag.insert(position, menu) # type: ignore
def remove_submenu(menu: Tag, submenu: Tag) -> None:
submenu_tag_exists = check_submenu_exists(menu=menu, submenu=submenu)
if submenu_tag_exists:
submenu_tag_exists.extract()
def remove_menu(menu: Tag, soup: BeautifulSoup) -> None:
menu_exists = check_menu_exists(menu=menu, soup=soup)
if menu_exists:
menu_exists.extract()
def remove_submenu_with_selectors(menu_selector: str, submenu_selector: str, soup: BeautifulSoup):
menu: Tag = sv.select_one(menu_selector, soup)
submenu: Tag = sv.select_one(submenu_selector, soup)
remove_submenu(menu, submenu)
def write_menuload(menuload_soup: BeautifulSoup, menuload_path: str) -> None:
menuload_soup.body.unwrap() # type: ignore
menuload_soup.html.unwrap() # type: ignore
menuload_soup.smooth()
menuload_str = menuload_soup.prettify().replace("</i>","</i> ")
menuload_str = menuload_str.replace("C:/xampp/htdocs/Websites", "")
with open(menuload_path, "w") as file:
file.write(menuload_str)
def submenu_hrefs_to_path(soup: BeautifulSoup) -> None:
href_tags = sv.select("[id^=submenu]", soup) # [attribute^=value] Represents elements with an attribute named attribute whose value starts with value.
for href_tag in href_tags:
href = str(href_tag.get("href"))
href_tag['href'] = href_to_path(href)
def add_menu_form_list_selectors(list_selectors: str, extract_soup: BeautifulSoup, soup: BeautifulSoup) -> None:
"""add menu tags from a list of css selectors in a string like so
"div:has(> div[id=collapseExample90]), div:has(> div[id=collapseExample111])"
Args:
Args:
list_selectors (str): the str containing the list of css selectors as shown above
extract_soup (BeautifulSoup): the beautifulsoup object from which the menu tags will be found
soup (BeautifulSoup): the beautifulsoup object in which the menu tags will be added to
"""
menu_list: list[Tag] = sv.select(list_selectors, extract_soup) # parent:has(> child)
for menu in menu_list:
add_menu(menu=menu, soup=soup)
def main() -> None:
menuload_path = r"C:\xampp\htdocs\Websites\menuload\menuload.php"
with open(r"C:\xampp\htdocs\Websites\menuload\extract.php", "r") as file:
extract: str = file.read()
with open(menuload_path, "r") as file:
menuload: str = file.read()
menuload_soup = BeautifulSoup(menuload, "lxml")
extract_soup = BeautifulSoup(extract, "lxml")
# student account
menu_list: list[Tag] = sv.select("div:has(> div[id=collapseExample12]), div:has(> div[id=collapseExample9])", extract_soup) # parent:has(> child)
for menu in menu_list:
add_menu(menu=menu, soup=menuload_soup, position=0)
# remove unwanted submenus in resumption
remove_submenu_with_selectors("div:has(> div[id=collapseExample99])", "[id=submenu1147]", extract_soup)
# course control
list_selectors_str = "div:has(> div[id=collapseExample126]), div:has(> div[id=collapseExample94]), div:has(> div[id=collapseExample183]), div:has(> div[id=collapseExample117])"
add_menu_form_list_selectors(list_selectors=list_selectors_str, soup=menuload_soup, extract_soup=extract_soup)
# remove unwanted course control submenus
submenu_parent = sv.select_one("div[id=collapseExample94]", menuload_soup)
submenu_list = sv.select("a[id^=submenu]", submenu_parent)
for submenu in submenu_list:
if submenu.get("id") != "submenu1053":
remove_submenu(menu=submenu_parent, submenu=submenu)
# attendance
list_selectors_str = "div:has(> div[id=collapseExample90]), div:has(> div[id=collapseExample111])"
add_menu_form_list_selectors(list_selectors=list_selectors_str, soup=menuload_soup, extract_soup=extract_soup)
#result processing
list_selectors_str = "div:has(> div[id=collapseExample68])"
add_menu_form_list_selectors(list_selectors=list_selectors_str, soup=menuload_soup, extract_soup=extract_soup)
submenu_hrefs_to_path(menuload_soup)
write_menuload(menuload_soup=menuload_soup, menuload_path=menuload_path)
if "__main__" == __name__ :
main()
# submenu_tags: Tag = sv.select_one("a[id=submenu1777]", soup) # [attribute^=value] Represents elements with an attribute named attribute whose value starts with value.
# remove_submenu(menu, submenu_tags)