From 9e7e1b27fd8ea69fd760902b074fede33cf97eaa Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 11 Jun 2023 20:24:22 +0200 Subject: [PATCH] change WIP test --- lib/WPChange.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/WPChange.py b/lib/WPChange.py index 255e9c1..c08b5f0 100644 --- a/lib/WPChange.py +++ b/lib/WPChange.py @@ -21,14 +21,16 @@ class WPChange: def fromFile(self, files=[], number_thread=1, max_thread=1): divFiles = int(len(files) / max_thread) - currentRangeFiles = int(divFiles * (number_thread+1)) + currentRangeFiles = int(divFiles * (number_thread)) firstRange = int(currentRangeFiles - divFiles) self._logger.debug("{0} : index : {1}".format(self._name,number_thread)) + self._logger.debug("{0} : first range : {1}".format(self._name,firstRange)) self._logger.debug("{0} : last range : {1}".format(self._name,currentRangeFiles)) for i in range(firstRange, currentRangeFiles): + if os.path.exists(files[i]): self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, currentRangeFiles + 1, files[i])) self._change(files[i]) @@ -72,12 +74,27 @@ class WPChange: ## Change path img file def _change(self, file): - with open(file, 'r') as f: - content = f.read() - soup = BeautifulSoup(content, self._parser) - img = soup.find_all("img") - for i in img: - src = i.get("src") - self._logger.info(src) + try: + with open(file, 'r') as f: + content = f.read() + soup = BeautifulSoup(content, self._parser) + img = soup.find_all("img") + for i in img: + src = i.get("src") + o = urlparse(src) + if len(o.netloc) > 0: + self._logger.info("{0} : Change source {1} /dists/img/{2}/{3}".format(self._name, src, o.netloc, o.path)) + content = content.replace(src, "/dists/img/{0}/{1}".format(o.netloc, o.path)) + try: + with open(file, "w") as f: + self._logger.info("{0} : File write : {1}".format(self._name, file)) + f.write(content) + except Exception as ex: + self._logger.error("{0} : Error for write file {1} : {2}".format(self._name, file, ex)) + + except Exception as ex: + self._logger.error("{0} : Error for read file {1} : {2}".format(self._name, file, ex)) + +