add json dumps for post

This commit is contained in:
Valentin CZERYBA 2023-04-01 18:32:17 +02:00
parent faa22f1438
commit 4054f41e9b

View File

@ -3,7 +3,7 @@ from bs4 import BeautifulSoup
from urllib.parse import urlparse from urllib.parse import urlparse
from requests.auth import HTTPBasicAuth from requests.auth import HTTPBasicAuth
from getpass import getpass from getpass import getpass
import requests, os, argparse, logging, re import requests, os, argparse, logging, re, json
class WPimport: class WPimport:
@ -134,7 +134,6 @@ class WPimport:
bodyhtml = bodyhtml.replace(i["old_src"], o.path) bodyhtml = bodyhtml.replace(i["old_src"], o.path)
hour = articledate[0].text hour = articledate[0].text
time = dateheader[0].text.split(" ") 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"]} 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} 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)
@ -145,6 +144,7 @@ class WPimport:
params = {"search":title} 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 page_exist = True
headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
if page.status_code == 200: if page.status_code == 200:
result = page.json() result = page.json()
if len(result) == 0: if len(result) == 0:
@ -152,7 +152,7 @@ class WPimport:
else: else:
print("La page {0} existe deja et mis à jour".format(title)) print("La page {0} existe deja et mis à jour".format(title))
post_id = result[0]["id"] post_id = result[0]["id"]
page = requests.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self.wordpress, post_id), auth=self.basic, data=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: if page.status_code == 200:
result = page.json() result = page.json()
print("Article mis à jour : {0}".format(result["title"]["raw"])) print("Article mis à jour : {0}".format(result["title"]["raw"]))
@ -160,7 +160,7 @@ class WPimport:
if page_exist == False: if page_exist == False:
page = requests.post("http://{0}/wp-json/wp/v2/posts".format(self.wordpress), auth=self.basic, data=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: if page.status_code == 201:
result = page.json() result = page.json()
print("Article ajoute : {0}".format(result["title"]["raw"])) print("Article ajoute : {0}".format(result["title"]["raw"]))