get featured image for post wip
This commit is contained in:
parent
cc33ab34df
commit
42b7e7e408
@ -6,24 +6,57 @@ from getpass import getpass
|
|||||||
import requests, os, argparse, logging, re, json
|
import requests, os, argparse, logging, re, json
|
||||||
|
|
||||||
class WPimport:
|
class WPimport:
|
||||||
|
# Constructor
|
||||||
def __init__(self, basic, wordpress):
|
def __init__(self, basic, wordpress):
|
||||||
self._basic = basic
|
self._basic = basic
|
||||||
self._wordpress = wordpress
|
self._wordpress = wordpress
|
||||||
|
|
||||||
|
# Public method
|
||||||
|
|
||||||
def fromFile(self, file):
|
def fromFile(self, file):
|
||||||
with open(file, 'r') as f:
|
with open(file, 'r') as f:
|
||||||
contents = f.read()
|
content = f.read()
|
||||||
self._insertWordpress(contents)
|
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
|
||||||
|
|
||||||
|
|
||||||
|
## Get or update featured image
|
||||||
|
|
||||||
|
def _addOrUpdateFeaturedMedia(self, soup):
|
||||||
|
item_div = soup.find_all("div", {"data-edittype": "post"})
|
||||||
|
for i in item_div:
|
||||||
|
h2 = i.find_all("h2")[0].text
|
||||||
|
params = {"search":h2, "type":"post"}
|
||||||
|
page = requests.get("http://{0}/wp-json/wp/v2/search".format(self._wordpress), auth=self._basic, params=params)
|
||||||
|
if page.status_code == 200:
|
||||||
|
result = page.json()
|
||||||
|
if len(result) > 0:
|
||||||
|
if h2 == result[0]["title"]:
|
||||||
|
img = i.find_all("img")
|
||||||
|
if len(img) > 0:
|
||||||
|
img_src = img[0].get("src")
|
||||||
|
print(img_src)
|
||||||
|
|
||||||
|
|
||||||
|
## Association image to post
|
||||||
|
|
||||||
def _linkImgPost(self, title, list_img, post_id):
|
def _linkImgPost(self, title, list_img, post_id):
|
||||||
for i in list_img:
|
for i in list_img:
|
||||||
data = {"post": post_id}
|
data = {"post": post_id}
|
||||||
r = requests.post("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, i["id"]), auth=self._basic, data=data)
|
r = requests.post("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, i["id"]), auth=self._basic, data=data)
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
print("Association d'une image à l'article {0}".format(title))
|
print("Association d'une image à l'article {0}".format(title))
|
||||||
|
|
||||||
|
|
||||||
|
## Add or Update post
|
||||||
|
|
||||||
def _insertWordpress(self, content):
|
def _addOrUpdatePost(self, soup):
|
||||||
tags = []
|
tags = []
|
||||||
month = {"janvier":"01", "février": "02", "mars": "03", "avril":"04", "mai": "05", "juin": "06", "juillet": "07", "août": "08", "septembre": "09", "octobre": "10", "novembre": "11", "décembre": "12"}
|
month = {"janvier":"01", "février": "02", "mars": "03", "avril":"04", "mai": "05", "juin": "06", "juillet": "07", "août": "08", "septembre": "09", "octobre": "10", "novembre": "11", "décembre": "12"}
|
||||||
liste = ["categories", "tags"]
|
liste = ["categories", "tags"]
|
||||||
@ -38,7 +71,6 @@ class WPimport:
|
|||||||
element[i] = []
|
element[i] = []
|
||||||
listelement[i] = []
|
listelement[i] = []
|
||||||
|
|
||||||
soup = BeautifulSoup(content, 'html.parser')
|
|
||||||
articletitle = soup.find_all("h2", class_="articletitle")
|
articletitle = soup.find_all("h2", class_="articletitle")
|
||||||
articlebody = soup.find_all("div", class_="articlebody")
|
articlebody = soup.find_all("div", class_="articlebody")
|
||||||
articledate = soup.find_all("span", class_="articledate")
|
articledate = soup.find_all("span", class_="articledate")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user