#!/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, 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: content = f.read() soup = BeautifulSoup(content, 'html.parser') articlebody = soup.find_all("div", class_="articlebody") if len(articlebody) > 0: self._addOrUpdatePost(soup) # Private method ## Association image to post def _linkImgPost(self, title, list_img, post_id): for i in list_img: data = {"post": post_id} r = requests.post("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, i["id"]), auth=self._basic, data=data) if r.status_code == 200: print("Association d'une image à l'article {0}".format(title)) ## Add or update img def _addOrUpdateMedia(self, href_img, page): media = {"id":"", "rendered":""} split_fileimg = href_img.split("/") img_name = split_fileimg[len(split_fileimg)-1] params = { "search": img_name} r = requests.get("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, params=params) if r.status_code == 200: res = r.json() if len(res) == 0: data = page.content img_type = "image/png" if img_name.split(".")[1] == "jpg" or img_name.split(".")[1] == "jpeg": img_type = "image/jpg" headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)} r = requests.post("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, headers=headers, data=data) if r.status_code == 201: res = r.json() media["id"] = res["id"] media["rendered"] = res["guid"]["rendered"] else: media["id"] = res[0]["id"] media["rendered"] = res[0]["guid"]["rendered"] return media ## 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"] elements = {} element = {} listelement = {} for i in liste: page = requests.get("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress,i)) if page.status_code == 200: elements[i] = page.json() element[i] = [] listelement[i] = [] articletitle = soup.find_all("h2", class_="articletitle") articlebody = soup.find_all("div", class_="articlebody") articledate = soup.find_all("span", class_="articledate") articleacreator = soup.find_all("span", class_="articlecreator") dateheader = soup.find_all("div", class_="dateheader") itemfooter = soup.find_all("div", class_="itemfooter") comment = soup.find_all("div", class_="comment_item") img_a = articlebody[0].find_all("a", {"target": "_blank"}) list_img = [] for i in img_a: new_img = {} img = i.find_all("img") if len(img) > 0: href_a = i.get("href") href_img = img[0].get("src") new_img["old_src"]=href_img new_img["old_href"]=href_a page_img = requests.get(href_img) img_break = False if page_img.status_code == 404: href_img = href_a img_break = True page = requests.get(href_a) if page.status_code == 200: media=self._addOrUpdateMedia(href_img, page) new_img["id"]=media["id"] new_img["new_src"]=media["rendered"] new_img["break"]=img_break list_img.append(new_img) comment_post = [] for i in comment: comment_item = i.text.split("\n") footer = i.find_all("div", class_="itemfooter") comment_author = footer[0].text.split(",")[0].replace("Posté par ", "") comment_date = footer[0].find_all("abbr")[0].get("title") comment_content = "
"
for j in range(0, len(comment_item)-2):
if len(comment_item[j]) > 0:
comment_content = comment_content + comment_item[j] + "
"
comment_content = comment_content + "
"
for i in body:
if len(i.text) == 1:
bodyhtml = bodyhtml + "
"
else:
bodyhtml = bodyhtml + str(i).replace("
", "").replace("
", "").replace("