get featured image for post wip

This commit is contained in:
Valentin CZERYBA 2023-04-02 16:56:07 +02:00
parent cc33ab34df
commit 42b7e7e408

View File

@ -6,15 +6,45 @@ 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:
@ -23,7 +53,10 @@ class WPimport:
if r.status_code == 200:
print("Association d'une image à l'article {0}".format(title))
def _insertWordpress(self, content):
## Add or Update post
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")