From d64aed6240632d915401fd3c9247d9af95fea258 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 18 Apr 2023 00:00:32 +0200 Subject: [PATCH] update error message + add debug --- lib/WPImport.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/WPImport.py b/lib/WPImport.py index dd5de7d..3dd4a75 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -13,6 +13,7 @@ class WPimport: self._wordpress = wordpress self._logger = logger self._parser = parser + self._headers_json = {'Content-Type': 'application/json', 'Accept':'application/json'} self._request = requests.Session() @@ -124,10 +125,9 @@ class WPimport: res = page.json() if len(res) > 0: id_media = res[0]["id"] - headers = {'Content-Type': 'application/json', 'Accept':'application/json'} data = {"featured_media": id_media} try: - r = self._request.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, result[0]["id"]), auth=self._basic, headers=headers, data=json.dumps(data)) + r = self._request.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, result[0]["id"]), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) except Exception as err: self._logger.error("Connection error : {0}".format(err)) exit(1) @@ -181,7 +181,7 @@ class WPimport: self._logger.error("Connection error : {0}".format(err)) exit(1) if r.status_code == 200: - self._logger.info("Image supprimé {0}".format(img_name)) + self._logger.info("Image removed {0}".format(img_name)) else: self._logger.error("Image not removed due status code : {0}".format(r.status_code)) data = page.content @@ -195,7 +195,7 @@ class WPimport: self._logger.error("Connection error : {0}".format(err)) exit(1) if r.status_code == 201: - self._logger.info("Ajout d'image {0}".format(img_name)) + self._logger.info("Image added {0}".format(img_name)) res = r.json() media["id"] = res["id"] media["rendered"] = res["guid"]["rendered"] @@ -231,9 +231,9 @@ class WPimport: self._logger.error("Connection error : {0}".format(err)) exit(1) if page.status_code == 200: - self._logger.info("Commentaire mise à jour pour {0}".format(title)) + self._logger.info("Comment updated for {0}".format(title)) else: - self._logger.error("Comment not updated due status code : {0}".format(page.status_code)) + self._logger.error("Comment not updated for {0} due status code : {1}".format(title, page.status_code)) else: if i["parent_id"] != -1: parent_id = int(i["parent_id"]) @@ -257,9 +257,9 @@ class WPimport: exit(1) self._logger.info(page.status_code) if page.status_code == 201: - self._logger.info("Commentaire ajoute pour {0}".format(title)) + self._logger.info("Comment added for {0}".format(title)) else: - self._logger.error("Comment not added due status code : {0}".format(page.status_code)) + self._logger.error("Comment not added for {0} due status code : {1}".format(title, page.status_code)) else: self._logger.error("Connection error with status code : {0}".format(page.status_code)) @@ -362,8 +362,7 @@ class WPimport: except Exception as err: self._logger.error("Connection error : {0}".format(err)) exit(1) - if page_img.status_code not in [200, 404]: - self._logger.error("Connection error with status code : {0}".format(page_img.status_code)) + if page_img.status_code == 200: media=self._addOrUpdateMedia(href_img, page_img) new_img["id"]=media["id"] @@ -374,6 +373,8 @@ class WPimport: new_img["id"]=media["id"] new_img["new_src"]=media["rendered"] list_img.append(new_img) + if page_img.status_code not in [200, 404]: + self._logger.error("Connection error with status code : {0}".format(page_img.status_code)) comment_post = self._getComment(comment) @@ -395,8 +396,9 @@ class WPimport: listelement[i].append(k["id"]) if element_exist is False: data = {"name": j} + self._logger.debug("URL : {0} with data : {1} and with headers : {2}".format("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i), data, self._headers_json)) try: - page = self._request.post("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i), auth=self._basic, data=data) + page = self._request.post("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i), auth=self._basic, headers=self._headers_json, data=data) except Exception as err: self._logger.error("Connection error : {0}".format(err)) exit(1)