fix add comment

This commit is contained in:
Valentin CZERYBA 2023-04-19 23:16:39 +02:00
parent 1c252c9a14
commit f8d103ff61

View File

@ -209,24 +209,25 @@ class WPimport:
def _addOrUpdateComment(self, post, comment, title): def _addOrUpdateComment(self, post, comment, title):
params = {"post": post} params = {"post": post}
block = True for i in comment:
try: id_comment = []
page = self._request.get("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, params=params) comment_exist = False
except Exception as err: try:
self._logger.error("Connection error : {0}".format(err)) params = {"post": post, "author_name":i["author"], "date":i["date"]}
exit(1) page = self._request.get("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, params=params)
if page.status_code == 200: except Exception as err:
result = page.json() self._logger.error("Connection error : {0}".format(err))
for i in comment: exit(1)
comment_exist = False if page.status_code == 200:
result = page.json()
for j in result: for j in result:
if i["author"] == j["author_name"] and i["date"] == j["date"]: comment_exist = True
comment_exist = True id_comment.append(j["id"])
id_comment = j["id"] data = {"post": post, "content": i["content"], "date": i["date"], "author_name": i["author"], "status": "approved"}
data = {"post": post, "content": i["content"], "date": i["date"], "author_name": i["author"], "status": "approved"} if comment_exist is True:
if comment_exist is True: for j in id_comment:
try: try:
page = page = self._request.delete("http://{0}/wp-json/wp/v2/comments/{1}".format(self._wordpress, id_comment), auth=self._basic) page = page = self._request.delete("http://{0}/wp-json/wp/v2/comments/{1}".format(self._wordpress, j), auth=self._basic)
except Exception as err: except Exception as err:
self._logger.error("Connection error : {0}".format(err)) self._logger.error("Connection error : {0}".format(err))
exit(1) exit(1)
@ -235,32 +236,31 @@ class WPimport:
else: else:
self._logger.error("Comment not deleted for {0} due status code : {1}".format(title, page.status_code)) self._logger.error("Comment not deleted for {0} due status code : {1}".format(title, page.status_code))
if i["parent_id"] != -1: if i["parent_id"] != -1:
parent_id = int(i["parent_id"]) parent_id = int(i["parent_id"])
try: params = {"post": post, "author_name":comment[parent_id]["author"], "date":comment[parent_id]["date"]}
page = self._request.get("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, params=params)
except Exception as err:
self._logger.error("Connection error : {0}".format(err))
exit(1)
if page.status_code == 200:
result = page.json()
for j in result:
if comment[parent_id]["author"] == j["author_name"] and comment[parent_id]["date"] == j["date"]:
data["parent"]=j["id"]
else:
self._logger.error("Connection error with status code : {0}".format(page.status_code))
try: try:
page = self._request.post("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, data=data) page = self._request.get("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, params=params)
except Exception as err: except Exception as err:
self._logger.error("Connection error : {0}".format(err)) self._logger.error("Connection error : {0}".format(err))
exit(1) exit(1)
if page.status_code == 201: if page.status_code == 200:
self._logger.info("Comment added for {0}".format(title)) result = page.json()
if len(result) > 0:
data["parent"]=result[0]["id"]
else: else:
self._logger.error("Comment not added for {0} due status code : {1}".format(title, page.status_code)) self._logger.error("Connection error for parent comment with status code : {0}".format(page.status_code))
self._logger.debug(page.content) try:
else: page = self._request.post("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, data=data)
self._logger.error("Connection error with status code : {0}".format(page.status_code)) except Exception as err:
self._logger.error("Connection error : {0}".format(err))
exit(1)
if page.status_code == 201:
self._logger.info("Comment added for {0}".format(title))
else:
self._logger.error("Comment not added for {0} due status code : {1}".format(title, page.status_code))
self._logger.debug(page.content)
self._logger.debug("Data : {0}".format(data))
## Check class name ## Check class name
@ -392,7 +392,7 @@ class WPimport:
listelement[i].append(result[0]["id"]) listelement[i].append(result[0]["id"])
else: else:
self._logger.error("{0} not finded due status code : {1}".format(i, page.status_code)) self._logger.error("{0} not found due status code : {1}".format(i, page.status_code))
if element_exist is False: if element_exist is False:
data = {"name": j} data = {"name": j}
@ -409,7 +409,6 @@ class WPimport:
listelement[i].append(result["id"]) listelement[i].append(result["id"])
else: else:
self._logger.error("{0} not added due status code : {1}".format(i, page.status_code)) self._logger.error("{0} not added due status code : {1}".format(i, page.status_code))
self._logger.debug(listelement)
title = articletitle[0].text title = articletitle[0].text
author = articleacreator[0].text.lower() author = articleacreator[0].text.lower()
body = articlebody[0].find_all("p") body = articlebody[0].find_all("p")
@ -467,7 +466,7 @@ class WPimport:
else: else:
self._logger.error("Post not updated due status code : {0}".format(page.status_code)) self._logger.error("Post not updated due status code : {0}".format(page.status_code))
else: else:
self._logger.error("Connection error with status code : {0}".format(page.status_code)) self._logger.error("Connection for update post error with status code : {0}".format(page.status_code))
if page_exist == False: if page_exist == False:
try: try: