insert #6
@ -8,22 +8,22 @@ import requests, os, argparse, logging, re, json
|
||||
class WPimport:
|
||||
|
||||
def __init__(self, basic, wordpress):
|
||||
self.basic = basic
|
||||
self.wordpress = wordpress
|
||||
self._basic = basic
|
||||
self._wordpress = wordpress
|
||||
|
||||
def fromFile(self, file):
|
||||
with open(file, 'r') as f:
|
||||
contents = f.read()
|
||||
self.insertWordpress(contents)
|
||||
self._insertWordpress(contents)
|
||||
|
||||
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)
|
||||
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))
|
||||
|
||||
def insertWordpress(self, content):
|
||||
def _insertWordpress(self, content):
|
||||
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"]
|
||||
@ -32,7 +32,7 @@ class WPimport:
|
||||
listelement = {}
|
||||
|
||||
for i in liste:
|
||||
page = requests.get("http://{0}/wp-json/wp/v2/{1}".format(self.wordpress,i))
|
||||
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] = []
|
||||
@ -67,7 +67,7 @@ class WPimport:
|
||||
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)
|
||||
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:
|
||||
@ -76,7 +76,7 @@ class WPimport:
|
||||
if img_name.split(".")[1]:
|
||||
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)
|
||||
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()
|
||||
id_res = res["id"]
|
||||
@ -120,7 +120,7 @@ class WPimport:
|
||||
listelement[i].append(k["id"])
|
||||
if element_exist is False:
|
||||
data = {"name": j}
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/{1}".format(self.wordpress, i), auth=self.basic, data=data)
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i), auth=self._basic, data=data)
|
||||
if page.status_code == 201:
|
||||
result = page.json()
|
||||
listelement[i].append(result["id"])
|
||||
@ -143,13 +143,13 @@ class WPimport:
|
||||
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":author}
|
||||
page = requests.get("http://{0}/wp-json/wp/v2/users".format(self.wordpress), auth=self.basic, params=params)
|
||||
page = requests.get("http://{0}/wp-json/wp/v2/users".format(self._wordpress), auth=self._basic, params=params)
|
||||
if page.status_code == 200:
|
||||
result = page.json()
|
||||
data["author"] = result[0]["id"]
|
||||
|
||||
params = {"search":title}
|
||||
page = requests.get("http://{0}/wp-json/wp/v2/posts".format(self.wordpress), auth=self.basic, params=params)
|
||||
page = requests.get("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, params=params)
|
||||
page_exist = True
|
||||
headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
|
||||
if page.status_code == 200:
|
||||
@ -159,7 +159,7 @@ class WPimport:
|
||||
else:
|
||||
print("La page {0} existe deja et mis à jour".format(title))
|
||||
post_id = result[0]["id"]
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self.wordpress, post_id), auth=self.basic, headers=headers, data=json.dumps(data))
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id), auth=self._basic, headers=headers, data=json.dumps(data))
|
||||
if page.status_code == 200:
|
||||
result = page.json()
|
||||
print("Article mis à jour : {0}".format(result["title"]["raw"]))
|
||||
@ -168,13 +168,13 @@ class WPimport:
|
||||
|
||||
|
||||
if page_exist == False:
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/posts".format(self.wordpress), auth=self.basic, headers=headers, data=json.dumps(data))
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, headers=headers, data=json.dumps(data))
|
||||
if page.status_code == 201:
|
||||
result = page.json()
|
||||
print("Article ajoute : {0}".format(result["title"]["raw"]))
|
||||
for i in comment_post:
|
||||
data = {"post": result["id"], "content": i["content"], "date": i["date"], "author_name": i["author"]}
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/comments".format(self.wordpress), auth=self.basic, data=data)
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, data=data)
|
||||
if page.status_code == 201:
|
||||
print("Commentaire ajoute pour {0}".format(result["title"]["raw"]))
|
||||
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
|
||||
|
Loading…
x
Reference in New Issue
Block a user