diff --git a/insert_wordpress.py b/insert_wordpress.py index 897a1d4..fb61e6b 100644 --- a/insert_wordpress.py +++ b/insert_wordpress.py @@ -6,24 +6,57 @@ from getpass import getpass import requests, os, argparse, logging, re, json class WPimport: - + # Constructor def __init__(self, basic, wordpress): self._basic = basic self._wordpress = wordpress + # Public method + def fromFile(self, file): with open(file, 'r') as f: - contents = f.read() - self._insertWordpress(contents) + 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 + + + ## 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): for i in list_img: 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) if r.status_code == 200: print("Association d'une image à l'article {0}".format(title)) + + + ## Add or Update post - def _insertWordpress(self, content): + def _addOrUpdatePost(self, soup): 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"} liste = ["categories", "tags"] @@ -38,7 +71,6 @@ class WPimport: element[i] = [] listelement[i] = [] - soup = BeautifulSoup(content, 'html.parser') articletitle = soup.find_all("h2", class_="articletitle") articlebody = soup.find_all("div", class_="articlebody") articledate = soup.find_all("span", class_="articledate")