From 269a9e9ccd1f6508a6449da00c0982bb174c6397 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 28 May 2023 22:31:46 +0200 Subject: [PATCH] add replace exception import --- lib/WPImport.py | 459 ++++++++++++++++++++++++++---------------------- 1 file changed, 245 insertions(+), 214 deletions(-) diff --git a/lib/WPImport.py b/lib/WPImport.py index d13fe60..2640c16 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -206,14 +206,16 @@ class WPimport: data = {"post": post_id} try: r = self._request.post("{2}://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, i["id"], self._protocol), auth=self._basic, data=data) - except Exception as err: + if r.status_code == 200: + self._logger.info("{0} : Link image to post {1}".format(self._name, title)) + else: + self._logger.error("{0} Connection error with status code for link image to post : {1}".format(self._name, r.status_code)) + self._logger.debug("{0} : {1}".format(self._name, r.content)) + except ConnectionError as err: self._logger.error("{0} : Connection error for link image to post : {1}".format(self._name, err)) exit(1) - if r.status_code == 200: - self._logger.info("{0} : Link image to post {1}".format(self._name, title)) - else: - self._logger.error("{0} Connection error with status code for link image to post : {1}".format(self._name, r.status_code)) - self._logger.debug("{0} : {1}".format(self._name, r.content)) + except Exception as err: + self._logger.error("{0} : Exception error for link image to post : {1}".format(self._name, err)) ## Add or update img @@ -233,49 +235,57 @@ class WPimport: params = { "search": img_name} try: r = self._request.get("{1}://{0}/wp-json/wp/v2/media".format(self._wordpress, self._protocol), auth=self._basic, params=params) - except Exception as err: + + self._logger.debug("{0} : Search for image {1} and his status code {2}".format(self._name, img_name, r.status_code)) + if r.status_code == 200: + res = r.json() + self._logger.debug("{0} : Number of image in search : {1}".format(self._name, len(res))) + if len(res) > 0: + params = {"force":1} + try: + r = self._request.delete("{2}://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, res[0]["id"], self._protocol), auth=self._basic, params=params) + if r.status_code == 200: + self._logger.info("{0} : Image removed {1}".format(self._name, img_name)) + else: + self._logger.error("{0} : Image {1} not removed due status code : {2}".format(self._name, img_name, r.status_code)) + self._logger.debug("{0} : {1}".format(self._name, r.content)) + except ConnectionError as err: + self._logger.error("{0} Connection error for delete image : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} Exception error for delete image : {1}".format(self._name, err)) + + data = page.content + img_type = "image/{0}".format(img_type_file) + if img_type_file == "jpg": + img_type = "image/jpeg" + headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)} + try: + r = self._request.post("{1}://{0}/wp-json/wp/v2/media".format(self._wordpress, self._protocol), auth=self._basic, headers=headers, data=data) + + if r.status_code == 201: + self._logger.info("{0} : Image added {1}".format(self._name, img_name)) + res = r.json() + media["id"] = res["id"] + media["rendered"] = res["guid"]["rendered"] + else: + self._logger.error("{0} : Image {1}.{2} not added due status code : {3}".format(self._name, img_name, img_type, r.status_code)) + self._logger.debug("{0} : {1}".format(self._name, r.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for add image : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for add image : {1}".format(self._name, err)) + exit(1) + + else: + self._logger.error("{0} : Connection error for search image with status code : {1}".format(self._name, r.status_code)) + self._logger.debug("{0} : {1}".format(self._name, r.content)) + except ConnectionError as err: self._logger.error("{0} : Connection error for search media : {1}".format(self._name, err)) exit(1) - self._logger.debug("{0} : Search for image {1} and his status code {2}".format(self._name, img_name, r.status_code)) - if r.status_code == 200: - res = r.json() - self._logger.debug("{0} : Number of image in search : {1}".format(self._name, len(res))) - if len(res) > 0: - params = {"force":1} - try: - r = self._request.delete("{2}://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, res[0]["id"], self._protocol), auth=self._basic, params=params) - except Exception as err: - self._logger.error("{0} Connection error for delete image : {1}".format(self._name, err)) - exit(1) - if r.status_code == 200: - self._logger.info("{0} : Image removed {1}".format(self._name, img_name)) - else: - self._logger.error("{0} : Image {1} not removed due status code : {2}".format(self._name, img_name, r.status_code)) - self._logger.debug("{0} : {1}".format(self._name, r.content)) - - data = page.content - img_type = "image/{0}".format(img_type_file) - if img_type_file == "jpg": - img_type = "image/jpeg" - headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)} - try: - r = self._request.post("{1}://{0}/wp-json/wp/v2/media".format(self._wordpress, self._protocol), auth=self._basic, headers=headers, data=data) - except Exception as err: - self._logger.error("{0} : Connection error for add image : {1}".format(self._name, err)) - exit(1) - if r.status_code == 201: - self._logger.info("{0} : Image added {1}".format(self._name, img_name)) - res = r.json() - media["id"] = res["id"] - media["rendered"] = res["guid"]["rendered"] - else: - self._logger.error("{0} : Image {1}.{2} not added due status code : {3}".format(self._name, img_name, img_type, r.status_code)) - self._logger.debug("{0} : {1}".format(self._name, r.content)) - - else: - self._logger.error("{0} : Connection error for search image with status code : {1}".format(self._name, r.status_code)) - self._logger.debug("{0} : {1}".format(self._name, r.content)) - + except Exception as err: + self._logger.error("{0} : Exception error for search media : {1}".format(self._name, err)) return media ## Add or update comment @@ -286,28 +296,34 @@ class WPimport: try: params = {"post": post, "author_name":i["author"], "date":i["date"]} page = self._request.get("{1}://{0}/wp-json/wp/v2/comments".format(self._wordpress, self._protocol), auth=self._basic, params=params) - except Exception as err: + + if page.status_code == 200: + result = page.json() + for j in result: + try: + params = {"force":1} + page = self._request.delete("{2}://{0}/wp-json/wp/v2/comments/{1}".format(self._wordpress, j["id"], self._protocol), params=params, auth=self._basic) + + if page.status_code == 200: + self._logger.info("{0} : Comment deleted for {1}".format(self._name, title)) + self._logger.debug("{0} : Comment deleted : {1}".format(self._name, j)) + else: + self._logger.error("{0} : Comment not deleted for {1} due status code : {2}".format(self._name, title, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for delete comment : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for delete comment : {1}".format(self._name, err)) + + else: + self._logger.error("{0} : Comment not listed for {1} due status code : {2}".format(self._name, title, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + except ConnectionError as err: self._logger.error("{0} : Connection error for search comment : {1}".format(self._name, err)) exit(1) - if page.status_code == 200: - result = page.json() - for j in result: - try: - params = {"force":1} - page = self._request.delete("{2}://{0}/wp-json/wp/v2/comments/{1}".format(self._wordpress, j["id"], self._protocol), params=params, auth=self._basic) - except Exception as err: - self._logger.error("{0} : Connection error for delete comment : {1}".format(self._name, err)) - exit(1) - if page.status_code == 200: - self._logger.info("{0} : Comment deleted for {1}".format(self._name, title)) - self._logger.debug("{0} : Comment deleted : {1}".format(self._name, j)) - else: - self._logger.error("{0} : Comment not deleted for {1} due status code : {2}".format(self._name, title, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - - else: - self._logger.error("{0} : Comment not listed for {1} due status code : {2}".format(self._name, title, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) + except Exception as err: + self._logger.error("{0} : Exception error for search comment : {1}".format(self._name, err)) for i in comment: data = {"post": post, "content": i["content"], "date": i["date"], "author_name": i["author"], "status": "approved"} @@ -317,28 +333,35 @@ class WPimport: params = {"post": post, "author_name":comment[parent_id]["author"], "date":comment[parent_id]["date"]} try: page = self._request.get("{1}://{0}/wp-json/wp/v2/comments".format(self._wordpress, self._protocol), auth=self._basic, params=params) - except Exception as err: + + if page.status_code == 200: + result = page.json() + if len(result) > 0: + data["parent"]=result[0]["id"] + else: + self._logger.error("{0} : Connection error for parent comment with 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 parent comment : {1}".format(self._name, err)) exit(1) - if page.status_code == 200: - result = page.json() - if len(result) > 0: - data["parent"]=result[0]["id"] - else: - self._logger.error("{0} : Connection error for parent comment with status code : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) + except Exception as err: + self._logger.error("{0} : Exception error for parent comment : {1}".format(self._name, err)) try: page = self._request.post("{1}://{0}/wp-json/wp/v2/comments".format(self._wordpress, self._protocol), auth=self._basic, data=data) - except Exception as err: + + if page.status_code == 201: + self._logger.info("{0} : Comment added for {1}".format(self._name, title)) + self._logger.debug("{0} : Data : {1}".format(self._name, data)) + else: + self._logger.error("{0} : Comment not added for {1} due status code : {2}".format(self._name, title, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + except ConnectionError as err: self._logger.error("{0} : Connection error for add comment : {1}".format(self._name, err)) exit(1) - if page.status_code == 201: - self._logger.info("{0} : Comment added for {1}".format(self._name, title)) - self._logger.debug("{0} : Data : {1}".format(self._name, data)) - else: - self._logger.error("{0} : Comment not added for {1} due status code : {2}".format(self._name, title, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) + except Exception as err: + self._logger.error("{0} : Exception error for add comment : {1}".format(self._name, err)) + exit(1) ## Check class name @@ -423,30 +446,37 @@ class WPimport: new_img["old_href"]=href_a try: page_img = self._request.get(href_img) - except Exception as err: - self._logger.error("{0} : Connection error for get image : {1}".format(self._name, err)) - exit(1) - if page_img.status_code == 404: - href_img = href_a - try: - page_img = self._request.get(href_a) - except Exception as err: - self._logger.error("{0} : Connection error for get image : {1}".format(self._name, err)) - exit(1) - self._logger.debug("{0} : Status code for image {1} : {2}".format(self._name, href_img, page_img.status_code)) - if page_img.status_code == 200: - media=self._addOrUpdateMedia(href_img, page_img) - new_img["id"]=media["id"] - new_img["new_src"]=media["rendered"] - list_img.append(new_img) - if href_img != href_a: - media=self._addOrUpdateMedia(href_a, page_img) + + if page_img.status_code == 404: + href_img = href_a + try: + page_img = self._request.get(href_a) + except ConnectionError as err: + self._logger.error("{0} : Connection error for get image : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get image : {1}".format(self._name, err)) + exit(1) + self._logger.debug("{0} : Status code for image {1} : {2}".format(self._name, href_img, page_img.status_code)) + if page_img.status_code == 200: + media=self._addOrUpdateMedia(href_img, page_img) 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("{0} : Connection error with status code for get image : {1}".format(self._name, page_img.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page_img.content)) + if href_img != href_a: + media=self._addOrUpdateMedia(href_a, page_img) + 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("{0} : Connection error with status code for get image : {1}".format(self._name, page_img.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page_img.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for get image : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get image : {1}".format(self._name, err)) + exit(1) self._logger.debug("{0} : Number of image : {1}".format(self._name, len(list_img))) comment_post = self._getComment(comment) @@ -469,36 +499,36 @@ class WPimport: try: params = {"search":title_element, "per_page":"100", "page":index} page = self._request.get("{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol), auth=self._basic, params=params) + + if page.status_code == 200: + result = page.json() + self._logger.debug("{0} : content {3} {2} : {1}".format(self._name, result, title_element, i)) + if len(result) > 0: + for k in result: + title_rendered = k["name"] + self._logger.debug("{0} : content {2} : {1}".format(self._name, title_rendered, i)) + self._logger.debug("{0} : size of content {3} : {2} - {1}".format(self._name, len(title_rendered), len(title_element), i)) + if len(title_element) != len(title_rendered): + title_rendered = self._replaceCaracter(title_rendered) + + if title_element == title_rendered: + self._logger.info("{0} : {1} found : {2}".format(self._name, i, title_rendered)) + element_exist = True + listelement[i].append(k["id"]) + else: + break + if page.status_code == 400: + self._logger.error("{0} : {1} not found due status code : {2}".format(self._name, i, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + break + else: + self._logger.error("{0} : {1} not found due status code : {2}".format(self._name, i, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) except ConnectionError as err: self._logger.error("{0} : Connection error for {1} : {2}".format(self._name, i, err)) exit(1) except Exception as err: self._logger.error("{0} : Exception error for {1} : {2}".format(self._name, i, err)) - if page.status_code == 200: - result = page.json() - self._logger.debug("{0} : content {3} {2} : {1}".format(self._name, result, title_element, i)) - if len(result) > 0: - for k in result: - title_rendered = k["name"] - self._logger.debug("{0} : content {2} : {1}".format(self._name, title_rendered, i)) - self._logger.debug("{0} : size of content {3} : {2} - {1}".format(self._name, len(title_rendered), len(title_element), i)) - if len(title_element) != len(title_rendered): - title_rendered = self._replaceCaracter(title_rendered) - - if title_element == title_rendered: - self._logger.info("{0} : {1} found : {2}".format(self._name, i, title_rendered)) - element_exist = True - listelement[i].append(k["id"]) - else: - break - if page.status_code == 400: - self._logger.error("{0} : {1} not found due status code : {2}".format(self._name, i, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - break - else: - self._logger.error("{0} : {1} not found due status code : {2}".format(self._name, i, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - self._logger.debug("{0} : Element {3} {2} is {1}".format(self._name, element_exist, title_element, i)) if element_exist == False: data = {"name": title_element} @@ -506,18 +536,19 @@ class WPimport: self._logger.debug("{0} : Data : {1}".format(self._name, data)) try: page = self._request.post("{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + + if page.status_code == 201: + self._logger.info("{0} : {1} created : {2}".format(self._name, i, j)) + result = page.json() + listelement[i].append(result["id"]) + else: + self._logger.error("{0} : {1} not added due status code : {2}".format(self._name, i, 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 {1} : {2}".format(self._name, i, err)) exit(1) except Exception as err: self._logger.error("{0} : Exception error for post {1} : {2}".format(self._name, i, err)) - if page.status_code == 201: - self._logger.info("{0} : {1} created : {2}".format(self._name, i, j)) - result = page.json() - listelement[i].append(result["id"]) - else: - self._logger.error("{0} : {1} not added due status code : {2}".format(self._name, i, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) title = articletitle[0].text author = articleacreator[0].text.lower() @@ -548,21 +579,19 @@ class WPimport: page = self._request.get("{1}://{0}/wp-json/wp/v2/users".format(self._wordpress, self._protocol), auth=self._basic, headers=self._headers_json, params=params) self._logger.debug("{0} : End Search author : {1}".format(self._name, author)) self._logger.debug("{0} : Debug requests : {1}".format(self._name, page.content)) - - + if page.status_code == 200: + self._logger.info("{0} : Get author id : {1}".format(self._name, result)) + result = page.json() + for a in result: + data["author"] = a["id"] + else: + self._logger.error("{0} : Connection error with status code for get author : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(page.content)) except ConnectionError as err: self._logger.error("{0} : Connection error for get author : {1}".format(self._name, err)) exit(1) except Exception as err: self._logger.error("{0} : Exception error for get author : {1}".format(self._name, err)) - if page.status_code == 200: - self._logger.info("{0} : Get author id : {1}".format(self._name, result)) - result = page.json() - for a in result: - data["author"] = a["id"] - else: - self._logger.error("{0} : Connection error with status code for get author : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(page.content)) page_is_exist = False for index in range(1,10): @@ -570,87 +599,89 @@ class WPimport: try: self._logger.info("{0} : Search post with index {2} : {1}".format(self._name, title, index)) page = self._request.get("{1}://{0}/wp-json/wp/v2/posts".format(self._wordpress, self._protocol), auth=self._basic, params=params, headers=self._headers_json) + if page.status_code == 200: + self._logger.debug("{0} : Encoding : {1}".format(self._name, page.encoding)) + page.encoding = "utf-8" + result = page.json() + if len(result) == 0: + break + self._logger.info("{0} : Number result posts : {1}".format(self._name, len(result))) + count = 0 + for i in result: + title_rendered = i["title"]["rendered"] + self._logger.info("{0} : Search title posts for |{2}| : |{1}|".format(self._name, title_rendered, title)) + if len(title_rendered) != len(title): + title_rendered = self._replaceCaracter(title_rendered) + self._logger.debug("{0} : Search title posts for |{2}| : |{1}|".format(self._name, title_rendered, title)) + self._logger.debug("{0} : SIze of title : {1} - {2}".format(self._name, len(title), len(title_rendered))) + if title_rendered == title: + page_is_exist = True + post_id = i["id"] + count = count + 1 + if count > 1: + self._logger.info("{0} : Page {1} is double and going to delete".format(self._name, title)) + try: + params = {"force":1} + page = self._request.delete("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id, self._protocol), auth=self._basic, headers=self._headers_json, params=params) + if page.status_code == 200: + self._logger.info("{0} : Post deleted : {1}".format(self._name, title)) + else: + self._logger.error("{0} : Post not updated 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 deleted post : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for deleted post : {1}".format(self._name, err)) + + else: + self._logger.debug("{0} : Data for post to update : {1}".format(self._name, i)) + self._logger.info("{0} : Page {1} already exist and going to update".format(self._name, title)) + + try: + page = self._request.post("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id, self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + + if page.status_code == 200: + result = page.json() + self._logger.info("{0} : Post updated : {1}".format(self._name, title)) + self._addOrUpdateComment(result["id"], comment_post, result["title"]["raw"]) + self._linkImgPost(result["title"]["raw"], list_img, result["id"]) + else: + self._logger.error("{0} : Post not updated 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 update post : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for update post : {1}".format(self._name, err)) + if page.status_code == 400: + self._logger.error("{0} : Connection for update post unauthorized : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + break + else: + self._logger.error("{0} : Connection for update post error with 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 search post : {1}".format(self._name, err)) exit(1) except Exception as err: self._logger.error("{0} : Exception error for search post : {1}".format(self._name, err)) - if page.status_code == 200: - self._logger.debug("{0} : Encoding : {1}".format(self._name, page.encoding)) - page.encoding = "utf-8" - result = page.json() - if len(result) == 0: - break - self._logger.info("{0} : Number result posts : {1}".format(self._name, len(result))) - count = 0 - for i in result: - title_rendered = i["title"]["rendered"] - self._logger.info("{0} : Search title posts for |{2}| : |{1}|".format(self._name, title_rendered, title)) - if len(title_rendered) != len(title): - title_rendered = self._replaceCaracter(title_rendered) - self._logger.debug("{0} : Search title posts for |{2}| : |{1}|".format(self._name, title_rendered, title)) - self._logger.debug("{0} : SIze of title : {1} - {2}".format(self._name, len(title), len(title_rendered))) - if title_rendered == title: - page_is_exist = True - post_id = i["id"] - count = count + 1 - if count > 1: - self._logger.info("{0} : Page {1} is double and going to delete".format(self._name, title)) - try: - params = {"force":1} - page = self._request.delete("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id, self._protocol), auth=self._basic, headers=self._headers_json, params=params) - except ConnectionError as err: - self._logger.error("{0} : Connection error for deleted post : {1}".format(self._name, err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for deleted post : {1}".format(self._name, err)) - if page.status_code == 200: - self._logger.info("{0} : Post deleted : {1}".format(self._name, title)) - else: - self._logger.error("{0} : Post not updated due status code : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - - else: - self._logger.debug("{0} : Data for post to update : {1}".format(self._name, i)) - self._logger.info("{0} : Page {1} already exist and going to update".format(self._name, title)) - - try: - page = self._request.post("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id, self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) - except ConnectionError as err: - self._logger.error("{0} : Connection error for update post : {1}".format(self._name, err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for update post : {1}".format(self._name, err)) - if page.status_code == 200: - result = page.json() - self._logger.info("{0} : Post updated : {1}".format(self._name, title)) - self._addOrUpdateComment(result["id"], comment_post, result["title"]["raw"]) - self._linkImgPost(result["title"]["raw"], list_img, result["id"]) - else: - self._logger.error("{0} : Post not updated due status code : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - if page.status_code == 400: - self._logger.error("{0} : Connection for update post unauthorized : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - break - else: - self._logger.error("{0} : Connection for update post error with status code : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) if page_is_exist is False: try: self._logger.info("{0} : Creating posts : {1}".format(self._name, data["title"])) page = self._request.post("{1}://{0}/wp-json/wp/v2/posts".format(self._wordpress, self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + + if page.status_code == 201: + result = page.json() + self._logger.info("{0} : Post added : {1}".format(self._name, result["title"]["raw"])) + self._addOrUpdateComment(result["id"], comment_post, result["title"]["raw"]) + self._linkImgPost(result["title"]["raw"], list_img, result["id"]) + else: + self._logger.error("{0} : Post not added due status code : {1}".format(self._name, r.status_code)) + self._logger.debug("{0} : {1}".format(self._name, r.content)) except ConnectionError as err: self._logger.error("{0} : Connection error for create post : {1}".format(self._name, err)) exit(1) except Exception as err: self._logger.error("{0} : Exception error for create post : {1}".format(self._name, err)) - if page.status_code == 201: - result = page.json() - self._logger.info("{0} : Post added : {1}".format(self._name, result["title"]["raw"])) - self._addOrUpdateComment(result["id"], comment_post, result["title"]["raw"]) - self._linkImgPost(result["title"]["raw"], list_img, result["id"]) - else: - self._logger.error("{0} : Post not added due status code : {1}".format(self._name, r.status_code)) - self._logger.debug("{0} : {1}".format(self._name, r.content))