add password parameter and fix post to delete 75%

This commit is contained in:
Valentin CZERYBA 2023-05-04 00:47:06 +02:00
parent 4789fe80aa
commit 21d2f35e6e
2 changed files with 26 additions and 18 deletions

View File

@ -96,6 +96,7 @@ if __name__ == '__main__':
import_parser = subparsers.add_parser("import") import_parser = subparsers.add_parser("import")
import_parser.add_argument("--user", help="wordpress user", required=True) 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("--file", help="HTML file", default="")
import_parser.add_argument("--directory", help="HTML directory", default="") import_parser.add_argument("--directory", help="HTML directory", default="")
import_parser.add_argument("--canalblog", help="URL Canalblog", 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") import_parser.add_argument("--serial", help="Serial execution", action="store_true")
export_parser = subparsers.add_parser("export") export_parser = subparsers.add_parser("export")
export_parser.add_argument("--url", help="canblog URL to be scraping", required=True) export_parser.add_argument("--url", help="canblog URL to be scraping", required=True)
@ -143,10 +145,12 @@ if __name__ == '__main__':
logger.addHandler(fileHandler) logger.addHandler(fileHandler)
if args.command == "import": if args.command == "import":
password = getpass() password = args.password
if len(password) == 0: if len(args.password) == 0:
logger.error("No password error !!! ") password = getpass()
exit(1) if len(password) == 0:
logger.error("No password error !!! ")
exit(1)
basic = HTTPBasicAuth(args.user, password) basic = HTTPBasicAuth(args.user, password)
wordpress = args.wordpress.split(",") wordpress = args.wordpress.split(",")

View File

@ -228,6 +228,8 @@ class WPimport:
data = page.content data = page.content
img_type = "image/{0}".format(img_type_file) 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)} headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)}
try: try:
r = self._request.post("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, headers=headers, data=data) 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() result = page.json()
if len(result) > 0: if len(result) > 0:
for i in result: for i in result:
self._logger.info("{0} : Page {1} already exist and going to delete".format(self._name, title)) self._logger.debug("{0} : Data for post to delete : {1}".format(self._name, i))
post_id = i["id"] if i["title"]["rendered"] == title:
try: self._logger.info("{0} : Page {1} already exist and going to delete".format(self._name, title))
params = {"force":1} post_id = i["id"]
page = self._request.delete("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id), auth=self._basic, headers=headers, params=params) try:
except Exception as err: params = {"force":1}
self._logger.error("{0} : Connection error for delete post : {1}".format(self._name, err)) page = self._request.delete("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id), auth=self._basic, headers=headers, params=params)
exit(1) except Exception as err:
if page.status_code == 200: self._logger.error("{0} : Connection error for delete post : {1}".format(self._name, err))
result = page.json() exit(1)
self._logger.info("{0} : Post deleted : {1}".format(self._name, title)) if page.status_code == 200:
result = page.json()
self._logger.info("{0} : Post deleted : {1}".format(self._name, title))
else: else:
self._logger.error("{0} : Post not deleted due status code : {1}".format(self._name, page.status_code)) 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)) self._logger.debug("{0} : {1}".format(self._name, page.content))
else: else:
self._logger.error("{0} : Connection for delete post error with status code : {1}".format(self._name, page.status_code)) 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)) self._logger.debug("{0} : {1}".format(self._name, page.content))