add error status code

This commit is contained in:
Valentin CZERYBA 2023-04-17 23:44:09 +02:00
parent ae7cb1e4e0
commit a5e7cb89f7

View File

@ -41,6 +41,8 @@ class WPimport:
self._addOrUpdatePost(soup)
else:
self._addOrUpdateFeaturedMedia(soup)
else:
self._logger.error("Connection error with status code : {0}".format(r.status_code))
def fromDirectory(self, directory):
@ -131,9 +133,17 @@ class WPimport:
exit(1)
if r.status_code == 200:
self._logger.info("Ajout media featured : {0}".format(r.json()["title"]["raw"]))
else:
self._logger.error("Connection error with status code : {0}".format(r.status_code))
else:
self._logger.info("Aucun media trouvé pour {0}".format(h2))
else:
self._logger.error("Connection error with status code : {0}".format(page.status_code))
else:
self._logger.error("Connection error with status code : {0}".format(page.status_code))
else:
self._logger.error("Connection error with status code : {0}".format(page.status_code))
## Association image to post
def _linkImgPost(self, title, list_img, post_id):
@ -146,6 +156,8 @@ class WPimport:
exit(1)
if r.status_code == 200:
self._logger.info("Association d'une image à l'article {0}".format(title))
else:
self._logger.error("Connection error with status code : {0}".format(r.status_code))
## Add or update img
@ -170,6 +182,8 @@ class WPimport:
exit(1)
if r.status_code == 200:
self._logger.info("Image supprimé {0}".format(img_name))
else:
self._logger.error("Image not removed due status code : {0}".format(r.status_code))
data = page.content
img_type = "image/png"
if img_name.split(".")[1] == "jpg" or img_name.split(".")[1] == "jpeg":
@ -185,6 +199,10 @@ class WPimport:
res = r.json()
media["id"] = res["id"]
media["rendered"] = res["guid"]["rendered"]
else:
self._logger.error("Image not added due status code : {0}".format(r.status_code))
else:
self._logger.error("Connection error with status code : {0}".format(r.status_code))
return media
## Add or update comment
@ -214,6 +232,8 @@ class WPimport:
exit(1)
if page.status_code == 200:
self._logger.info("Commentaire mise à jour pour {0}".format(title))
else:
self._logger.error("Comment not updated due status code : {0}".format(page.status_code))
else:
if i["parent_id"] != -1:
parent_id = int(i["parent_id"])
@ -227,13 +247,21 @@ class WPimport:
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))
self._logger.info(data)
try:
page = self._request.post("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, data=data)
except Exception as err:
self._logger.error("Connection error : {0}".format(err))
exit(1)
self._logger.info(page.status_code)
if page.status_code == 201:
self._logger.info("Commentaire ajoute pour {0}".format(title))
else:
self._logger.error("Comment not added due status code : {0}".format(page.status_code))
else:
self._logger.error("Connection error with status code : {0}".format(page.status_code))
## Check class name
@ -302,6 +330,8 @@ class WPimport:
elements[i] = page.json()
element[i] = []
listelement[i] = []
else:
self._logger.error("Connection error with status code : {0}".format(page.status_code))
articletitle = soup.find_all("h2", class_="articletitle")
articlebody = soup.find_all("div", class_="articlebody")
@ -332,6 +362,8 @@ 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"]
@ -371,6 +403,8 @@ class WPimport:
if page.status_code == 201:
result = page.json()
listelement[i].append(result["id"])
else:
self._logger.error("{0} not added due status code : {1}".format(i, page.status_code))
title = articletitle[0].text
author = articleacreator[0].text.lower()
@ -398,6 +432,8 @@ class WPimport:
if page.status_code == 200:
result = page.json()
data["author"] = result[0]["id"]
else:
self._logger.error("Connection error with status code : {0}".format(page.status_code))
params = {"search":title}
try:
@ -424,6 +460,10 @@ class WPimport:
self._logger.info("Article mis à jour : {0}".format(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("Post not updated due status code : {0}".format(page.status_code))
else:
self._logger.error("Connection error with status code : {0}".format(page.status_code))
if page_exist == False:
try:
@ -435,4 +475,6 @@ class WPimport:
result = page.json()
self._logger.info("Article ajoute : {0}".format(result["title"]["raw"]))
self._addOrUpdateComment(result["id"], comment_post, result["title"]["raw"])
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
else:
self._logger.error("Post not added due status code : {0}".format(r.status_code))