add try/except for request
This commit is contained in:
parent
225c7ecabb
commit
74e7f1d74b
120
lib/WPImport.py
120
lib/WPImport.py
@ -28,7 +28,11 @@ class WPimport:
|
||||
|
||||
def fromUrl(self, webpage):
|
||||
for i in range(0, len(webpage)):
|
||||
r = self._request.get(webpage[i])
|
||||
try:
|
||||
r = self._request.get(webpage[i])
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if r.status_code == 200:
|
||||
self._logger.info("({0}/{1} : Page en cours d'import : {2}".format(i+1, len(webpage), webpage[i]))
|
||||
soup = BeautifulSoup(r.content, self._parser)
|
||||
@ -88,7 +92,11 @@ class WPimport:
|
||||
for i in item_div:
|
||||
h2 = i.find_all("h2")[0].text
|
||||
params = {"search":h2, "type":"post"}
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/search".format(self._wordpress), auth=self._basic, params=params)
|
||||
try:
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/search".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()
|
||||
if len(result) > 0:
|
||||
@ -96,19 +104,31 @@ class WPimport:
|
||||
img = i.find_all("img")
|
||||
if len(img) > 0:
|
||||
img_src = img[0].get("src")
|
||||
page = self._request.get(img_src)
|
||||
try:
|
||||
page = self._request.get(img_src)
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if page.status_code == 200:
|
||||
name_img = img_src.replace("_q", "")
|
||||
name_img = name_img.split("/")[len(name_img.split("/"))-1]
|
||||
params = {"search": name_img}
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, params=params)
|
||||
try:
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/media".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:
|
||||
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}
|
||||
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))
|
||||
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))
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if r.status_code == 200:
|
||||
self._logger.info("Ajout media featured : {0}".format(r.json()["title"]["raw"]))
|
||||
else:
|
||||
@ -119,7 +139,11 @@ class WPimport:
|
||||
def _linkImgPost(self, title, list_img, post_id):
|
||||
for i in list_img:
|
||||
data = {"post": post_id}
|
||||
r = self._request.post("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, i["id"]), auth=self._basic, data=data)
|
||||
try:
|
||||
r = self._request.post("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, i["id"]), auth=self._basic, data=data)
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if r.status_code == 200:
|
||||
self._logger.info("Association d'une image à l'article {0}".format(title))
|
||||
|
||||
@ -130,12 +154,20 @@ class WPimport:
|
||||
split_fileimg = href_img.split("/")
|
||||
img_name = split_fileimg[len(split_fileimg)-1]
|
||||
params = { "search": img_name}
|
||||
r = self._request.get("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, params=params)
|
||||
try:
|
||||
r = self._request.get("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, params=params)
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if r.status_code == 200:
|
||||
res = r.json()
|
||||
if len(res) > 0:
|
||||
params = {"force":1}
|
||||
r = self._request.delete("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, res[0]["id"]), auth=self._basic, params=params)
|
||||
try:
|
||||
r = self._request.delete("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, res[0]["id"]), auth=self._basic, params=params)
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if r.status_code == 200:
|
||||
self._logger.info("Image supprimé {0}".format(img_name))
|
||||
data = page.content
|
||||
@ -143,7 +175,11 @@ class WPimport:
|
||||
if img_name.split(".")[1] == "jpg" or img_name.split(".")[1] == "jpeg":
|
||||
img_type = "image/jpg"
|
||||
headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)}
|
||||
r = self._request.post("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, headers=headers, data=data)
|
||||
try:
|
||||
r = self._request.post("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, headers=headers, data=data)
|
||||
except Exception as err:
|
||||
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))
|
||||
res = r.json()
|
||||
@ -156,7 +192,11 @@ class WPimport:
|
||||
def _addOrUpdateComment(self, post, comment, title):
|
||||
params = {"post": post}
|
||||
block = True
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, params=params)
|
||||
try:
|
||||
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 i in comment:
|
||||
@ -167,11 +207,19 @@ class WPimport:
|
||||
id_comment = j["id"]
|
||||
data = {"post": post, "content": i["content"], "date": i["date"], "author_name": i["author"]}
|
||||
if comment_exist is True:
|
||||
page = page = self._request.post("http://{0}/wp-json/wp/v2/comments/{1}".format(self._wordpress, id_comment), auth=self._basic, data=data)
|
||||
try:
|
||||
page = page = self._request.post("http://{0}/wp-json/wp/v2/comments/{1}".format(self._wordpress, id_comment), auth=self._basic, data=data)
|
||||
except Exception as err:
|
||||
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))
|
||||
else:
|
||||
page = self._request.post("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, data=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)
|
||||
if page.status_code == 201:
|
||||
self._logger.info("Commentaire ajoute pour {0}".format(title))
|
||||
|
||||
@ -186,7 +234,11 @@ class WPimport:
|
||||
listelement = {}
|
||||
|
||||
for i in liste:
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress,i))
|
||||
try:
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress,i))
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if page.status_code == 200:
|
||||
elements[i] = page.json()
|
||||
element[i] = []
|
||||
@ -209,10 +261,18 @@ class WPimport:
|
||||
href_img = img[0].get("src")
|
||||
new_img["old_src"]=href_img
|
||||
new_img["old_href"]=href_a
|
||||
page_img = self._request.get(href_img)
|
||||
try:
|
||||
page_img = self._request.get(href_img)
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if page_img.status_code == 404:
|
||||
href_img = href_a
|
||||
page_img = self._request.get(href_a)
|
||||
try:
|
||||
page_img = self._request.get(href_a)
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if page_img.status_code == 200:
|
||||
media=self._addOrUpdateMedia(href_img, page_img)
|
||||
new_img["id"]=media["id"]
|
||||
@ -254,7 +314,11 @@ class WPimport:
|
||||
listelement[i].append(k["id"])
|
||||
if element_exist is False:
|
||||
data = {"name": j}
|
||||
page = self._request.post("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i), auth=self._basic, data=data)
|
||||
try:
|
||||
page = self._request.post("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i), auth=self._basic, data=data)
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if page.status_code == 201:
|
||||
result = page.json()
|
||||
listelement[i].append(result["id"])
|
||||
@ -277,13 +341,21 @@ class WPimport:
|
||||
time = dateheader[0].text.split(" ")
|
||||
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"]}
|
||||
params = {"search":author}
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/users".format(self._wordpress), auth=self._basic, params=params)
|
||||
try:
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/users".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()
|
||||
data["author"] = result[0]["id"]
|
||||
|
||||
params = {"search":title}
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, params=params)
|
||||
try:
|
||||
page = self._request.get("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, params=params)
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
page_exist = True
|
||||
headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
|
||||
if page.status_code == 200:
|
||||
@ -293,7 +365,11 @@ class WPimport:
|
||||
else:
|
||||
self._logger.info("La page {0} existe deja et mis à jour".format(title))
|
||||
post_id = result[0]["id"]
|
||||
page = self._request.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id), auth=self._basic, headers=headers, data=json.dumps(data))
|
||||
try:
|
||||
page = self._request.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id), auth=self._basic, headers=headers, data=json.dumps(data))
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if page.status_code == 200:
|
||||
result = page.json()
|
||||
self._logger.info("Article mis à jour : {0}".format(result["title"]["raw"]))
|
||||
@ -301,7 +377,11 @@ class WPimport:
|
||||
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
|
||||
|
||||
if page_exist == False:
|
||||
page = self._request.post("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, headers=headers, data=json.dumps(data))
|
||||
try:
|
||||
page = self._request.post("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, headers=headers, data=json.dumps(data))
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
if page.status_code == 201:
|
||||
result = page.json()
|
||||
self._logger.info("Article ajoute : {0}".format(result["title"]["raw"]))
|
||||
|
Loading…
x
Reference in New Issue
Block a user