diff --git a/lib/WPImport.py b/lib/WPImport.py index b2af6d8..ba405ee 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -211,7 +211,7 @@ class WPimport: ## Add or update img def _addOrUpdateMedia(self, href_img, page): - media_authorized = ["png", "jpg", "jpeg", "svg"] + media_authorized = ["png", "jpg", "jpeg", "svg", "gif"] media = {"id":"", "rendered":""} split_fileimg = href_img.split("/") img_name = split_fileimg[len(split_fileimg)-1] @@ -455,33 +455,56 @@ class WPimport: for i in liste: for j in element[i]: element_exist = False - try: - params = {"params":j} - page = self._request.get("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i), auth=self._basic, params=params) - except Exception as err: - self._logger.error("{0} : Connection error for {1} : {2}".format(self._name, i, err)) - exit(1) - if page.status_code == 200: - element_exist = True - result = page.json() - listelement[i].append(result[0]["id"]) - - 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)) - - - if element_exist is False: - data = {"name": j} - self._logger.debug("{0} : URL : {1} ".format("http://{1}/wp-json/wp/v2/{2}".format(self._name, self._wordpress, i))) - self._logger.debug("{0} : data : {1}".format(self._name, data)) - self._logger.debug("{0} : headers : {1}".format(self._name, self._headers_form)) + title_element = self._removeSpace(j) + for index in range(1,10): + self._logger.info("{0} : search {1} with index {2} : {3}".format(self._name, i, index, title_element)) try: - page = self._request.post("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i), auth=self._basic, headers=self._headers_json, data=data) + params = {"search":title_element, "per_page":"100", "page":index} + page = self._request.get("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i), auth=self._basic, params=params) + 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} + self._logger.info("{0} : Create {1} : {2}".format(self._name, i, title_element)) + self._logger.debug("{0} : Data : {1}".format(self._name, data)) + try: + page = self._request.post("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + 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: @@ -504,30 +527,40 @@ class WPimport: bodyhtml = bodyhtml.replace(i["old_src"], o.path) hour = articledate[0].text time = dateheader[0].text.split(" ") + self._logger.debug("{0} : Title post : |{1}|".format(self._name, title)) + title = self._removeSpace(title) + self._logger.debug("{0} : Rendered Title post : |{1}|".format(self._name, title)) + data = {"title":title, "content":bodyhtml, "status":"publish", "date": "{0}-{1}-{2}T{3}:00".format(time[2],month[time[1]],time[0], hour), "tags": listelement["tags"], "categories": listelement["categories"]} + self._logger.debug("{0} : Data for post : |{1}| : {2}" .format(self._name, title, data)) + params = {"search":author, "per_page":100} try: self._logger.info("{0} : Search author : {1}".format(self._name, author)) - page = self._request.get("http://{0}/wp-json/wp/v2/users".format(self._wordpress), auth=self._basic, params=params) + page = self._request.get("http://{0}/wp-json/wp/v2/users".format(self._wordpress), 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)) + + 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[0]["id"])) + self._logger.info("{0} : Get author id : {1}".format(self._name, result)) result = page.json() - data["author"] = result[0]["id"] + 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)) - title = self._removeSpace(title) page_is_exist = False for index in range(1,10): params = {"search": title, "per_page":100, "page": index} try: - self._logger.info("{0} : Search post withi index {2} : {1}".format(self._name, title, index)) + self._logger.info("{0} : Search post with index {2} : {1}".format(self._name, title, index)) page = self._request.get("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, params=params, headers=self._headers_json) except ConnectionError as err: self._logger.error("{0} : Connection error for search post : {1}".format(self._name, err))