From 9acb620f93014f07096cb8dfb05c9fbb218bffa3 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 9 Jul 2023 12:29:13 +0200 Subject: [PATCH] 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)