diff --git a/lib/WPImport.py b/lib/WPImport.py index 3163886..2f1dc61 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -122,6 +122,8 @@ class WPimport: albumdesc = albumbody.find("div", class_="albumdesc").find("p") img_a = albumbody.find_all("img") list_img = [] + page_is_exist = False + if self._no_image is False: self._logger.debug("{0} : Number of image's tag : {1}".format(self._name, len(img_a))) @@ -155,6 +157,7 @@ class WPimport: self._logger.debug("{0} content img : {1}".format(self._name, list_img)) content_html = "" if len(list_img) > 0: + content_html = "

{0}

\n\n".format(albumdesc) @@ -165,6 +168,99 @@ class WPimport: content_html = content_html + "" self._logger.info("{0} : content html : {1}".format(self._name, content_html)) + if len(content_html) > 0: + data = {"title":albumtitle, "content":content_html, "status":"publish"} + + for index in range(1,10): + params = {"search": albumtitle, "per_page":100, "page": index} + try: + self._logger.info("{0} : Search post with index {2} : {1}".format(self._name, albumtitle, index)) + page = self._request.get("{1}://{0}/wp-json/wp/v2/pages".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 pages for |{2}| : |{1}|".format(self._name, title_rendered, albumtitle)) + if len(title_rendered) != len(albumtitle): + title_rendered = self._replaceCaracter(title_rendered) + self._logger.debug("{0} : Search title pages for |{2}| : |{1}|".format(self._name, title_rendered, albumtitle)) + self._logger.debug("{0} : SIze of title : {1} - {2}".format(self._name, len(albumtitle), len(title_rendered))) + if title_rendered == albumtitle: + if self._no_update is False: + 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, albumtitle)) + try: + params = {"force":1} + page = self._request.delete("{2}://{0}/wp-json/wp/v2/pages/{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} : Page deleted : {1}".format(self._name, albumtitle)) + else: + self._logger.error("{0} : Page 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 page : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for deleted page : {1}".format(self._name, err)) + + else: + self._logger.debug("{0} : Data for page to update : {1}".format(self._name, i)) + self._logger.info("{0} : Page {1} already exist and going to update".format(self._name, albumtitle)) + + try: + page = self._request.post("{2}://{0}/wp-json/wp/v2/pages/{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} : page updated : {1}".format(self._name, albumtitle)) + else: + self._logger.error("{0} : page 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 page : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for update page : {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 page 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 page : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for search page : {1}".format(self._name, err)) + + if page_is_exist is False and self._no_create is False: + try: + self._logger.info("{0} : Creating page : {1}".format(self._name, data["title"])) + page = self._request.post("{1}://{0}/wp-json/wp/v2/pages".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} : page added : {1}".format(self._name, result["title"]["raw"])) + + else: + self._logger.error("{0} : page 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 page : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for create page : {1}".format(self._name, err)) +