add script insert wip
This commit is contained in:
parent
4b9a790f8a
commit
eae95d5671
50
insert_wordpress.py
Normal file
50
insert_wordpress.py
Normal file
@ -0,0 +1,50 @@
|
||||
#!/usr/bin/python3
|
||||
from bs4 import BeautifulSoup
|
||||
from urllib.parse import urlparse
|
||||
from requests.auth import HTTPBasicAuth
|
||||
from getpass import getpass
|
||||
import requests, os, argparse, logging
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
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)
|
||||
|
||||
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})
|
||||
tag = []
|
||||
for i in a:
|
||||
rel = i.get("rel")
|
||||
if rel[0] == 'tag':
|
||||
tag.append(i.text)
|
||||
print(tag)
|
||||
title = articletitle[0].text
|
||||
body = articlebody[0]
|
||||
hour = articledate[0].text
|
||||
time = dateheader[0].text.split(" ")
|
||||
data = {"title":title, "content":body, "status":"publish", "date": "{0}-{1}-{2}T{3}:00".format(time[2],month[time[1]],time[0], hour), "tags": [5]}
|
||||
#print(data)
|
||||
exit(0)
|
||||
|
||||
page = requests.post("http://localhost:8080/wp-json/wp/v2/posts", auth=basic, data=data)
|
||||
print(page.status_code)
|
||||
if page.status_code == 201:
|
||||
print(page.content)
|
Loading…
x
Reference in New Issue
Block a user