2 Commits

Author SHA1 Message Date
ba511bc6c4 Ajout print image ajoute 2023-04-04 22:14:10 +02:00
665f1474f2 delete and replace image 2023-04-04 22:07:36 +02:00

View File

@@ -13,23 +13,21 @@ class WPimport:
# Public method # Public method
def fromFile(self, file): def fromFile(self, files):
with open(file, 'r') as f: for file in files.split(","):
content = f.read() with open(file, 'r') as f:
soup = BeautifulSoup(content, 'html.parser') content = f.read()
articlebody = soup.find_all("div", class_="articlebody") soup = BeautifulSoup(content, 'html.parser')
if len(articlebody) > 0: articlebody = soup.find_all("div", class_="articlebody")
self._addOrUpdatePost(soup) if len(articlebody) > 0:
else: self._addOrUpdatePost(soup)
self._addOrUpdateFeaturedMedia(soup) else:
self._addOrUpdateFeaturedMedia(soup)
# Private method # Private method
## 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:
@@ -59,7 +57,7 @@ class WPimport:
if r.status_code == 200: if r.status_code == 200:
print("Ajout media featured : {0}".format(r.json()["title"]["raw"])) print("Ajout media featured : {0}".format(r.json()["title"]["raw"]))
else: else:
print("Aucun media trouvé") print("Aucun media trouvé pour {0}".format(h2))
## Association image to post ## Association image to post
@@ -80,17 +78,19 @@ class WPimport:
r = requests.get("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, params=params) r = requests.get("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, params=params)
if r.status_code == 200: if r.status_code == 200:
res = r.json() res = r.json()
url = "http://{0}/wp-json/wp/v2/media".format(self._wordpress)
if len(res) > 0: if len(res) > 0:
url = "http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, res[0]["id"]) params = {"force":1}
print(url) r = requests.delete("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, res[0]["id"]), auth=self._basic, params=params)
if r.status_code == 200:
print("Image supprimé {0}".format(img_name))
data = page.content data = page.content
img_type = "image/png" img_type = "image/png"
if img_name.split(".")[1] == "jpg" or img_name.split(".")[1] == "jpeg": if img_name.split(".")[1] == "jpg" or img_name.split(".")[1] == "jpeg":
img_type = "image/jpg" img_type = "image/jpg"
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(url, 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 or r.status_code == 200: 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"]