web_scrap/insert_wordpress.py

94 lines
3.7 KiB
Python
Raw Normal View History

2023-03-23 23:28:57 +01:00
#!/usr/bin/python3
from bs4 import BeautifulSoup
from urllib.parse import urlparse
from requests.auth import HTTPBasicAuth
from getpass import getpass
2023-03-27 23:51:51 +02:00
import requests, os, argparse, logging, re
2023-03-23 23:28:57 +01:00
if __name__ == '__main__':
2023-03-23 23:49:42 +01:00
tags = []
2023-03-23 23:28:57 +01:00
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"}
parser = argparse.ArgumentParser()
parser.add_argument("--user", help="wordpress user", required=True)
parser.add_argument("--file", help="HTML file", required=True)
args = parser.parse_args()
password = getpass()
if len(password) == 0:
print("No password error !!! ")
exit(1)
basic = HTTPBasicAuth(args.user, password)
2023-03-27 23:51:51 +02:00
liste = ["categories", "tags"]
elements = {}
element = {}
listelement = {}
2023-03-23 23:49:42 +01:00
2023-03-27 23:51:51 +02:00
for i in liste:
page = requests.get("http://localhost:8080/wp-json/wp/v2/{0}".format(i))
if page.status_code == 200:
elements[i] = page.json()
element[i] = []
listelement[i] = []
2023-03-23 23:49:42 +01:00
2023-03-27 23:51:51 +02:00
2023-03-23 23:28:57 +01:00
with open(args.file, 'r') as f:
contents = f.read()
soup = BeautifulSoup(contents, 'html.parser')
articletitle = soup.find_all("h2", class_="articletitle")
articlebody = soup.find_all("div", class_="articlebody")
articledate = soup.find_all("span", class_="articledate")
dateheader = soup.find_all("div", class_="dateheader")
itemfooter = soup.find_all("div", class_="itemfooter")
a = itemfooter[0].find_all("a", {"rel": True})
for i in a:
rel = i.get("rel")
if rel[0] == 'tag':
2023-03-27 23:51:51 +02:00
href = i.get("href")
if re.search(r'/tag/', href):
element["tags"].append(i.text)
if re.search(r'/archives/', href):
element["categories"].append(i.text)
for i in liste:
for j in element[i]:
element_exist = False
for k in elements[i]:
if k["name"] == j:
element_exist = True
array = listelement[i].append(k["id"])
if element_exist is False:
data = {"name": j}
page = requests.post("http://localhost:8080/wp-json/wp/v2/{0}".format(i), auth=basic, data=data)
if page.status_code == 201:
result = page.json()
listelement[i].append(result["id"])
2023-03-23 23:49:42 +01:00
2023-03-27 23:51:51 +02:00
2023-03-23 23:49:42 +01:00
2023-03-23 23:28:57 +01:00
title = articletitle[0].text
body = articlebody[0].find_all("p")
bodyhtml = "<p>"
for i in body:
if len(i.text) == 1:
bodyhtml = bodyhtml + "<br />"
else:
bodyhtml = bodyhtml + str(i).replace("<p>", "").replace("</p>", "").replace("<br>", "<br />") + "<br />"
bodyhtml = bodyhtml + "</p>"
print(bodyhtml)
2023-03-23 23:28:57 +01:00
hour = articledate[0].text
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"]}
params = {"search":title}
page = requests.get("http://localhost:8080/wp-json/wp/v2/posts", auth=basic, params=params)
page_exist = True
if page.status_code:
2023-03-28 12:07:11 +02:00
result = page.json()
if len(result) == 0:
page_exist = False
2023-03-23 23:28:57 +01:00
if page_exist == False:
page = requests.post("http://localhost:8080/wp-json/wp/v2/posts", auth=basic, data=data)
if page.status_code == 201:
print(page.content)