diff --git a/insert_wordpress.py b/insert_wordpress.py index fff8279..06185fe 100644 --- a/insert_wordpress.py +++ b/insert_wordpress.py @@ -20,8 +20,46 @@ class WPimport: articlebody = soup.find_all("div", class_="articlebody") if len(articlebody) > 0: self._addOrUpdatePost(soup) + else: + self._addOrUpdateFeaturedMedia(soup) # Private method + + ## Add or update featured media + + + ## Get or update featured image + + def _addOrUpdateFeaturedMedia(self, soup): + item_div = soup.find_all("div", {"data-edittype": "post"}) + for i in item_div: + h2 = i.find_all("h2")[0].text + params = {"search":h2, "type":"post"} + page = requests.get("http://{0}/wp-json/wp/v2/search".format(self._wordpress), auth=self._basic, params=params) + if page.status_code == 200: + result = page.json() + if len(result) > 0: + if h2 == result[0]["title"]: + img = i.find_all("img") + if len(img) > 0: + img_src = img[0].get("src") + page = requests.get(img_src) + 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 = requests.get("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, params=params) + 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 = requests.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, result[0]["id"]), auth=self._basic, headers=headers, data=json.dumps(data)) + if r.status_code == 200: + print("Ajout media featured : {0}".format(r.json()["title"]["raw"])) + else: + print("Aucun media trouvé") ## Association image to post @@ -93,17 +131,19 @@ class WPimport: new_img["old_src"]=href_img new_img["old_href"]=href_a page_img = requests.get(href_img) - img_break = False if page_img.status_code == 404: href_img = href_a - img_break = True - page = requests.get(href_a) - if page.status_code == 200: - media=self._addOrUpdateMedia(href_img, page) + page_img = requests.get(href_a) + if page_img.status_code == 200: + media=self._addOrUpdateMedia(href_img, page_img) new_img["id"]=media["id"] new_img["new_src"]=media["rendered"] - new_img["break"]=img_break list_img.append(new_img) + if href_img != href_a: + media=self._addOrUpdateMedia(href_a, page_img) + new_img["id"]=media["id"] + new_img["new_src"]=media["rendered"] + list_img.append(new_img) comment_post = [] for i in comment: @@ -156,7 +196,7 @@ class WPimport: bodyhtml = bodyhtml.replace(i["old_src"], o.path) hour = articledate[0].text 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"], "featured_media":list_img[0]["id"]} + 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 = requests.get("http://{0}/wp-json/wp/v2/users".format(self._wordpress), auth=self._basic, params=params) if page.status_code == 200: