separate class file for WPimport
This commit is contained in:
parent
6f7504e669
commit
481fc40929
20
WPImport.py
20
WPImport.py
@ -23,7 +23,7 @@ class WPimport:
|
||||
def fromFile(self, files):
|
||||
for file in files:
|
||||
if os.path.exists(file):
|
||||
logger.info("Fichier en cours de traitement : {0}".format(file))
|
||||
self._logger.info("Fichier en cours de traitement : {0}".format(file))
|
||||
with open(file, 'r') as f:
|
||||
content = f.read()
|
||||
soup = BeautifulSoup(content, 'html.parser')
|
||||
@ -84,9 +84,9 @@ class WPimport:
|
||||
data = {"featured_media": id_media}
|
||||
r = requests.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, result[0]["id"]), auth=self._basic, headers=headers, data=json.dumps(data))
|
||||
if r.status_code == 200:
|
||||
logger.info("Ajout media featured : {0}".format(r.json()["title"]["raw"]))
|
||||
self._logger.info("Ajout media featured : {0}".format(r.json()["title"]["raw"]))
|
||||
else:
|
||||
logger.info("Aucun media trouvé pour {0}".format(h2))
|
||||
self._logger.info("Aucun media trouvé pour {0}".format(h2))
|
||||
|
||||
## Association image to post
|
||||
|
||||
@ -95,7 +95,7 @@ class WPimport:
|
||||
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:
|
||||
logger.info("Association d'une image à l'article {0}".format(title))
|
||||
self._logger.info("Association d'une image à l'article {0}".format(title))
|
||||
|
||||
## Add or update img
|
||||
|
||||
@ -111,7 +111,7 @@ class WPimport:
|
||||
params = {"force":1}
|
||||
r = requests.delete("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, res[0]["id"]), auth=self._basic, params=params)
|
||||
if r.status_code == 200:
|
||||
logger.info("Image supprimé {0}".format(img_name))
|
||||
self._logger.info("Image supprimé {0}".format(img_name))
|
||||
data = page.content
|
||||
img_type = "image/png"
|
||||
if img_name.split(".")[1] == "jpg" or img_name.split(".")[1] == "jpeg":
|
||||
@ -119,7 +119,7 @@ class WPimport:
|
||||
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:
|
||||
logger.info("Ajout d'image {0}".format(img_name))
|
||||
self._logger.info("Ajout d'image {0}".format(img_name))
|
||||
res = r.json()
|
||||
media["id"] = res["id"]
|
||||
media["rendered"] = res["guid"]["rendered"]
|
||||
@ -241,12 +241,12 @@ class WPimport:
|
||||
if len(result) == 0:
|
||||
page_exist = False
|
||||
else:
|
||||
logger.info("La page {0} existe deja et mis à jour".format(title))
|
||||
self._logger.info("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))
|
||||
if page.status_code == 200:
|
||||
result = page.json()
|
||||
logger.info("Article mis à jour : {0}".format(result["title"]["raw"]))
|
||||
self._logger.info("Article mis à jour : {0}".format(result["title"]["raw"]))
|
||||
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
|
||||
|
||||
|
||||
@ -255,12 +255,12 @@ class WPimport:
|
||||
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()
|
||||
logger.info("Article ajoute : {0}".format(result["title"]["raw"]))
|
||||
self._logger.info("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)
|
||||
if page.status_code == 201:
|
||||
logger.info("Commentaire ajoute pour {0}".format(result["title"]["raw"]))
|
||||
self._logger.info("Commentaire ajoute pour {0}".format(result["title"]["raw"]))
|
||||
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
|
||||
|
||||
|
||||
|
@ -46,7 +46,7 @@ if __name__ == '__main__':
|
||||
exit(1)
|
||||
|
||||
basic = HTTPBasicAuth(args.user, password)
|
||||
importWp = WPimport(basic, args.wordpress, logger)
|
||||
importWp = WPImport.WPimport(basic, args.wordpress, logger)
|
||||
if len(args.file) > 0:
|
||||
importWp.fromFile(args.file.split(","))
|
||||
exit(0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user