From d10867a9837db6f37343296887e42ea276554b10 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 30 Jun 2023 23:28:54 +0200 Subject: [PATCH 01/35] add class WPMenu --- lib/WPMenu.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/WPMenu.py diff --git a/lib/WPMenu.py b/lib/WPMenu.py new file mode 100644 index 0000000..cfa2f28 --- /dev/null +++ b/lib/WPMenu.py @@ -0,0 +1,31 @@ +#!/usr/bin/python3 + +from bs4 import BeautifulSoup +from urllib.parse import urlparse +import requests, os, logging, re, json +from requests.adapters import HTTPAdapter +from requests.packages.urllib3.util.retry import Retry + +class WPMenu: + # Constructor + def __init__(self, name="Thread-0", basic=None, wordpress="", logger=None, parser="html.parser", ssl_wordpress=True): + self._name = name + self._basic = basic + self._wordpress = wordpress + self._logger = logger + self._parser = parser + self._headers_json = {'Content-Type': 'application/json; charset=utf-8', 'Accept':'application/json'} + self._protocol = "https" + self._directory = "backup" + if ssl_wordpress is False: + self._protocol = "http" + self._request = requests.Session() + + retries = Retry(connect=10, read=10, redirect=5, + status_forcelist=[429, 500, 502, 503, 504], backoff_factor=2) + + self._request.mount('{0}://'.format(self._protocol), HTTPAdapter(max_retries=retries)) + + # Destructor + def __del__(self): + print("{0} : Import finished for {1}".format(self._name, self._wordpress)) \ No newline at end of file From 4b6b06aadeb88804c3b4f763c29f12f279b01bf0 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 30 Jun 2023 23:52:56 +0200 Subject: [PATCH 02/35] add public method from URL --- lib/WPMenu.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index cfa2f28..d2bb997 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -8,17 +8,21 @@ from requests.packages.urllib3.util.retry import Retry class WPMenu: # Constructor - def __init__(self, name="Thread-0", basic=None, wordpress="", logger=None, parser="html.parser", ssl_wordpress=True): + def __init__(self, name="Thread-0", basic=None, canalblog="", wordpress="", logger=None, parser="html.parser", ssl_canalblog=True, ssl_wordpress=True): self._name = name self._basic = basic + self._canalblog = canalblog self._wordpress = wordpress self._logger = logger self._parser = parser self._headers_json = {'Content-Type': 'application/json; charset=utf-8', 'Accept':'application/json'} - self._protocol = "https" + self._protocol_wordpress, self._protocol_canalblog = "https" self._directory = "backup" + if ssl_wordpress is False: - self._protocol = "http" + self._protocol_wordpress = "http" + if ssl_canalblog is False: + self._protocol_canalblog = "http" self._request = requests.Session() retries = Retry(connect=10, read=10, redirect=5, @@ -28,4 +32,25 @@ class WPMenu: # Destructor def __del__(self): - print("{0} : Import finished for {1}".format(self._name, self._wordpress)) \ No newline at end of file + print("{0} : Import finished for {1}".format(self._name, self._wordpress)) + + # Public method + + ## Get from URL + + def fromUrl(self): + try: + o = urlparse(self._canalblog) + o = o._replace(scheme=self._protocol_canalblog) + i = o.geturl().replace(":///", "://") + page = self._request.get(i) + if page.status_code == 200: + soup = BeautifulSoup(page.text, self._parser) + else: + self._logger.error("{0} : index didn't get due status code : {1}".format(self._name, page.status_code)) + 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, webpage[i], err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, webpage[i], err)) \ No newline at end of file From 47f504beb559c8a51f4569470ae9d6303fca902e Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 1 Jul 2023 00:11:43 +0200 Subject: [PATCH 03/35] list tag --- lib/WPMenu.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index d2bb997..287702b 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -46,6 +46,11 @@ class WPMenu: page = self._request.get(i) if page.status_code == 200: soup = BeautifulSoup(page.text, self._parser) + ul = soup.find_all("ul", id="listsmooth") + for anchor in ul[0].find_all("li"): + li = anchor.find_all("li") + for content in li: + a = content.find("a") else: self._logger.error("{0} : index didn't get due status code : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content)) From f0b4ba5e27978d4a03624530c57a6ebd6182bca6 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 1 Jul 2023 21:56:10 +0200 Subject: [PATCH 04/35] add url and file --- import_export_canalblog.py | 19 +++++++++++++++++-- lib/WPMenu.py | 39 ++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/import_export_canalblog.py b/import_export_canalblog.py index a404d16..de1c15e 100644 --- a/import_export_canalblog.py +++ b/import_export_canalblog.py @@ -10,6 +10,7 @@ from lib.WPImport import WPimport from lib.WPExport import WPExport from lib.WPRemove import WPRemove from lib.WPChange import WPChange +from lib.WPMenu import WPMenu def errorRevert(logger, revert, tmp): if revert is True: @@ -202,8 +203,14 @@ if __name__ == '__main__': change_parser.add_argument("--file", default="", help="File") - + menu_parser = subparsers.add_parser("menu") + menu_parser.add_argument("--user", help="wordpress user", required=True) + menu_parser.add_argument("--password", help="password wordpress's user", default="") + menu_parser.add_argument("--file", help="HTML file", default="") + menu_parser.add_argument("--canalblog", help="URL Canalblog", default="") + menu_parser.add_argument("--wordpress", help="URL Wordpress", required=True) + args = parser.parse_args() logger = logging.getLogger('import export canalblog') @@ -240,7 +247,7 @@ if __name__ == '__main__': os.makedirs(args.tmp, exist_ok=True) - if args.command == "import" or args.command == "remove": + if args.command == "import" or args.command == "remove" or args.command = "menu": password = args.password if len(args.password) == 0: password = getpass() @@ -341,4 +348,12 @@ if __name__ == '__main__': changeWp = WPChange(logger=logger) for filei in args.file.split(","): changeWp.fromFile(filei) + exit(0) + + if args.command == "menu": + menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress) + if len(args.file) > 0: + menuWp.fromFile(args.file) + if len(args.canalblog) > 0: + menuWp.fromUrl(args.canalblog) exit(0) \ No newline at end of file diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 287702b..90d9dfc 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -34,23 +34,32 @@ class WPMenu: def __del__(self): print("{0} : Import finished for {1}".format(self._name, self._wordpress)) - # Public method + # Public method + + ## From file + + def fromFile(self, files): + if os.path.exists(files): + with open(files, 'r') as f: + self._logger.info("{0} : File is being processed : {1}".format(self._name, files)) + content = f.read() + self._menu(content) + else: + self._logger.error("{0} : File isn't exist : {1}".format(self._name, files)) + + ## Get from URL - def fromUrl(self): + def fromUrl(self, canalblog): try: - o = urlparse(self._canalblog) + o = urlparse(canalblog) o = o._replace(scheme=self._protocol_canalblog) i = o.geturl().replace(":///", "://") page = self._request.get(i) if page.status_code == 200: - soup = BeautifulSoup(page.text, self._parser) - ul = soup.find_all("ul", id="listsmooth") - for anchor in ul[0].find_all("li"): - li = anchor.find_all("li") - for content in li: - a = content.find("a") + self._logger.info("{0} : Page web is being processed : {1}".format(self._name, i)) + self._menu(page.content) else: self._logger.error("{0} : index didn't get due status code : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content)) @@ -58,4 +67,14 @@ class WPMenu: self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, webpage[i], err)) exit(1) except Exception as err: - self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, webpage[i], err)) \ No newline at end of file + self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, webpage[i], err)) + + + def _menu(self, content): + + soup = BeautifulSoup(page.text, self._parser) + ul = soup.find_all("ul", id="listsmooth") + for anchor in ul[0].find_all("li"): + li = anchor.find_all("li") + for content in li: + a = content.find("a") From 9bbf769b40b1da6d92fbaa15572e8589f3676b5a Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 1 Jul 2023 22:16:35 +0200 Subject: [PATCH 05/35] test WIP --- import_export_canalblog.py | 2 +- lib/WPMenu.py | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/import_export_canalblog.py b/import_export_canalblog.py index de1c15e..70cccdd 100644 --- a/import_export_canalblog.py +++ b/import_export_canalblog.py @@ -247,7 +247,7 @@ if __name__ == '__main__': os.makedirs(args.tmp, exist_ok=True) - if args.command == "import" or args.command == "remove" or args.command = "menu": + if args.command == "import" or args.command == "remove" or args.command == "menu": password = args.password if len(args.password) == 0: password = getpass() diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 90d9dfc..b118276 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -16,19 +16,23 @@ class WPMenu: self._logger = logger self._parser = parser self._headers_json = {'Content-Type': 'application/json; charset=utf-8', 'Accept':'application/json'} - self._protocol_wordpress, self._protocol_canalblog = "https" + self._protocol_wordpress = "https" + self._protocol_canalblog = "https" self._directory = "backup" if ssl_wordpress is False: self._protocol_wordpress = "http" if ssl_canalblog is False: self._protocol_canalblog = "http" - self._request = requests.Session() + self._request_canalblog = requests.Session() + self._request_wordpress = requests.Session() retries = Retry(connect=10, read=10, redirect=5, status_forcelist=[429, 500, 502, 503, 504], backoff_factor=2) - self._request.mount('{0}://'.format(self._protocol), HTTPAdapter(max_retries=retries)) + self._request_canalblog.mount('{0}://'.format(self._protocol_canalblog), HTTPAdapter(max_retries=retries)) + self._request_wordpress.mount('{0}://'.format(self._protocol_wordpress), HTTPAdapter(max_retries=retries)) + # Destructor def __del__(self): @@ -56,7 +60,7 @@ class WPMenu: o = urlparse(canalblog) o = o._replace(scheme=self._protocol_canalblog) i = o.geturl().replace(":///", "://") - page = self._request.get(i) + page = self._request_canalblog.get(i) if page.status_code == 200: self._logger.info("{0} : Page web is being processed : {1}".format(self._name, i)) self._menu(page.content) @@ -64,17 +68,18 @@ class WPMenu: self._logger.error("{0} : index didn't get due status code : {1}".format(self._name, page.status_code)) 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, webpage[i], err)) + self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, canalblog, err)) exit(1) except Exception as err: - self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, webpage[i], err)) + self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, canalblog, err)) def _menu(self, content): - soup = BeautifulSoup(page.text, self._parser) + soup = BeautifulSoup(content, self._parser) ul = soup.find_all("ul", id="listsmooth") for anchor in ul[0].find_all("li"): li = anchor.find_all("li") - for content in li: - a = content.find("a") + for child in li: + a = child.find("a") + self._logger.info("{0} {1} : {2}".format(self._name, child, a)) From 95f5203727e13cb27399316c4d00f014e90715a1 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 4 Jul 2023 00:26:27 +0200 Subject: [PATCH 06/35] list menu with id --- lib/WPMenu.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index b118276..bcdac72 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -78,8 +78,27 @@ class WPMenu: soup = BeautifulSoup(content, self._parser) ul = soup.find_all("ul", id="listsmooth") + menu = list() for anchor in ul[0].find_all("li"): li = anchor.find_all("li") for child in li: a = child.find("a") - self._logger.info("{0} {1} : {2}".format(self._name, child, a)) + self._logger.info("{0} {1} : {2}".format(self._name, anchor.find("a").get_text(), a.get_text())) + for i in ["categories", "tags"]: + 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":anchor.find("a").get_text()}) + 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)) + for i in menu: + self._logger.info("{0} : Menu : {1}".format(self._name, i)) \ No newline at end of file From c6ccf98b1bc5026bd127c05cfac619472b226cd9 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 6 Jul 2023 00:42:47 +0200 Subject: [PATCH 07/35] working menu --- lib/WPMenu.py | 60 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index bcdac72..6b7af6c 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -79,26 +79,42 @@ class WPMenu: soup = BeautifulSoup(content, self._parser) ul = soup.find_all("ul", id="listsmooth") menu = list() + parents = list() for anchor in ul[0].find_all("li"): - li = anchor.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"]: - 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":anchor.find("a").get_text()}) - 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)) - for i in menu: - self._logger.info("{0} : Menu : {1}".format(self._name, i)) \ No newline at end of file + parent = anchor.find("a").get_text() + href = anchor.find("a").get("href") + if href == "#": + li = anchor.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"]: + 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}) + 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))) + for j in range(0, len(parents)-1): + self._logger.info("{0} : Comparaison : {1} {2}".format(self._name, menu[i]["title"], parents[j])) + + if menu[i]["title"] == parents[j]: + self._logger.info("{0} del : {1}".format(self._name, parents[j])) + del parents[j] + + for i in parents: + self._logger.info("{0} : Parent : {1}".format(self._name, i)) \ No newline at end of file From 9acb620f93014f07096cb8dfb05c9fbb218bffa3 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 9 Jul 2023 12:29:13 +0200 Subject: [PATCH 08/35] add condition if --- lib/WPMenu.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 6b7af6c..cae3ddc 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -89,21 +89,24 @@ class WPMenu: 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"]: - 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}) - 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)) + 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) From 03f833a2c357db2b09d4556c737b59c45b3ad3b4 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 9 Jul 2023 15:27:20 +0200 Subject: [PATCH 09/35] 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] From 74fa87ea73fa678c14e244ce34ddb081ce84ed05 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 9 Jul 2023 19:11:05 +0200 Subject: [PATCH 10/35] list menu wip --- lib/WPMenu.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 67e5826..d101d92 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -73,7 +73,7 @@ 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): + def _getId(self, title, parent): menu = {"id":"", "type":"", "title": "", "parent":"", "children": []} exist = False for i in ["categories", "tags"]: @@ -100,31 +100,31 @@ class WPMenu: soup = BeautifulSoup(content, self._parser) ul = soup.find("ul", id="listsmooth") - menus = list() - menu = {"id":"", "type":"", "title": "", "parent":"", "children": []} + menu = list() parents = list() - children = list() for anchor in ul.find_all("li"): - parent = anchor.find("a").get_text() + + parent = anchor.find("a").get_text().replace(" \xa0", "") + itemMenu = {"id":"", "type":"", "title": parent, "parent": parent, "children":[]} + itemChild = {"id":"", "type":"", "title": parent, "parent": parent} href = anchor.find("a").get("href") - menu = self._child(parent, parent) + #menu = self._child(parent, parent) if href == "#": 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())) - 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} Parent {1} : Child {2}".format(self._name, parent, a.get_text())) + itemChild["title"] = a.get_text() + itemChild["parent"] = parent + itemMenu["children"].append(itemChild) + menu.append(itemMenu) - if menus[i]["title"] == parents[j]: - self._logger.info("{0} del : {1}".format(self._name, parents[j])) - del parents[j] - for i in parents: - self._logger.info("{0} : Parent : {1}".format(self._name, i)) \ No newline at end of file + for i in range(0, len(menu)-1): + self._logger.info("{0} Menu : {1}".format(self._name, menu[i])) + #for j in menu[i]["children"]: + # if menu[i]["title"] == j["title"]: + # del menu[i] + + + \ No newline at end of file From 20c4adb3cff066ac9a70474819f404e854544a96 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 9 Jul 2023 21:42:09 +0200 Subject: [PATCH 11/35] menu and sub-menu wip --- lib/WPMenu.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index d101d92..64bbb71 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -101,30 +101,34 @@ class WPMenu: soup = BeautifulSoup(content, self._parser) ul = soup.find("ul", id="listsmooth") menu = list() - parents = list() + children = list() for anchor in ul.find_all("li"): parent = anchor.find("a").get_text().replace(" \xa0", "") - itemMenu = {"id":"", "type":"", "title": parent, "parent": parent, "children":[]} - itemChild = {"id":"", "type":"", "title": parent, "parent": parent} + itemMenu = {"id":"", "type":"", "title": parent, "children":list()} + href = anchor.find("a").get("href") + if href == self._wordpress: + parent = "home" #menu = self._child(parent, parent) if href == "#": li = anchor.find("ul").find_all("li") for child in li: a = child.find("a") self._logger.info("{0} Parent {1} : Child {2}".format(self._name, parent, a.get_text())) - itemChild["title"] = a.get_text() - itemChild["parent"] = parent - itemMenu["children"].append(itemChild) + children.append({"title": a.get_text(), "parent": parent}) menu.append(itemMenu) - for i in range(0, len(menu)-1): - self._logger.info("{0} Menu : {1}".format(self._name, menu[i])) - #for j in menu[i]["children"]: - # if menu[i]["title"] == j["title"]: - # del menu[i] + for i in range(0, len(children)-1): + self._logger.info("{0} : Child {1}".format(self._name, children[i])) + for j in range(0, len(menu)-1): + if menu[j]["title"] == children[i]["title"]: + self._logger.info("{0} : Parent {1}".format(self._name, menu[j])) + del menu[j] + for j in range(0, len(menu)-1): + if menu[j]["title"] == children[i]["parent"]: + menu[j]["children"].append({"id":"", "title":children[i]["title"], "parent": children[i]["parent"]}) - - \ No newline at end of file + for i in menu: + self._logger.info("{0} : Menu {1}".format(self._name)) \ No newline at end of file From 5059a15826a18ce0a4041e10164ef6b9eb39db64 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 11 Jul 2023 00:17:24 +0200 Subject: [PATCH 12/35] menu and submenu --- lib/WPMenu.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 64bbb71..c87e585 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -56,6 +56,7 @@ class WPMenu: ## Get from URL def fromUrl(self, canalblog): + self._canalblog = canalblog try: o = urlparse(canalblog) o = o._replace(scheme=self._protocol_canalblog) @@ -105,11 +106,12 @@ class WPMenu: for anchor in ul.find_all("li"): parent = anchor.find("a").get_text().replace(" \xa0", "") + href = anchor.find("a").get("href") + if href == "{0}://{1}/".format(self._protocol_canalblog, self._canalblog): + parent = "home" itemMenu = {"id":"", "type":"", "title": parent, "children":list()} - href = anchor.find("a").get("href") - if href == self._wordpress: - parent = "home" + #menu = self._child(parent, parent) if href == "#": li = anchor.find("ul").find_all("li") @@ -131,4 +133,4 @@ class WPMenu: menu[j]["children"].append({"id":"", "title":children[i]["title"], "parent": children[i]["parent"]}) for i in menu: - self._logger.info("{0} : Menu {1}".format(self._name)) \ No newline at end of file + self._logger.info("{0} : Menu {1} {2}".format(self._name, i["title"], len(i["children"]))) \ No newline at end of file From bca529f3c30349339bf9908b71921007d53eab88 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 11 Jul 2023 21:39:41 +0200 Subject: [PATCH 13/35] parent and children array --- lib/WPMenu.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index c87e585..fa739d7 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -109,10 +109,7 @@ 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, "children":list()} - - - #menu = self._child(parent, parent) + itemMenu = {"id":"", "type":"", "title": parent, "children":list()} if href == "#": li = anchor.find("ul").find_all("li") for child in li: @@ -122,13 +119,16 @@ class WPMenu: menu.append(itemMenu) - for i in range(0, len(children)-1): - self._logger.info("{0} : Child {1}".format(self._name, children[i])) - for j in range(0, len(menu)-1): - if menu[j]["title"] == children[i]["title"]: - self._logger.info("{0} : Parent {1}".format(self._name, menu[j])) - del menu[j] - for j in range(0, len(menu)-1): + for i in range(0, len(children)): + self._logger.info("{0} : Child {1} {2}".format(self._name, children[i], i)) + for j in range(0, len(menu)): + if j < len(menu): + if menu[j]["title"] == children[i]["title"]: + self._logger.info("{0} : Parent {1} {2}".format(self._name, menu[j], j)) + del menu[j] + + 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":"", "title":children[i]["title"], "parent": children[i]["parent"]}) From dded126926d4380906df8c4cfe7000dde9940a8f Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 11 Jul 2023 21:42:50 +0200 Subject: [PATCH 14/35] change return getId --- lib/WPMenu.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index fa739d7..5883808 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -74,8 +74,8 @@ class WPMenu: except Exception as err: self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, canalblog, err)) - def _getId(self, title, parent): - menu = {"id":"", "type":"", "title": "", "parent":"", "children": []} + def _getId(self, title): + idMenu = 0 exist = False for i in ["categories", "tags"]: if exist is False: @@ -85,7 +85,7 @@ class WPMenu: if page.status_code == 200: result = page.json() if len(result) > 0: - menu = {"id":result[0]["id"], "type":i, "title": title, "parent":parent, "children": []} + idMenu = result[0]["id"] exist = True else: self._logger.error("{0} : {2} didn't get due status code : {1}".format(self._name, page.status_code, i)) @@ -95,7 +95,7 @@ class WPMenu: 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 + return idMenu def _menu(self, content): @@ -132,5 +132,6 @@ class WPMenu: if menu[j]["title"] == children[i]["parent"]: menu[j]["children"].append({"id":"", "title":children[i]["title"], "parent": children[i]["parent"]}) - for i in menu: - self._logger.info("{0} : Menu {1} {2}".format(self._name, i["title"], len(i["children"]))) \ No newline at end of file + for i in range(0, len(menu)): + self._logger.info("{0} : Menu {1} {2}".format(self._name, menu[i]["title"], len(menu[i]["children"]))) + \ No newline at end of file From e6328135da14e53c645a515889c227381f598010 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 13 Jul 2023 00:09:18 +0200 Subject: [PATCH 15/35] get id --- lib/WPMenu.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 5883808..33178a9 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -134,4 +134,17 @@ class WPMenu: for i in range(0, len(menu)): self._logger.info("{0} : Menu {1} {2}".format(self._name, menu[i]["title"], len(menu[i]["children"]))) + if menu[i]["title"] != "home": + for j in range(0, len(menu[i]["children"])): + idMenu = self._getId(menu[i]["children"][j]["title"]) + if idMenu != 0: + menu[i]["children"][j] = {"id":idMenu, "title": menu[i]["children"][j]["title"], "parent": menu[i]["children"][j]["parent"]} + idMenu = self._getId(menu[i]["title"]) + if idMenu != 0: + menu[i] = {"id":idMenu, "title":menu[i]["title"], "children": menu[i]["children"]} + + for i in menu: + self._logger.info("{0} Menu parent {1} with id : {2}".format(self._name, i["title"], i["id"])) + for j in i["children"]: + self._logger.info("{0} Menu children {1} with id : {2}".format(self._name, j["title"], j["id"])) \ No newline at end of file From be9ad9b934522c0a9647b7a13a45e15555054ec4 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 13 Jul 2023 00:25:14 +0200 Subject: [PATCH 16/35] swap loop --- lib/WPMenu.py | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 33178a9..a60fd44 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -78,23 +78,28 @@ class WPMenu: idMenu = 0 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: - idMenu = result[0]["id"] - 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)) + for index in range(1,10): + if exist is False: + try: + params = {"search":title, "per_page":"100", "page":index} + 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: + for j in result: + self._logger.info("{0} info : {1} {2}".format(self._name, j["name"], title)) + if j["name"] == title: + idMenu = j["id"] + 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 idMenu def _menu(self, content): From 0445054dc8cc849a779da24317a627496044d2a8 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 13 Jul 2023 22:35:38 +0200 Subject: [PATCH 17/35] fix get id menu --- lib/WPMenu.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index a60fd44..fc7d8dd 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -82,13 +82,15 @@ class WPMenu: if exist is False: try: params = {"search":title, "per_page":"100", "page":index} + self._logger.info("{0} Get menu {1} {2} {3}".format(self._name, "{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), index, title)) 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: for j in result: - self._logger.info("{0} info : {1} {2}".format(self._name, j["name"], title)) - if j["name"] == title: + 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 = j["id"] exist = True @@ -114,13 +116,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, "children":list()} + itemMenu = {"id":"", "type":"", "title": parent, "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}) + children.append({"title": a.get_text(), "parent": parent, "href":href}) menu.append(itemMenu) @@ -142,6 +145,15 @@ class WPMenu: if menu[i]["title"] != "home": for j in range(0, len(menu[i]["children"])): idMenu = self._getId(menu[i]["children"][j]["title"]) + if idMenu == 0: + title = href[::-1] + link = title.split("/")[0] + link = link[::-1] + title = title.split("/")[1] + title = title[::-1] + self._logger.info("{0} link {1} title {2}".format(self._name, link, title)) + if link == "index.html": + idMenu = self._getId(title) if idMenu != 0: menu[i]["children"][j] = {"id":idMenu, "title": menu[i]["children"][j]["title"], "parent": menu[i]["children"][j]["parent"]} idMenu = self._getId(menu[i]["title"]) From 5fe4b1f7869f4c230026c914262721f192f5fdc8 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 13 Jul 2023 23:53:54 +0200 Subject: [PATCH 18/35] add href --- lib/WPMenu.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index fc7d8dd..aae2864 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -72,7 +72,25 @@ class WPMenu: self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, canalblog, err)) exit(1) except Exception as err: - self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, canalblog, err)) + self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, canalblog, err)) + + def _getIdFromPost(self, href): + idMenu = 0 + return idMenu + + def _getIdFromReverse(self, title, href): + self._logger.info("{0} : get title {1} from href {2}".format(self._name, title, href)) + idMenu = 0 + if href != "#": + title = href[::-1] + link = title.split("/")[0] + link = link[::-1] + title = title.split("/")[1] + title = title[::-1] + self._logger.info("{0} link {1} title {2}".format(self._name, link, title)) + if link == "index.html": + idMenu = self._getId(title) + return idMenu def _getId(self, title): idMenu = 0 @@ -138,7 +156,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":"", "title":children[i]["title"], "parent": children[i]["parent"]}) + menu[j]["children"].append({"id":"", "title":children[i]["title"], "parent": children[i]["parent"], "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"]))) @@ -146,17 +164,14 @@ class WPMenu: for j in range(0, len(menu[i]["children"])): idMenu = self._getId(menu[i]["children"][j]["title"]) if idMenu == 0: - title = href[::-1] - link = title.split("/")[0] - link = link[::-1] - title = title.split("/")[1] - title = title[::-1] - self._logger.info("{0} link {1} title {2}".format(self._name, link, title)) - if link == "index.html": - idMenu = self._getId(title) + 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 != 0: menu[i]["children"][j] = {"id":idMenu, "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])) + if idMenu == 0: + idMenu = self._getIdFromReverse(menu[i]["title"], menu[i]["href"]) if idMenu != 0: menu[i] = {"id":idMenu, "title":menu[i]["title"], "children": menu[i]["children"]} From 7593b68b6c8a27902a535555349bd9223a274267 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 15 Jul 2023 11:51:15 +0200 Subject: [PATCH 19/35] getting post title --- lib/WPMenu.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index aae2864..ef7eb6a 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -74,10 +74,38 @@ class WPMenu: except Exception as err: self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, canalblog, err)) + def _getIdfromTitlePost(self, content): + idMenu = 0 + + return idMenu + def _getIdFromPost(self, href): idMenu = 0 + o = urlparse(href) + if len(o.netloc) > 0: + try: + page = self._request_canalblog.get(href) + if page.status_code == 200: + self._logger.info("{0} : Get content : {1}".format(self._name, href)) + self._getIdfromTitlePost(page.content) + 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, href, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, href, err)) + else: + if os.path.exists("{0}/..{1}".format(self._directory, o.path)): + try: + content = open("{0}/..{1}".format(self._directory, o.path), "r") + self._getIdfromTitlePost(content) + except Exception as err: + self._logger.error("{0} : Exception error for get file content {1} : {2}".format(self._name, href, err)) return idMenu + def _getIdFromReverse(self, title, href): self._logger.info("{0} : get title {1} from href {2}".format(self._name, title, href)) idMenu = 0 From 82f9acd658f1c78bd1436851a2928cf362334eac Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 15 Jul 2023 13:55:01 +0200 Subject: [PATCH 20/35] get title from post --- lib/WPMenu.py | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index ef7eb6a..ba9ef99 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -74,9 +74,44 @@ class WPMenu: except Exception as err: self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, canalblog, err)) + ## replace caracter + + def _replaceCaracter(self, title_rendered): + list_replace = {'’': "'", '–': '-', '…': '...', '« ': '"', ' »': '"', '« ': '"', ' »': '"', '’': "'", '"‘': "'"} + for old, new in list_replace.items(): + title_rendered = title_rendered.replace(old, new) + return title_rendered + def _getIdfromTitlePost(self, content): idMenu = 0 - + soup = BeautifulSoup(content, self._parser) + articletitle = soup.find("h2", class_="articletitle") + for index in range(0,10): + params = {"search":articletitle, "per_page":"100", "page":index} + try: + 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() + self._logger.info("{0} : Get content post : {1}".format(self._name, len(result))) + for i in result: + title_rendered = i["name"] + if len(articletitle) != len(title_rendered): + title_rendered = self._replaceCaracter(title_rendered) + + if articletitle == title_rendered: + idMenu = i["id"] + + elif page.status_code == 400: + break + else: + self._logger.error("{0} : Post didn't get due status code : {1}".format(self._name, page.status_code)) + 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, href, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, href, err)) + return idMenu def _getIdFromPost(self, href): @@ -118,6 +153,7 @@ class WPMenu: self._logger.info("{0} link {1} title {2}".format(self._name, link, title)) if link == "index.html": idMenu = self._getId(title) + return idMenu def _getId(self, title): @@ -139,7 +175,8 @@ class WPMenu: self._logger.info("{0} : comparaison ok : {1} {2}".format(self._name, j["id"], i)) idMenu = j["id"] exist = True - + elif page.status_code == 400: + break 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)) From aa8ac9cfcbf24a0b46cafa3ae24e7e75acd7ace5 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 15 Jul 2023 14:00:23 +0200 Subject: [PATCH 21/35] first test id from post --- lib/WPMenu.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index ba9ef99..15aff40 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -153,6 +153,8 @@ class WPMenu: self._logger.info("{0} link {1} title {2}".format(self._name, link, title)) if link == "index.html": idMenu = self._getId(title) + else: + idMenu = self._getIdFromPost(href) return idMenu From d5ddf1601b7e3376df3e42ccb170462263222845 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 17 Jul 2023 23:20:47 +0200 Subject: [PATCH 22/35] add post fine --- lib/WPMenu.py | 104 +++++++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 47 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 15aff40..174827d 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -83,48 +83,56 @@ class WPMenu: return title_rendered def _getIdfromTitlePost(self, content): - idMenu = 0 + idMenu = {"id":0, "type":""} soup = BeautifulSoup(content, self._parser) - articletitle = soup.find("h2", class_="articletitle") - for index in range(0,10): - params = {"search":articletitle, "per_page":"100", "page":index} - try: - 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() - self._logger.info("{0} : Get content post : {1}".format(self._name, len(result))) - for i in result: - title_rendered = i["name"] - if len(articletitle) != len(title_rendered): - title_rendered = self._replaceCaracter(title_rendered) - - if articletitle == title_rendered: - idMenu = i["id"] - - elif page.status_code == 400: - break - else: - self._logger.error("{0} : Post didn't get due status code : {1}".format(self._name, page.status_code)) - 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, href, err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, href, err)) - + articletitle = soup.find("h2", class_="articletitle").get_text() + exist = False + for index in range(1,10): + if exist is False: + params = {"search":articletitle, "per_page":100, "page":index} + try: + self._logger.debug("{0} : Get Url for post : {1} {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/posts".format(self._wordpress, self._protocol_wordpress), params)) + page = self._request_wordpress.get("{1}://{0}/wp-json/wp/v2/posts".format(self._wordpress, self._protocol_wordpress), auth=self._basic, params=params) + if page.status_code == 200: + result = page.json() + self._logger.info("{0} : Get content post : {1}".format(self._name, len(result))) + if len(result) > 0: + for i in result: + title_rendered = i["title"]["rendered"] + if len(articletitle) != len(title_rendered): + title_rendered = self._replaceCaracter(title_rendered) + 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"} + exist = True + else: + self._logger.debug("{0} : {2} {1}".format(self._name, result, len(result))) + break + elif page.status_code == 400: + self._logger.debug("{0} : {2} {1}".format(self._name, page.content, page.status_code)) + break + else: + self._logger.error("{0} : Post didn't get due status code : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for get content : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get content : {1} ".format(self._name, err)) return idMenu def _getIdFromPost(self, href): - idMenu = 0 + idMenu = {"id":0, "type":""} o = urlparse(href) if len(o.netloc) > 0: try: page = self._request_canalblog.get(href) if page.status_code == 200: self._logger.info("{0} : Get content : {1}".format(self._name, href)) - self._getIdfromTitlePost(page.content) + idMenu = self._getIdfromTitlePost(page.content) else: - self._logger.error("{0} : {2} didn't get due status code : {1}".format(self._name, page.status_code, i)) + self._logger.error("{0} : {2} didn't get due status code : {1}".format(self._name, page.status_code, href)) 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, href, err)) @@ -135,7 +143,7 @@ class WPMenu: if os.path.exists("{0}/..{1}".format(self._directory, o.path)): try: content = open("{0}/..{1}".format(self._directory, o.path), "r") - self._getIdfromTitlePost(content) + idMenu = self._getIdfromTitlePost(content) except Exception as err: self._logger.error("{0} : Exception error for get file content {1} : {2}".format(self._name, href, err)) return idMenu @@ -143,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 = 0 + idMenu = {"id":0, "type":""} if href != "#": title = href[::-1] link = title.split("/")[0] @@ -159,11 +167,11 @@ class WPMenu: return idMenu def _getId(self, title): - idMenu = 0 + idMenu = {"id": 0, "type":""} exist = False - for i in ["categories", "tags"]: - for index in range(1,10): - if exist is False: + if exist is False: + for i in ["categories", "tags"]: + for index in range(1,10): try: params = {"search":title, "per_page":"100", "page":index} self._logger.info("{0} Get menu {1} {2} {3}".format(self._name, "{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), index, title)) @@ -175,8 +183,10 @@ 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 = j["id"] + idMenu = {"id": j["id"], "type": i} exist = True + else: + break elif page.status_code == 400: break else: @@ -223,27 +233,27 @@ 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":"", "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"], "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"]))) if menu[i]["title"] != "home": for j in range(0, len(menu[i]["children"])): idMenu = self._getId(menu[i]["children"][j]["title"]) - if idMenu == 0: + if idMenu["id"] == 0: 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 != 0: - menu[i]["children"][j] = {"id":idMenu, "title": menu[i]["children"][j]["title"], "parent": menu[i]["children"][j]["parent"]} + 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"]} idMenu = self._getId(menu[i]["title"]) self._logger.debug("{0} : content parent {1}".format(self._name, menu[i])) - if idMenu == 0: + if idMenu["id"] == 0: idMenu = self._getIdFromReverse(menu[i]["title"], menu[i]["href"]) - if idMenu != 0: - menu[i] = {"id":idMenu, "title":menu[i]["title"], "children": menu[i]["children"]} + if idMenu["id"] != 0: + menu[i] = {"id":idMenu["id"], "type": idMenu["type"], "title":menu[i]["title"], "children": menu[i]["children"]} for i in menu: - self._logger.info("{0} Menu parent {1} with id : {2}".format(self._name, i["title"], i["id"])) + self._logger.info("{0} Menu parent {1} with id {2} and type {3}".format(self._name, i["title"], i["id"], i["type"])) for j in i["children"]: - self._logger.info("{0} Menu children {1} with id : {2}".format(self._name, j["title"], j["id"])) + self._logger.info("{0} Menu children {1} with id {2} and type {3}".format(self._name, j["title"], j["id"], j["type"])) \ No newline at end of file From c6894648d15e94c9370745564cfd8f2f36f1b6b3 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 18 Jul 2023 22:09:20 +0200 Subject: [PATCH 23/35] 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 From 1f8ea70b40f68d3248b45bdc3a0a3b24cdc11f44 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 20 Jul 2023 00:04:11 +0200 Subject: [PATCH 24/35] define content for navigation --- lib/WPMenu.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 944d2f4..c61cee5 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -171,6 +171,9 @@ class WPMenu: exist = False if exist is False: for i in ["categories", "tags"]: + typeId = "category" + if i == "tags": + typeId = "tag" for index in range(1,10): try: params = {"search":title, "per_page":"100", "page":index} @@ -183,7 +186,8 @@ 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, "link": j["link"]} + + idMenu = {"id": j["id"], "type": typeId, "link": j["link"]} exist = True else: break @@ -254,8 +258,18 @@ class WPMenu: if idMenu["id"] != 0: menu[i] = {"id":idMenu["id"], "type": idMenu["type"], "title":menu[i]["title"], "link":idMenu["link"], "children": menu[i]["children"]} + navigation = '' for i in menu: 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"])) + if len(i["children"]) > 0: + navigation = navigation + " \n\n".replace("label-string", i["title"]).replace("id-string", str(i["id"])).replace("url-string", i["link"]).replace("kind-string", i["type"]) + else: + navigation = navigation + " \n".replace("label-string", i["title"]).replace("id-string", str(i["id"])).replace("url-string", i["link"]).replace("kind-string", i["type"]) for j in i["children"]: + navigation = navigation + " \n".replace("label-string", j["title"]).replace("id-string", str(j["id"])).replace("url-string", j["link"]).replace("kind-string", 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"])) + if len(i["children"]) > 0: + navigation = navigation + " \n" + self._logger.info("{0} : {1}".format(self._name, navigation)) \ No newline at end of file From 72fbe0a364ca050f3ae4bf433c7c65f4278eed76 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 22 Jul 2023 20:23:26 +0200 Subject: [PATCH 25/35] add menu WIP debug --- lib/WPMenu.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index c61cee5..b82ae93 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -272,4 +272,44 @@ class WPMenu: if len(i["children"]) > 0: navigation = navigation + " \n" self._logger.info("{0} : {1}".format(self._name, navigation)) - \ No newline at end of file + + self._navigation(navigation) + + + def _navigation(self, content): + self._logger.info("{0} Get ID navigation from wordpress for {1}".format(self._name, self._wordpress)) + try: + title = "Menu {0}".format(self._wordpress) + exist = False + page = self._request_wordpress.get("{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), auth=self._basic) + if page.status_code == 200: + result = page.json() + self._logger.info("{0} : Get content navigation : {1}".format(self._name, len(result))) + for i in result: + if i["title"]["rendered"] == title: + exist = True + self._logger.info("{0} : {1} is exist".format(self._name, title)) + if exist is False: + try: + self._logger.info("{0} Create navigation to wordpress for {1}".format(self._name, self._wordpress)) + data = {"title": "Menu {0}".format(self._wordpress), "content": content} + page = self._request_wordpress.post("{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + if page.status_code == 201: + self._logger.info("{0} : Navigation Menu {1} is created".format(self._name, self._wordpress)) + else: + self._logger.error("{0} : Post navigation didn't get due status code : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for post naviguation url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for post navigation url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) + + else: + self._logger.error("{0} : Get navigation didn't get due status code : {1}".format(self._name, page.status_code)) + 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, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) \ No newline at end of file From 941776a7c1d8e2b4538bc187645c7a81fd536d9f Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 24 Jul 2023 22:43:52 +0200 Subject: [PATCH 26/35] Create menu --- lib/WPMenu.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index b82ae93..1345b67 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -257,7 +257,9 @@ class WPMenu: 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"], "link":idMenu["link"], "children": menu[i]["children"]} - + + self._createMenu(menu) + exit(0) navigation = '' for i in menu: 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"])) @@ -286,16 +288,19 @@ class WPMenu: result = page.json() self._logger.info("{0} : Get content navigation : {1}".format(self._name, len(result))) for i in result: + self._logger.debug("{0} : Debug get item navigation : {1}".format(self._name, i)) if i["title"]["rendered"] == title: exist = True self._logger.info("{0} : {1} is exist".format(self._name, title)) if exist is False: try: self._logger.info("{0} Create navigation to wordpress for {1}".format(self._name, self._wordpress)) - data = {"title": "Menu {0}".format(self._wordpress), "content": content} + data = {"title": title, "content": content} page = self._request_wordpress.post("{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) if page.status_code == 201: self._logger.info("{0} : Navigation Menu {1} is created".format(self._name, self._wordpress)) + self._logger.debug("{0} : Navigation Menu {1} create debug : ".format(self._name, page.content)) + else: self._logger.error("{0} : Post navigation didn't get due status code : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content)) @@ -312,4 +317,52 @@ class WPMenu: self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) exit(1) except Exception as err: - self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) \ No newline at end of file + self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) + + def _createMenu(self, menu): + title = "Menu {0}".format(self._wordpress) + self._logger.info("{0} : Create menu from API Wordpress : {1}".format(self._name, title)) + try: + params = {"search": title} + page = self._request_wordpress.get("{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), auth=self._basic, params=params) + if page.status_code == 200: + result = page.json() + self._logger.info("{0} : Get content menus : {1}".format(self._name, len(result))) + idMenu = 0 + if len(result) == 0: + + self._logger.info("{0} : Create menu : {1}".format(self._name, title)) + data = {"name": title} + try: + page = self._request_wordpress.post("{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + if page.status_code == 201: + result = page.json() + self._logger.debug("{0} : Get menus : {1}".format(self._name, result)) + + if len(result) > 0: + idMenu = result["id"] + else: + self._logger.error("{0} : Post menu didn't get due status code : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + + except ConnectionError as err: + self._logger.error("{0} : Connection error for create menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err)) + else: + self._logger.debug("{0} : Get menus : {1}".format(self._name, result)) + for i in result: + self._logger.debug("{0} : List menus : {1}".format(self._name, i)) + if i["name"] == title: + idMenu = i["id"] + self._logger.info("{0} : Get ID menus : {1}".format(self._name, idMenu)) + else: + self._logger.error("{0} : Get menu didn't get due status code : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + + except ConnectionError as err: + self._logger.error("{0} : Connection error for get menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err)) From e17cace82072642032443571c03bc9658f0ffd17 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 24 Jul 2023 22:51:40 +0200 Subject: [PATCH 27/35] remove navigation --- lib/WPMenu.py | 60 +-------------------------------------------------- 1 file changed, 1 insertion(+), 59 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 1345b67..0330641 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -260,65 +260,7 @@ class WPMenu: self._createMenu(menu) exit(0) - navigation = '' - for i in menu: - 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"])) - if len(i["children"]) > 0: - navigation = navigation + " \n\n".replace("label-string", i["title"]).replace("id-string", str(i["id"])).replace("url-string", i["link"]).replace("kind-string", i["type"]) - else: - navigation = navigation + " \n".replace("label-string", i["title"]).replace("id-string", str(i["id"])).replace("url-string", i["link"]).replace("kind-string", i["type"]) - for j in i["children"]: - navigation = navigation + " \n".replace("label-string", j["title"]).replace("id-string", str(j["id"])).replace("url-string", j["link"]).replace("kind-string", 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"])) - if len(i["children"]) > 0: - navigation = navigation + " \n" - self._logger.info("{0} : {1}".format(self._name, navigation)) - - self._navigation(navigation) - - - def _navigation(self, content): - self._logger.info("{0} Get ID navigation from wordpress for {1}".format(self._name, self._wordpress)) - try: - title = "Menu {0}".format(self._wordpress) - exist = False - page = self._request_wordpress.get("{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), auth=self._basic) - if page.status_code == 200: - result = page.json() - self._logger.info("{0} : Get content navigation : {1}".format(self._name, len(result))) - for i in result: - self._logger.debug("{0} : Debug get item navigation : {1}".format(self._name, i)) - if i["title"]["rendered"] == title: - exist = True - self._logger.info("{0} : {1} is exist".format(self._name, title)) - if exist is False: - try: - self._logger.info("{0} Create navigation to wordpress for {1}".format(self._name, self._wordpress)) - data = {"title": title, "content": content} - page = self._request_wordpress.post("{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) - if page.status_code == 201: - self._logger.info("{0} : Navigation Menu {1} is created".format(self._name, self._wordpress)) - self._logger.debug("{0} : Navigation Menu {1} create debug : ".format(self._name, page.content)) - - else: - self._logger.error("{0} : Post navigation didn't get due status code : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - except ConnectionError as err: - self._logger.error("{0} : Connection error for post naviguation url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for post navigation url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) - - else: - self._logger.error("{0} : Get navigation didn't get due status code : {1}".format(self._name, page.status_code)) - 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, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err)) - + def _createMenu(self, menu): title = "Menu {0}".format(self._wordpress) self._logger.info("{0} : Create menu from API Wordpress : {1}".format(self._name, title)) From cf2c1aa617abc8ce45257c291f9934cc342c1130 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 24 Jul 2023 23:32:20 +0200 Subject: [PATCH 28/35] add item menu --- lib/WPMenu.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 0330641..9e64dbf 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -259,8 +259,42 @@ class WPMenu: menu[i] = {"id":idMenu["id"], "type": idMenu["type"], "title":menu[i]["title"], "link":idMenu["link"], "children": menu[i]["children"]} self._createMenu(menu) + + + exit(0) - + + def _createItemMenu(self, idMenu, itemMenu, parent): + idItemMenu = 0 + self._logger.info("{0} : Create item menu from API Wordpress : {1}".format(self._name, self._wordpress)) + try: + params = {"search": itemMenu["title"], "menus": idMenu} + page = self._request_wordpress.get("{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), auth=self._basic, params=params) + if page.status_code == 200: + result = page.json() + for i in result: + if i["title"]["rendered"] == itemMenu["title"]: + idItemMenu = i["id"] + self._logger.info("{0} : Length of result for item menus : {1}".format(self._name, len(result))) + if idItemMenu == 0: + try: + data = {"title": itemMenu["title"], "status": "publish", "parent":parent} + page = self._request_wordpress.post("{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for create item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) + else: + self._logger.error("{0} : Get menu items didn't get due status code : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for get item menus {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get item menus {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) + return idItemMenu + def _createMenu(self, menu): title = "Menu {0}".format(self._wordpress) self._logger.info("{0} : Create menu from API Wordpress : {1}".format(self._name, title)) From eee14e08f1981762be513204b63c101927791c85 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 24 Jul 2023 23:51:41 +0200 Subject: [PATCH 29/35] add update menu item --- lib/WPMenu.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 9e64dbf..841a618 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -276,15 +276,29 @@ class WPMenu: if i["title"]["rendered"] == itemMenu["title"]: idItemMenu = i["id"] self._logger.info("{0} : Length of result for item menus : {1}".format(self._name, len(result))) - if idItemMenu == 0: - try: - data = {"title": itemMenu["title"], "status": "publish", "parent":parent} - page = self._request_wordpress.post("{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) - except ConnectionError as err: - self._logger.error("{0} : Connection error for create item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for get item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) + url = "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress) + if idItemMenu != 0: + url = "{1}://{0}/wp-json/wp/v2/menu-items/{2}".format(self._wordpress, self._protocol_wordpress, idItemMenu) + + try: + objectt = itemMenu["type"] + if objectt == "tag": + objectt = "post_tag" + data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "menus":idMenu, "object":objectt, "object_id":itemMenu["id"]} + page = self._request_wordpress.post(url, auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + if page.status_code == 201: + self._logger.info("{0} : create item menu : {1}".format(self._name, itemMenu["title"])) + elif page.status_code == 200: + self._logger.info("{0} : update item menu : {1}".format(self._name, itemMenu["title"])) + else: + self._logger.error("{0} : Create menu items didn't get due status code : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + + except ConnectionError as err: + self._logger.error("{0} : Connection error for create item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) else: self._logger.error("{0} : Get menu items didn't get due status code : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content)) From 8de74857750079944ea46c05e968b954991f82e1 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 26 Jul 2023 19:18:18 +0200 Subject: [PATCH 30/35] return value menu --- lib/WPMenu.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 841a618..c3f92c9 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -262,7 +262,6 @@ class WPMenu: - exit(0) def _createItemMenu(self, idMenu, itemMenu, parent): idItemMenu = 0 @@ -285,10 +284,17 @@ class WPMenu: if objectt == "tag": objectt = "post_tag" data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "menus":idMenu, "object":objectt, "object_id":itemMenu["id"]} - page = self._request_wordpress.post(url, auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + page = self._request_wordpress.post(url, auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + if page.status_code == 201: + result = page.json() + idItemMenu = result["id"] self._logger.info("{0} : create item menu : {1}".format(self._name, itemMenu["title"])) elif page.status_code == 200: + result = page.json() + for i in result: + if result["title"]["rendered"] == itemMenu["title"]: + idItemMenu = i["id"] self._logger.info("{0} : update item menu : {1}".format(self._name, itemMenu["title"])) else: self._logger.error("{0} : Create menu items didn't get due status code : {1}".format(self._name, page.status_code)) @@ -356,3 +362,8 @@ class WPMenu: exit(1) except Exception as err: self._logger.error("{0} : Exception error for get menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err)) + + + def addItemMenu(self, menu, idMenu): + self._logger.info("{0} : add item to menu : {1}".format(self._name, idMenu)) + \ No newline at end of file From b7493206a2f391f030643f6a533a852c5e133aa4 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 26 Jul 2023 19:22:43 +0200 Subject: [PATCH 31/35] add private method addItemMenu --- lib/WPMenu.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index c3f92c9..d1f349a 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -366,4 +366,8 @@ class WPMenu: def addItemMenu(self, menu, idMenu): self._logger.info("{0} : add item to menu : {1}".format(self._name, idMenu)) - \ No newline at end of file + for i in menu: + parent = self._createItemMenu(idMenu, i, parent) + for j in menu["children"]: + self._createItemMenu(idMenu, i, parent) + \ No newline at end of file From 5399b12133712f6fadad5ae416a1dd747ce8da6b Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 27 Jul 2023 00:39:15 +0200 Subject: [PATCH 32/35] menu wordpress 75% --- lib/WPMenu.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index d1f349a..e8889de 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -273,7 +273,7 @@ class WPMenu: result = page.json() for i in result: if i["title"]["rendered"] == itemMenu["title"]: - idItemMenu = i["id"] + idItemMenu = int(i["id"]) self._logger.info("{0} : Length of result for item menus : {1}".format(self._name, len(result))) url = "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress) if idItemMenu != 0: @@ -283,28 +283,28 @@ class WPMenu: objectt = itemMenu["type"] if objectt == "tag": objectt = "post_tag" - data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "menus":idMenu, "object":objectt, "object_id":itemMenu["id"]} + data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "menus":idMenu, "url":"{0}://{1}".format(self._protocol_wordpress, self._wordpress)} + if type(itemMenu["id"]) is str: + if len(itemMenu["id"]) > 0: + data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "url": itemMenu["link"], "menus":idMenu, "object":objectt, "object_id":int(itemMenu["id"])} + elif type(itemMenu["id"]) is int: + data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "url": itemMenu["link"], "menus":idMenu, "object":objectt, "object_id":itemMenu["id"]} + self._logger.debug("{0} : data for create/update : {1}".format(self._name, data)) page = self._request_wordpress.post(url, auth=self._basic, headers=self._headers_json, data=json.dumps(data)) - if page.status_code == 201: + if page.status_code in [201, 200]: result = page.json() - idItemMenu = result["id"] - self._logger.info("{0} : create item menu : {1}".format(self._name, itemMenu["title"])) - elif page.status_code == 200: - result = page.json() - for i in result: - if result["title"]["rendered"] == itemMenu["title"]: - idItemMenu = i["id"] - self._logger.info("{0} : update item menu : {1}".format(self._name, itemMenu["title"])) + idItemMenu = int(result["id"]) + self._logger.info("{0} : create/update item menu : {1}".format(self._name, itemMenu["title"])) else: - self._logger.error("{0} : Create menu items didn't get due status code : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) + self._logger.error("{0} : Create menu items for {2} didn't get due status code : {1}".format(self._name, page.status_code, itemMenu["title"])) + self._logger.debug("{0} : {1} {2}".format(self._name, page.content, itemMenu)) except ConnectionError as err: self._logger.error("{0} : Connection error for create item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) exit(1) except Exception as err: - self._logger.error("{0} : Exception error for get item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) + self._logger.error("{0} : Exception error for create item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) else: self._logger.error("{0} : Get menu items didn't get due status code : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content)) @@ -353,6 +353,7 @@ class WPMenu: if i["name"] == title: idMenu = i["id"] self._logger.info("{0} : Get ID menus : {1}".format(self._name, idMenu)) + self._addItemMenu(menu, idMenu) else: self._logger.error("{0} : Get menu didn't get due status code : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content)) @@ -364,10 +365,11 @@ class WPMenu: self._logger.error("{0} : Exception error for get menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err)) - def addItemMenu(self, menu, idMenu): + def _addItemMenu(self, menu, idMenu): self._logger.info("{0} : add item to menu : {1}".format(self._name, idMenu)) + parent = 0 for i in menu: + self._logger.debug("{0} : debug create item menu : {1}".format(self._name, i)) parent = self._createItemMenu(idMenu, i, parent) - for j in menu["children"]: - self._createItemMenu(idMenu, i, parent) - \ No newline at end of file + for j in i["children"]: + self._createItemMenu(idMenu, j, parent) From 7f800c8f7bac93c020584aaf348321cc7c2ed433 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 31 Jul 2023 23:39:40 +0200 Subject: [PATCH 33/35] add menu 100% work --- lib/WPMenu.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index e8889de..b17be64 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -272,7 +272,7 @@ class WPMenu: if page.status_code == 200: result = page.json() for i in result: - if i["title"]["rendered"] == itemMenu["title"]: + if self._replaceCaracter(i["title"]["rendered"]) == itemMenu["title"]: idItemMenu = int(i["id"]) self._logger.info("{0} : Length of result for item menus : {1}".format(self._name, len(result))) url = "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress) @@ -283,7 +283,9 @@ class WPMenu: objectt = itemMenu["type"] if objectt == "tag": objectt = "post_tag" - data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "menus":idMenu, "url":"{0}://{1}".format(self._protocol_wordpress, self._wordpress)} + data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "menus":idMenu, "url":"#"} + if itemMenu["title"] == "home": + data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "menus":idMenu, "url":"{0}://{1}".format(self._protocol_wordpress, self._wordpress)} if type(itemMenu["id"]) is str: if len(itemMenu["id"]) > 0: data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "url": itemMenu["link"], "menus":idMenu, "object":objectt, "object_id":int(itemMenu["id"])} @@ -369,6 +371,7 @@ class WPMenu: self._logger.info("{0} : add item to menu : {1}".format(self._name, idMenu)) parent = 0 for i in menu: + parent = 0 self._logger.debug("{0} : debug create item menu : {1}".format(self._name, i)) parent = self._createItemMenu(idMenu, i, parent) for j in i["children"]: From 49c15520625d661b9798f015f230e65b0c4acba3 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 31 Jul 2023 23:57:35 +0200 Subject: [PATCH 34/35] add wpmenu --- import_export_canalblog.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/import_export_canalblog.py b/import_export_canalblog.py index 70cccdd..4948b94 100644 --- a/import_export_canalblog.py +++ b/import_export_canalblog.py @@ -263,6 +263,8 @@ if __name__ == '__main__': for i in wordpress: importWp.setUrl(i) importWp.fromFile(files=args.file.split(",")) + menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress) + menuWp.fromFile("{0}/index.html".format(args.file.split(",")[0])) if len(args.directory) > 0: try: with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex: @@ -273,6 +275,9 @@ if __name__ == '__main__': ex.submit(importDirectory, i, int(args.parallel), args.directory, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, args.create, args.update, args.image, args.revert) for i in range(0, int(args.parallel)) ] + wait(wait_for, return_when=ALL_COMPLETED) + menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress) + menuWp.fromFile("{0}/index.html".format(args.directory)) except Exception as err: logger.error("Threading error : {0}".format(err)) if len(args.canalblog) > 0: @@ -285,6 +290,10 @@ if __name__ == '__main__': ex.submit(importUrl, i, int(args.parallel), args.canalblog, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, ssl_canalblog, args.create, args.update, args.image, args.revert, args.tmp) for i in range(0, int(args.parallel)) ] + wait(wait_for, return_when=ALL_COMPLETED) + menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress) + menuWp.fromUrl(args.canalblog) + except Exception as err: From 16552170501c13b7921d91ccbe629dbb9a8d4544 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 4 Aug 2023 23:21:45 +0200 Subject: [PATCH 35/35] add album article --- lib/WPMenu.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index b17be64..bcb34a2 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -86,6 +86,8 @@ class WPMenu: idMenu = {"id":0, "type":"", "link":""} soup = BeautifulSoup(content, self._parser) articletitle = soup.find("h2", class_="articletitle").get_text() + if len(articletitle) > 0: + articletitle = soup.find("h2").get_text() exist = False for index in range(1,10): if exist is False: