From a936857eb9b8dafd19bab524e60632570cfcf51a Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 18 Sep 2023 22:44:52 +0200 Subject: [PATCH 1/2] add method private link featured media --- lib/WPImport.py | 123 +++++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 60 deletions(-) diff --git a/lib/WPImport.py b/lib/WPImport.py index 118c102..2ed874b 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -417,6 +417,67 @@ class WPimport: subdirectory = self._getDirectories(sub, "{0}/{1}".format(item, i)) return subdirectory + + def _linkFeaturedMedia(self, img_src): + try: + page = self._request.get(img_src) + if page.status_code == 200: + name_img = img_src.replace("_q", "") + name_img = name_img.split("/")[len(name_img.split("/"))-1] + params = {"search": name_img} + try: + page = self._request.get("{1}://{0}/wp-json/wp/v2/media".format(self._wordpress, self._protocol), auth=self._basic, params=params) + if page.status_code == 200: + res = page.json() + if len(res) > 0: + id_media = res[0]["id"] + data = {"featured_media": id_media} + try: + r = self._request.post("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, result[0]["id"], self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + if r.status_code == 200: + self._logger.info("{0} : Add media featured : {1}".format(self._name, r.json()["title"]["raw"])) + else: + self._logger.error("{0} : Connection error with status code for featured media : {1}".format(self._name, r.status_code)) + self._logger.debug("{0} : {1}".format(self._name, r.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for post media featured : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for post media featured : {1}".format(self._name, err)) + else: + self._logger.info("{0} : No media found for {1}".format(self._name, h2)) + try: + page = self._request.get(img_src.replace("_q", "")) + if page.status_code == 200: + self._logger.info("{0} : Get image for media featured : {1}".format(self._name, img_src.replace("_q", ""))) + media = self._addOrUpdateMedia(img_src.replace("_q", ""), page) + if media["id"] != "": + self._linkFeaturedMedia(img_src.replace("_q", "")) + else: + self._logger.error("{0} : Connection error with status code for get image for featured media : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for get to image for featured media : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get to image media featured : {1}".format(self._name, err)) + else: + self._logger.error("{0} : Connection error with status code for search featured media: {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error search featured media : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error search featured media : {1}".format(self._name, err)) + else: + self._logger.error("{0} : Connection error for get featured media with status code : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for get featured media : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get featured media : {1}".format(self._name, err)) + ## Add or update featured media def _addOrUpdateFeaturedMedia(self, soup): @@ -429,69 +490,11 @@ class WPimport: if page.status_code == 200: result = page.json() if len(result) > 0: - if h2 == result[0]["title"]: + if h2 == self._replaceCaracter(result[0]["title"]): img = i.find_all("img") if len(img) > 0: img_src = img[0].get("src") - try: - page = self._request.get(img_src) - if page.status_code == 200: - name_img = img_src.replace("_q", "") - name_img = name_img.split("/")[len(name_img.split("/"))-1] - params = {"search": name_img} - try: - page = self._request.get("{1}://{0}/wp-json/wp/v2/media".format(self._wordpress, self._protocol), auth=self._basic, params=params) - - if page.status_code == 200: - res = page.json() - if len(res) > 0: - id_media = res[0]["id"] - data = {"featured_media": id_media} - try: - r = self._request.post("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, result[0]["id"], self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) - if r.status_code == 200: - self._logger.info("{0} : Add media featured : {1}".format(self._name, r.json()["title"]["raw"])) - else: - self._logger.error("{0} : Connection error with status code for featured media : {1}".format(self._name, r.status_code)) - self._logger.debug("{0} : {1}".format(self._name, r.content)) - except ConnectionError as err: - self._logger.error("{0} : Connection error for post media featured : {1}".format(self._name, err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for post media featured : {1}".format(self._name, err)) - else: - self._logger.info("{0} : No media found for {1}".format(self._name, h2)) - try: - page = self._request.get(img_src.replace("_q", "")) - if page.status_code == 200: - self._logger.info("{0} : Get image for media featured : {1}".format(self._name, img_src.replace("_q", ""))) - media = self._addOrUpdateMedia(img_src.replace("_q", ""), page) - if media["id"] != "": - self._addOrUpdateFeaturedMedia(soup) - else: - self._logger.error("{0} : Connection error with status code for get image for featured media : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - except ConnectionError as err: - self._logger.error("{0} : Connection error for get to image for featured media : {1}".format(self._name, err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for get to image media featured : {1}".format(self._name, err)) - else: - self._logger.error("{0} : Connection error with status code for search featured media: {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - except ConnectionError as err: - self._logger.error("{0} : Connection error search featured media : {1}".format(self._name, err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error search featured media : {1}".format(self._name, err)) - else: - self._logger.error("{0} : Connection error for get featured media with status code : {1}".format(self._name, page.status_code)) - self._logger.debug("{0} : {1}".format(self._name, page.content)) - except ConnectionError as err: - self._logger.error("{0} : Connection error for get featured media : {1}".format(self._name, err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for get featured media : {1}".format(self._name, err)) + self._linkFeaturedMedia(img_src) else: self._logger.error("{0} : Connection error with status code for featured media : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content)) From 19f96abb2d5e1aae251b2fd2ec929f490bedefca Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 18 Sep 2023 22:58:36 +0200 Subject: [PATCH 2/2] add no-menu and fix method linkFeaturedMedia --- import_export_canalblog.py | 21 +++++++++++++-------- lib/WPImport.py | 6 +++--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/import_export_canalblog.py b/import_export_canalblog.py index 918d5b1..ab63e0e 100644 --- a/import_export_canalblog.py +++ b/import_export_canalblog.py @@ -170,6 +170,8 @@ if __name__ == '__main__': import_parser.add_argument("--no-create", help="No create post", dest="create", default="store_false", action="store_true") import_parser.add_argument("--no-update", help="No update post", dest="update", default="store_false", action="store_true") import_parser.add_argument("--no-image", help="No image add or update", dest="image", default="store_false", action="store_true") + import_parser.add_argument("--no-menu", help="No menu add or update", dest="menu", default="store_false", action="store_true") + import_parser.add_argument("--author", dest="author", help="Define author", default="") @@ -264,8 +266,9 @@ if __name__ == '__main__': for i in wordpress: importWp.setUrl(i) importWp.fromFile(files=args.file.split(",")) - menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress) - menuWp.fromFile("{0}".format(args.file.split(",")[0])) + if args.menu is False: + menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress) + menuWp.fromFile("{0}".format(args.file.split(",")[0])) if len(args.directory) > 0: try: with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex: @@ -276,9 +279,10 @@ if __name__ == '__main__': ex.submit(importDirectory, i, int(args.parallel), args.directory, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, args.create, args.update, args.image, args.revert, args.author) for i in range(0, int(args.parallel)) ] - wait(wait_for, return_when=ALL_COMPLETED) - menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress) - menuWp.fromFile("{0}/index.html".format(args.directory)) + if args.menu is False: + wait(wait_for, return_when=ALL_COMPLETED) + menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress) + menuWp.fromFile("{0}/index.html".format(args.directory)) except Exception as err: logger.error("Threading error : {0}".format(err)) if len(args.canalblog) > 0: @@ -291,9 +295,10 @@ if __name__ == '__main__': ex.submit(importUrl, i, int(args.parallel), args.canalblog, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, ssl_canalblog, args.create, args.update, args.image, args.revert, args.tmp, args.author) for i in range(0, int(args.parallel)) ] - wait(wait_for, return_when=ALL_COMPLETED) - menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress) - menuWp.fromUrl(args.canalblog) + if args.menu is False: + wait(wait_for, return_when=ALL_COMPLETED) + menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress) + menuWp.fromUrl(args.canalblog) diff --git a/lib/WPImport.py b/lib/WPImport.py index 2ed874b..efd936e 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -418,7 +418,7 @@ class WPimport: return subdirectory - def _linkFeaturedMedia(self, img_src): + def _linkFeaturedMedia(self, img_src, h2, result): try: page = self._request.get(img_src) if page.status_code == 200: @@ -452,7 +452,7 @@ class WPimport: self._logger.info("{0} : Get image for media featured : {1}".format(self._name, img_src.replace("_q", ""))) media = self._addOrUpdateMedia(img_src.replace("_q", ""), page) if media["id"] != "": - self._linkFeaturedMedia(img_src.replace("_q", "")) + self._linkFeaturedMedia(img_src.replace("_q", ""), h2) else: self._logger.error("{0} : Connection error with status code for get image for featured media : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content)) @@ -494,7 +494,7 @@ class WPimport: img = i.find_all("img") if len(img) > 0: img_src = img[0].get("src") - self._linkFeaturedMedia(img_src) + self._linkFeaturedMedia(img_src, h2, result) else: self._logger.error("{0} : Connection error with status code for featured media : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content))