diff --git a/insert_wordpress.py b/insert_wordpress.py index cd9b3ed..86fab01 100644 --- a/insert_wordpress.py +++ b/insert_wordpress.py @@ -3,7 +3,7 @@ from bs4 import BeautifulSoup from urllib.parse import urlparse from requests.auth import HTTPBasicAuth from getpass import getpass -import requests, os, argparse, logging +import requests, os, argparse, logging, re if __name__ == '__main__': @@ -19,13 +19,19 @@ if __name__ == '__main__': exit(1) basic = HTTPBasicAuth(args.user, password) - page = requests.get("http://localhost:8080/wp-json/wp/v2/tags") - - if page.status_code == 200: - tags = page.json() - print(tags) + liste = ["categories", "tags"] + elements = {} + element = {} + listelement = {} + 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] = [] + with open(args.file, 'r') as f: contents = f.read() @@ -36,34 +42,36 @@ if __name__ == '__main__': 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) - listtag = [] - for i in tag: - tag_exist = False - for j in tags: - if j["name"] == i: - tag_exist = True - listtag.append(j["id"]) - if tag_exist is False: - data = {"name": i} - page = requests.post("http://localhost:8080/wp-json/wp/v2/tags", auth=basic, data=data) - if page.status_code == 201: - result = page.json() - listtag.append(result["id"]) + 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"]) - + + 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": listtag} - print(data) - exit(0) + data = {"title":title, "content":body, "status":"publish", "date": "{0}-{1}-{2}T{3}:00".format(time[2],month[time[1]],time[0], hour), "tags": listelement["tags"], "categories": listelement["categories"]} page = requests.post("http://localhost:8080/wp-json/wp/v2/posts", auth=basic, data=data) print(page.status_code)