add remove all

This commit is contained in:
Valentin CZERYBA 2023-05-14 18:35:36 +02:00
parent 3d7aa19441
commit ece4d78dd8
2 changed files with 61 additions and 23 deletions

View File

@ -113,6 +113,7 @@ if __name__ == '__main__':
import_parser.add_argument("--canalblog", help="URL Canalblog", default="") import_parser.add_argument("--canalblog", help="URL Canalblog", default="")
import_parser.add_argument("--wordpress", help="URL Wordpress", required=True) import_parser.add_argument("--wordpress", help="URL Wordpress", required=True)
import_parser.add_argument("--serial", help="Serial execution", action="store_true") import_parser.add_argument("--serial", help="Serial execution", action="store_true")
import_parser.add_argument("--remove", help="Remove all articles", action="store_true")
@ -165,12 +166,16 @@ if __name__ == '__main__':
basic = HTTPBasicAuth(args.user, password) basic = HTTPBasicAuth(args.user, password)
wordpress = args.wordpress.split(",") wordpress = args.wordpress.split(",")
importWp = WPimport(basic, "", logger, args.parser) importWp = WPimport(basic=basic, wordpress="", logger=logger, parser=args.parser)
if len(args.file) > 0: if len(args.file) > 0:
for i in wordpress: for i in wordpress:
importWp.setUrl(i) importWp.setUrl(i)
importWp.fromFile(files=args.file.split(",")) importWp.fromFile(files=args.file.split(","))
if len(args.directory) > 0: if len(args.directory) > 0:
if args.remove:
for i in args.wordpress.split(","):
importWp.setUrl(i)
importWp.removeAll()
try: try:
with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex: with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex:
wait_for = [ wait_for = [
@ -180,6 +185,10 @@ if __name__ == '__main__':
except Exception as err: except Exception as err:
logger.error("Threading error : {0}".format(err)) logger.error("Threading error : {0}".format(err))
if len(args.canalblog) > 0: if len(args.canalblog) > 0:
if args.remove:
for i in args.wordpress.split(","):
importWp.setUrl(i)
importWp.removeAll()
try: try:
with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex: with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex:
wait_for = [ wait_for = [

View File

@ -5,7 +5,6 @@ from urllib.parse import urlparse
import requests, os, logging, re, json import requests, os, logging, re, json
from requests.adapters import HTTPAdapter from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry from requests.packages.urllib3.util.retry import Retry
from slugify import slugify
class WPimport: class WPimport:
# Constructor # Constructor
@ -87,6 +86,35 @@ class WPimport:
else: else:
self._addOrUpdateFeaturedMedia(soup) self._addOrUpdateFeaturedMedia(soup)
def removeAll(self):
params = {"per_page":100}
try:
self._logger.info("{0} : List posts to remove for url : {1}".format(self._name, self._wordpress))
r = self._request.get("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, params=params, headers=self._headers_json)
except Exception as err:
self._logger.error("{0} : Connection error for list post to remove : {1}".format(self._name, err))
if r.status_code == 200:
result = r.json()
if len(result) > 0:
for i in result:
self._logger.info("{0} : Remove article for url {1} : {2}".format(self._name, self._wordpress, i["title"]["rendered"]))
params = {"force":1}
try:
r = self._request.delete("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, i["id"]), auth=self._basic, headers=self._headers_json , params=params)
if r.status_code == 200:
self._logger.info("{0} : Post removed for URL {1} : {2}".format(self._name, self._wordpress, i["title"]["rendered"]))
else:
self._logger.error("{0} : Connection error for post {1} with status code {2}".format(self._name, self._wordpress, i["title"]["rendered"]))
except Exception as err:
self._logger.error("{0} : Connection error for post remove : {1}".format(self._name, err))
exit(1)
self.removeAll()
else:
self._logger.error("{0} : Error for list to remove due status code {1}".format(self._name, r.status_code))
self._logger.debug("{0} : Content error : {1}".format(self._name, r.content))
# Private method # Private method
## Get all files ## Get all files
@ -489,6 +517,7 @@ class WPimport:
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}
try: try:
self._logger.info("{0} : Get author : {1}".format(self._name, author))
page = self._request.get("http://{0}/wp-json/wp/v2/users".format(self._wordpress), auth=self._basic, params=params) page = self._request.get("http://{0}/wp-json/wp/v2/users".format(self._wordpress), auth=self._basic, params=params)
except Exception as err: except Exception as err:
self._logger.error("{0} : Connection error for get author : {1}".format(self._name, err)) self._logger.error("{0} : Connection error for get author : {1}".format(self._name, err))
@ -499,15 +528,13 @@ class WPimport:
else: else:
self._logger.error("{0} : Connection error with status code for get author : {1}".format(self._name, page.status_code)) self._logger.error("{0} : Connection error with status code for get author : {1}".format(self._name, page.status_code))
self._logger.debug("{0} : {1}".format(page.content)) self._logger.debug("{0} : {1}".format(page.content))
slug = slugify(title) params = {"search": title}
params = {"slug":slug}
try: try:
page = self._request.get("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, params=params) page = self._request.get("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, params=params)
except Exception as err: except Exception as err:
self._logger.error("{0} : Connection error for search post : {1}".format(self._name, err)) self._logger.error("{0} : Connection error for search post : {1}".format(self._name, err))
exit(1) exit(1)
headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
page_is_exist = True page_is_exist = True
if page.status_code == 200: if page.status_code == 200:
result = page.json() result = page.json()
@ -515,29 +542,31 @@ class WPimport:
page_is_exist = False page_is_exist = False
else: else:
for i in result: for i in result:
self._logger.debug("{0} : Data for post to update : {1}".format(self._name, i)) if i["title"]["rendered"] == title:
self._logger.info("{0} : Page {1} already exist and going to update".format(self._name, title)) post_id = i["id"]
post_id = i["id"] self._logger.debug("{0} : Data for post to update : {1}".format(self._name, result[0]))
try: self._logger.info("{0} : Page {1} already exist and going to update".format(self._name, title))
page = self._request.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id), auth=self._basic, headers=headers, data=json.dumps(data))
except Exception as err: try:
self._logger.error("{0} : Connection error for update post : {1}".format(self._name, err)) page = self._request.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id), auth=self._basic, headers=self._headers_json, data=json.dumps(data))
exit(1) except Exception as err:
if page.status_code == 200: self._logger.error("{0} : Connection error for update post : {1}".format(self._name, err))
result = page.json() exit(1)
self._logger.info("{0} : Post updated : {1}".format(self._name, title)) if page.status_code == 200:
self._addOrUpdateComment(result["id"], comment_post, result["title"]["raw"]) result = page.json()
self._linkImgPost(result["title"]["raw"], list_img, result["id"]) self._logger.info("{0} : Post updated : {1}".format(self._name, title))
else: self._addOrUpdateComment(result["id"], comment_post, result["title"]["raw"])
self._logger.error("{0} : Post not updated due status code : {1}".format(self._name, page.status_code)) self._linkImgPost(result["title"]["raw"], list_img, result["id"])
self._logger.debug("{0} : {1}".format(self._name, page.content)) else:
self._logger.error("{0} : Post not updated due status code : {1}".format(self._name, page.status_code))
self._logger.debug("{0} : {1}".format(self._name, page.content))
else: else:
self._logger.error("{0} : Connection for update post error with status code : {1}".format(self._name, page.status_code)) self._logger.error("{0} : Connection for update post error with status code : {1}".format(self._name, page.status_code))
self._logger.debug("{0} : {1}".format(self._name, page.content)) self._logger.debug("{0} : {1}".format(self._name, page.content))
if page_is_exist == False: if page_is_exist == False:
try: try:
page = self._request.post("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, headers=headers, data=json.dumps(data)) page = self._request.post("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, headers=self._headers_json, data=json.dumps(data))
except Exception as err: except Exception as err:
self._logger.error("{0} : Connection error for create post : {1}".format(self._name, err)) self._logger.error("{0} : Connection error for create post : {1}".format(self._name, err))
exit(1) exit(1)