From c6894648d15e94c9370745564cfd8f2f36f1b6b3 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 18 Jul 2023 22:09:20 +0200 Subject: [PATCH] add link --- lib/WPMenu.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 174827d..944d2f4 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -83,7 +83,7 @@ class WPMenu: return title_rendered def _getIdfromTitlePost(self, content): - idMenu = {"id":0, "type":""} + idMenu = {"id":0, "type":"", "link":""} soup = BeautifulSoup(content, self._parser) articletitle = soup.find("h2", class_="articletitle").get_text() exist = False @@ -104,7 +104,7 @@ class WPMenu: self._logger.debug("{0} : comparaison debug {1} {2}".format(self._name, articletitle, title_rendered)) if articletitle == title_rendered: self._logger.debug("{0} : get post id : {1}".format(self._name, i)) - idMenu = {"id":i["id"], "type":"post"} + idMenu = {"id":i["id"], "type":"post", "link": i["link"]} exist = True else: self._logger.debug("{0} : {2} {1}".format(self._name, result, len(result))) @@ -123,7 +123,7 @@ class WPMenu: return idMenu def _getIdFromPost(self, href): - idMenu = {"id":0, "type":""} + idMenu = {"id":0, "type":"", "link":""} o = urlparse(href) if len(o.netloc) > 0: try: @@ -151,7 +151,7 @@ class WPMenu: def _getIdFromReverse(self, title, href): self._logger.info("{0} : get title {1} from href {2}".format(self._name, title, href)) - idMenu = {"id":0, "type":""} + idMenu = {"id":0, "type":"", "link":""} if href != "#": title = href[::-1] link = title.split("/")[0] @@ -167,7 +167,7 @@ class WPMenu: return idMenu def _getId(self, title): - idMenu = {"id": 0, "type":""} + idMenu = {"id": 0, "type":"", "link":""} exist = False if exist is False: for i in ["categories", "tags"]: @@ -183,7 +183,7 @@ class WPMenu: self._logger.info("{0} info : {1} {2} {3}".format(self._name, j["name"], j["slug"], title)) if j["name"] == title or j["slug"] == title: self._logger.info("{0} : comparaison ok : {1} {2}".format(self._name, j["id"], i)) - idMenu = {"id": j["id"], "type": i} + idMenu = {"id": j["id"], "type": i, "link": j["link"]} exist = True else: break @@ -211,14 +211,14 @@ class WPMenu: href = anchor.find("a").get("href") if href == "{0}://{1}/".format(self._protocol_canalblog, self._canalblog): parent = "home" - itemMenu = {"id":"", "type":"", "title": parent, "href":href, "children":list()} + itemMenu = {"id":"", "type":"", "title": parent, "link":"", "href":href, "children":list()} if href == "#": li = anchor.find("ul").find_all("li") for child in li: a = child.find("a") href = a.get("href") self._logger.info("{0} Parent {1} : Child {2}".format(self._name, parent, a.get_text())) - children.append({"title": a.get_text(), "parent": parent, "href":href}) + children.append({"title": a.get_text(), "parent": parent, "href":href, "link":""}) menu.append(itemMenu) @@ -233,7 +233,7 @@ class WPMenu: for j in range(0, len(menu)): self._logger.info("{0} : Children for : {1}".format(self._name, menu[j]["title"])) if menu[j]["title"] == children[i]["parent"]: - menu[j]["children"].append({"id":"", "type":"", "title":children[i]["title"], "parent": children[i]["parent"], "href":children[i]["href"]}) + menu[j]["children"].append({"id":"", "type":"", "title":children[i]["title"], "parent": children[i]["parent"], "link":"", "href":children[i]["href"]}) for i in range(0, len(menu)): self._logger.info("{0} : Menu {1} {2}".format(self._name, menu[i]["title"], len(menu[i]["children"]))) @@ -244,16 +244,18 @@ class WPMenu: self._logger.debug("{0} : content children {1}".format(self._name, menu[i]["children"][j])) idMenu = self._getIdFromReverse(menu[i]["children"][j]["title"], menu[i]["children"][j]["href"]) if idMenu["id"] != 0: - menu[i]["children"][j] = {"id":idMenu["id"], "type": idMenu["type"], "title": menu[i]["children"][j]["title"], "parent": menu[i]["children"][j]["parent"]} + menu[i]["children"][j] = {"id":idMenu["id"], "type": idMenu["type"], "link": idMenu["link"], "title": menu[i]["children"][j]["title"], "parent": menu[i]["children"][j]["parent"]} idMenu = self._getId(menu[i]["title"]) self._logger.debug("{0} : content parent {1}".format(self._name, menu[i])) + self._logger.debug("{0} : content idMenu {1}".format(self._name, idMenu)) + if idMenu["id"] == 0: idMenu = self._getIdFromReverse(menu[i]["title"], menu[i]["href"]) if idMenu["id"] != 0: - menu[i] = {"id":idMenu["id"], "type": idMenu["type"], "title":menu[i]["title"], "children": menu[i]["children"]} + menu[i] = {"id":idMenu["id"], "type": idMenu["type"], "title":menu[i]["title"], "link":idMenu["link"], "children": menu[i]["children"]} for i in menu: - self._logger.info("{0} Menu parent {1} with id {2} and type {3}".format(self._name, i["title"], i["id"], i["type"])) + self._logger.info("{0} Menu parent {1} with id {2} and type {3} and link {4}".format(self._name, i["title"], i["id"], i["type"], i["link"])) for j in i["children"]: - self._logger.info("{0} Menu children {1} with id {2} and type {3}".format(self._name, j["title"], j["id"], j["type"])) + self._logger.info("{0} Menu children {1} with id {2} and type {3} and link {4}".format(self._name, j["title"], j["id"], j["type"], j["link"])) \ No newline at end of file