add featured media

This commit is contained in:
Valentin CZERYBA 2023-04-02 17:51:54 +02:00
parent ec4135c5d0
commit 1e162662e6

View File

@ -41,7 +41,13 @@ class WPimport:
img = i.find_all("img") img = i.find_all("img")
if len(img) > 0: if len(img) > 0:
img_src = img[0].get("src") img_src = img[0].get("src")
print(img_src) page = requests.get(img_src)
if page.status_code == 200:
media=self._addOrUpdateMedia(img_src, page)
data = {"featured_media": media["id"]}
r = requests.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, result[0]["id"]), auth=self._basic, data=json.dumps(data))
if r.status_code == 200:
print("Ajout media featured : {0}".format(r.json()["title"]["raw"]))
## Association image to post ## Association image to post
@ -55,7 +61,7 @@ class WPimport:
## Add or update img ## Add or update img
def _addOrUpdateMedia(self, href_img): def _addOrUpdateMedia(self, href_img, page):
media = {"id":"", "rendered":""} media = {"id":"", "rendered":""}
split_fileimg = href_img.split("/") split_fileimg = href_img.split("/")
img_name = split_fileimg[len(split_fileimg)-1] img_name = split_fileimg[len(split_fileimg)-1]
@ -120,7 +126,7 @@ class WPimport:
img_break = True img_break = True
page = requests.get(href_a) page = requests.get(href_a)
if page.status_code == 200: if page.status_code == 200:
media=self._addOrUpdateMedia(href_img) media=self._addOrUpdateMedia(href_img, page)
new_img["id"]=media["id"] new_img["id"]=media["id"]
new_img["new_src"]=media["rendered"] new_img["new_src"]=media["rendered"]
new_img["break"]=img_break new_img["break"]=img_break