Compare commits
2 Commits
404ad5dd6c
...
ba511bc6c4
Author | SHA1 | Date | |
---|---|---|---|
ba511bc6c4 | |||
665f1474f2 |
@@ -13,23 +13,21 @@ class WPimport:
|
||||
|
||||
# Public method
|
||||
|
||||
def fromFile(self, file):
|
||||
with open(file, 'r') as f:
|
||||
content = f.read()
|
||||
soup = BeautifulSoup(content, 'html.parser')
|
||||
articlebody = soup.find_all("div", class_="articlebody")
|
||||
if len(articlebody) > 0:
|
||||
self._addOrUpdatePost(soup)
|
||||
else:
|
||||
self._addOrUpdateFeaturedMedia(soup)
|
||||
def fromFile(self, files):
|
||||
for file in files.split(","):
|
||||
with open(file, 'r') as f:
|
||||
content = f.read()
|
||||
soup = BeautifulSoup(content, 'html.parser')
|
||||
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:
|
||||
@@ -59,7 +57,7 @@ class WPimport:
|
||||
if r.status_code == 200:
|
||||
print("Ajout media featured : {0}".format(r.json()["title"]["raw"]))
|
||||
else:
|
||||
print("Aucun media trouvé")
|
||||
print("Aucun media trouvé pour {0}".format(h2))
|
||||
|
||||
## 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)
|
||||
if r.status_code == 200:
|
||||
res = r.json()
|
||||
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)
|
||||
params = {"force":1}
|
||||
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
|
||||
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:
|
||||
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:
|
||||
print("Ajout d'image {0}".format(img_name))
|
||||
res = r.json()
|
||||
media["id"] = res["id"]
|
||||
media["rendered"] = res["guid"]["rendered"]
|
||||
|
Reference in New Issue
Block a user