add author for article

This commit is contained in:
Valentin CZERYBA 2023-03-28 15:28:34 +02:00
parent 605bd06e51
commit 82ce3d1a2b

View File

@ -39,6 +39,7 @@ if __name__ == '__main__':
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")
articleacreator = soup.find_all("span", class_="articlecreator")
dateheader = soup.find_all("div", class_="dateheader") dateheader = soup.find_all("div", class_="dateheader")
itemfooter = soup.find_all("div", class_="itemfooter") itemfooter = soup.find_all("div", class_="itemfooter")
a = itemfooter[0].find_all("a", {"rel": True}) a = itemfooter[0].find_all("a", {"rel": True})
@ -68,6 +69,15 @@ if __name__ == '__main__':
title = articletitle[0].text title = articletitle[0].text
author = articleacreator[0].text.lower()
author_exist = False
params = {"search":author}
page = requests.get("http://localhost:8080/wp-json/wp/v2/users", auth=basic, params=params)
if page.status_code == 200:
result = page.json()
print(result)
author_id = result[0]["id"]
author_exist = True
body = articlebody[0].find_all("p") body = articlebody[0].find_all("p")
bodyhtml = "<p>" bodyhtml = "<p>"
for i in body: for i in body:
@ -76,10 +86,12 @@ if __name__ == '__main__':
else: else:
bodyhtml = bodyhtml + str(i).replace("<p>", "").replace("</p>", "").replace("<br>", "<br />") + "<br />" bodyhtml = bodyhtml + str(i).replace("<p>", "").replace("</p>", "").replace("<br>", "<br />") + "<br />"
bodyhtml = bodyhtml + "</p>" bodyhtml = bodyhtml + "</p>"
print(bodyhtml)
hour = articledate[0].text hour = articledate[0].text
time = dateheader[0].text.split(" ") time = dateheader[0].text.split(" ")
data = {"title":title, "content":bodyhtml, "status":"publish", "date": "{0}-{1}-{2}T{3}:00".format(time[2],month[time[1]],time[0], hour), "tags": listelement["tags"], "categories": listelement["categories"]} data = {"title":title, "content":bodyhtml, "status":"publish", "date": "{0}-{1}-{2}T{3}:00".format(time[2],month[time[1]],time[0], hour), "tags": listelement["tags"], "categories": listelement["categories"]}
if author_exist == True:
data["author"] = author_id
params = {"search":title} params = {"search":title}
page = requests.get("http://localhost:8080/wp-json/wp/v2/posts", auth=basic, params=params) page = requests.get("http://localhost:8080/wp-json/wp/v2/posts", auth=basic, params=params)
page_exist = True page_exist = True