From 03f833a2c357db2b09d4556c737b59c45b3ad3b4 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 9 Jul 2023 15:27:20 +0200 Subject: [PATCH] Menu and sub-menu --- lib/WPMenu.py | 67 ++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index cae3ddc..67e5826 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -73,49 +73,56 @@ class WPMenu: except Exception as err: self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, canalblog, err)) + def _child(self, title, parent): + menu = {"id":"", "type":"", "title": "", "parent":"", "children": []} + exist = False + for i in ["categories", "tags"]: + if exist is False: + try: + params = {"search":title, "per_page":"100"} + page = self._request_wordpress.get("{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), auth=self._basic, params=params) + if page.status_code == 200: + result = page.json() + if len(result) > 0: + menu = {"id":result[0]["id"], "type":i, "title": title, "parent":parent, "children": []} + exist = True + else: + self._logger.error("{0} : {2} didn't get due status code : {1}".format(self._name, page.status_code, i)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, "{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, "{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), err)) + return menu def _menu(self, content): soup = BeautifulSoup(content, self._parser) - ul = soup.find_all("ul", id="listsmooth") - menu = list() + ul = soup.find("ul", id="listsmooth") + menus = list() + menu = {"id":"", "type":"", "title": "", "parent":"", "children": []} parents = list() - for anchor in ul[0].find_all("li"): + children = list() + for anchor in ul.find_all("li"): parent = anchor.find("a").get_text() href = anchor.find("a").get("href") + menu = self._child(parent, parent) if href == "#": - li = anchor.find_all("li") + li = anchor.find("ul").find_all("li") for child in li: a = child.find("a") self._logger.info("{0} {1} : {2}".format(self._name, anchor.find("a").get_text(), a.get_text())) - for i in ["categories", "tags"]: - exist == False - if exist is False: - try: - params = {"search":a.get_text(), "per_page":"100"} - page = self._request_wordpress.get("{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), auth=self._basic, params=params) - if page.status_code == 200: - result = page.json() - if len(result) > 0: - menu.append({"id":result[0]["id"], "type":i, "title": a.get_text(), "parent":parent}) - exist = True - else: - self._logger.error("{0} : {2} didn't get due status code : {1}".format(self._name, page.status_code, i)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - except ConnectionError as err: - self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, "{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, "{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), err)) - else: - self._logger.info("{0} : Parent title : {1}".format(self._name, parent)) - parents.append(parent) - for i in range(0, len(menu)-1): - self._logger.info("{0} : Menu : {1} {2}".format(self._name, menu[i], len(parents))) + children.append(self._child(a.get_text(), parent)) + self._logger.info(children) + menu["children"] = children + menus.append(menu) + for i in range(0, len(menus)-1): + self._logger.info("{0} : Menu : {1} {2}".format(self._name, menus[i], len(parents))) for j in range(0, len(parents)-1): - self._logger.info("{0} : Comparaison : {1} {2}".format(self._name, menu[i]["title"], parents[j])) + #self._logger.info("{0} : Comparaison : {1} {2}".format(self._name, menu[i]["title"], parents[j])) - if menu[i]["title"] == parents[j]: + if menus[i]["title"] == parents[j]: self._logger.info("{0} del : {1}".format(self._name, parents[j])) del parents[j]