From 21d2f35e6ed2a0a3b19ba3b4fe883bc4c3c4037e Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 4 May 2023 00:47:06 +0200 Subject: [PATCH] add password parameter and fix post to delete 75% --- import_export_canalblog.py | 12 ++++++++---- lib/WPImport.py | 32 ++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/import_export_canalblog.py b/import_export_canalblog.py index 8250a9e..b44ab9c 100644 --- a/import_export_canalblog.py +++ b/import_export_canalblog.py @@ -96,6 +96,7 @@ if __name__ == '__main__': import_parser = subparsers.add_parser("import") import_parser.add_argument("--user", help="wordpress user", required=True) + import_parser.add_argument("--password", help="password wordpress's user", default="") import_parser.add_argument("--file", help="HTML file", default="") import_parser.add_argument("--directory", help="HTML directory", default="") import_parser.add_argument("--canalblog", help="URL Canalblog", default="") @@ -103,6 +104,7 @@ if __name__ == '__main__': import_parser.add_argument("--serial", help="Serial execution", action="store_true") + export_parser = subparsers.add_parser("export") export_parser.add_argument("--url", help="canblog URL to be scraping", required=True) @@ -143,10 +145,12 @@ if __name__ == '__main__': logger.addHandler(fileHandler) if args.command == "import": - password = getpass() - if len(password) == 0: - logger.error("No password error !!! ") - exit(1) + password = args.password + if len(args.password) == 0: + password = getpass() + if len(password) == 0: + logger.error("No password error !!! ") + exit(1) basic = HTTPBasicAuth(args.user, password) wordpress = args.wordpress.split(",") diff --git a/lib/WPImport.py b/lib/WPImport.py index 50b58b6..6f82ec4 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -228,6 +228,8 @@ class WPimport: data = page.content img_type = "image/{0}".format(img_type_file) + if img_type_file == "jpg": + img_type = "image/jpeg" headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)} try: r = self._request.post("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, headers=headers, data=data) @@ -509,21 +511,23 @@ class WPimport: result = page.json() if len(result) > 0: for i in result: - self._logger.info("{0} : Page {1} already exist and going to delete".format(self._name, title)) - post_id = i["id"] - try: - params = {"force":1} - page = self._request.delete("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id), auth=self._basic, headers=headers, params=params) - except Exception as err: - self._logger.error("{0} : Connection error for delete post : {1}".format(self._name, err)) - exit(1) - if page.status_code == 200: - result = page.json() - self._logger.info("{0} : Post deleted : {1}".format(self._name, title)) + self._logger.debug("{0} : Data for post to delete : {1}".format(self._name, i)) + if i["title"]["rendered"] == title: + self._logger.info("{0} : Page {1} already exist and going to delete".format(self._name, title)) + post_id = i["id"] + try: + params = {"force":1} + page = self._request.delete("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id), auth=self._basic, headers=headers, params=params) + except Exception as err: + self._logger.error("{0} : Connection error for delete post : {1}".format(self._name, err)) + exit(1) + if page.status_code == 200: + result = page.json() + self._logger.info("{0} : Post deleted : {1}".format(self._name, title)) - else: - self._logger.error("{0} : Post not deleted due status code : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) + else: + self._logger.error("{0} : Post not deleted due status code : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) else: self._logger.error("{0} : Connection for delete post error with status code : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content))