Ajout print image ajoute

This commit is contained in:
Valentin CZERYBA 2023-04-04 22:14:10 +02:00
parent 665f1474f2
commit ba511bc6c4

View File

@ -13,7 +13,8 @@ class WPimport:
# Public method # Public method
def fromFile(self, file): def fromFile(self, files):
for file in files.split(","):
with open(file, 'r') as f: with open(file, 'r') as f:
content = f.read() content = f.read()
soup = BeautifulSoup(content, 'html.parser') soup = BeautifulSoup(content, 'html.parser')
@ -27,9 +28,6 @@ class WPimport:
## Add or update featured media ## Add or update featured media
## Get or update featured image
def _addOrUpdateFeaturedMedia(self, soup): def _addOrUpdateFeaturedMedia(self, soup):
item_div = soup.find_all("div", {"data-edittype": "post"}) item_div = soup.find_all("div", {"data-edittype": "post"})
for i in item_div: for i in item_div:
@ -92,6 +90,7 @@ class WPimport:
headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)} headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)}
r = requests.post("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, headers=headers, data=data) r = requests.post("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, headers=headers, data=data)
if r.status_code == 201: if r.status_code == 201:
print("Ajout d'image {0}".format(img_name))
res = r.json() res = r.json()
media["id"] = res["id"] media["id"] = res["id"]
media["rendered"] = res["guid"]["rendered"] media["rendered"] = res["guid"]["rendered"]