diff --git a/insert_wordpress.py b/insert_wordpress.py index 662bcda..ad30fd8 100644 --- a/insert_wordpress.py +++ b/insert_wordpress.py @@ -15,6 +15,13 @@ class WPimport: with open(file, 'r') as f: contents = f.read() self.insertWordpress(contents) + + def _linkImgPost(self, title, list_img, post_id): + for i in list_img: + data = {"post": post_id} + r = requests.post("http://{0}/wp-json/wp/v2/media/{1}".format(self.wordpress, i["id"]), auth=self.basic, data=data) + if r.status_code == 200: + print("Association d'une image à l'article {0}".format(title)) def insertWordpress(self, content): tags = [] @@ -156,6 +163,7 @@ class WPimport: if page.status_code == 200: result = page.json() print("Article mis à jour : {0}".format(result["title"]["raw"])) + self._linkImgPost(result["title"]["raw"], list_img, result["id"]) @@ -169,12 +177,7 @@ class WPimport: page = requests.post("http://{0}/wp-json/wp/v2/comments".format(self.wordpress), auth=self.basic, data=data) if page.status_code == 201: print("Commentaire ajoute pour {0}".format(result["title"]["raw"])) - for i in list_img: - data = {"post": result["id"]} - r = requests.post("http://{0}/wp-json/wp/v2/media/{1}".format(self.wordpress, i["id"]), auth=self.basic, data=data) - if r.status_code == 200: - print("Association d'une image à l'article {0}".format(result["title"]["raw"])) - + self._linkImgPost(result["title"]["raw"], list_img, result["id"])