From 404ad5dd6cd3ebd5ce5ca7aa27385833b8885f81 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 4 Apr 2023 00:00:28 +0200 Subject: [PATCH] update image not fix --- insert_wordpress.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/insert_wordpress.py b/insert_wordpress.py index 06185fe..ba799d3 100644 --- a/insert_wordpress.py +++ b/insert_wordpress.py @@ -80,20 +80,20 @@ class WPimport: r = requests.get("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, params=params) if r.status_code == 200: res = r.json() - if len(res) == 0: - data = page.content - img_type = "image/png" - if img_name.split(".")[1] == "jpg" or img_name.split(".")[1] == "jpeg": - img_type = "image/jpg" - 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) - if r.status_code == 201: - res = r.json() - media["id"] = res["id"] - media["rendered"] = res["guid"]["rendered"] - else: - media["id"] = res[0]["id"] - media["rendered"] = res[0]["guid"]["rendered"] + url = "http://{0}/wp-json/wp/v2/media".format(self._wordpress) + if len(res) > 0: + url = "http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, res[0]["id"]) + print(url) + data = page.content + img_type = "image/png" + if img_name.split(".")[1] == "jpg" or img_name.split(".")[1] == "jpeg": + img_type = "image/jpg" + headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)} + r = requests.post(url, auth=self._basic, headers=headers, data=data) + if r.status_code == 201 or r.status_code == 200: + res = r.json() + media["id"] = res["id"] + media["rendered"] = res["guid"]["rendered"] return media ## Add or Update post