From f69298179ad75ebc0d0e680500d6256560066766 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 23 May 2023 13:45:59 +0200 Subject: [PATCH] reduce line code and add private method --- lib/WPImport.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/WPImport.py b/lib/WPImport.py index bf04c98..b2af6d8 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -90,6 +90,23 @@ class WPimport: # Private method + ## replace caracter + + def _replaceCaracter(self, title_rendered): + list_replace = {'’': "'", '–': '-', '…': '...', '« ': '"', ' »': '"', '« ': '"', ' »': '"', '’': "'", '"‘': "'"} + for old, new in list_replace.items(): + title_rendered = title_rendered.replace(old, new) + return title_rendered + + ## remove space + + def _removeSpace(self, title): + if title[len(title)-1] == " ": + title = title[:-1] + if title[0] == " ": + title = title[1:] + return title + ## Get all files def _getFiles(self, item): @@ -504,10 +521,7 @@ class WPimport: else: 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)) - if title[len(title)-1] == " ": - title = title[:-1] - if title[0] == " ": - title = title[1:] + title = self._removeSpace(title) page_is_exist = False for index in range(1,10): @@ -529,14 +543,10 @@ class WPimport: self._logger.info("{0} : Number result posts : {1}".format(self._name, len(result))) count = 0 for i in result: - self._logger.info("{0} : Search title posts for |{2}| : |{1}|".format(self._name, i["title"]["rendered"], title)) - if len(i["title"]["rendered"]) == len(title): - title_rendered = i["title"]["rendered"] - else: - title_rendered = i["title"]["rendered"] - list_replace = {'’': "'", '–': '-', '…': '...', '« ': '"', ' »': '"', '« ': '"', ' »': '"', '’': "'", '"‘': "'"} - for old, new in list_replace.items(): - title_rendered = title_rendered.replace(old, new) + title_rendered = i["title"]["rendered"] + self._logger.info("{0} : Search title posts for |{2}| : |{1}|".format(self._name, title_rendered, title)) + if len(title_rendered) != len(title): + title_rendered = self._replaceCaracter(title_rendered) self._logger.debug("{0} : Search title posts for |{2}| : |{1}|".format(self._name, title_rendered, title)) self._logger.debug("{0} : SIze of title : {1} - {2}".format(self._name, len(title), len(title_rendered))) if title_rendered == title: