update error message + add debug

This commit is contained in:
Valentin CZERYBA 2023-04-18 00:00:32 +02:00
parent a5e7cb89f7
commit d64aed6240

View File

@ -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)