diff --git a/lib/WPImport.py b/lib/WPImport.py index 4bf897d..42d6596 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -16,6 +16,7 @@ class WPimport: self._parser = parser self._headers_json = {'Content-Type': 'application/json; charset=utf-8', 'Accept':'application/json'} self._protocol = "https" + self._directory = "backup" if ssl_wordpress is False: self._protocol = "http" self._request = requests.Session() @@ -60,6 +61,7 @@ class WPimport: def fromDirectory(self, directory="", number_thread=1, max_thread=1): + self._directory = directory directory = "{0}/archives".format(directory) directories = self._getDirectories([], "{0}".format(directory)) if len(directories) > 0: @@ -445,23 +447,38 @@ class WPimport: if len(img) > 0: href_a = i.get("href") href_img = img[0].get("src") + href_a_o = urlparse(href_a) + href_img_o = urlparse(href_img) new_img["old_src"]=href_img new_img["old_href"]=href_a try: - page_img = self._request.get(href_img) + if len(href_img_o.netloc) > 0: + img_ok = False + page_img = self._request.get(href_img) - if page_img.status_code == 404: - href_img = href_a - try: - page_img = self._request.get(href_a) - except ConnectionError as err: - self._logger.error("{0} : Connection error for get image : {1}".format(self._name, err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for get image : {1}".format(self._name, err)) - exit(1) + if page_img.status_code == 404: + href_img = href_a + try: + page_img = self._request.get(href_a) + if page_img.status_code == 200: + img_ok = True + except ConnectionError as err: + self._logger.error("{0} : Connection error for get image : {1}".format(self._name, err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get image : {1}".format(self._name, err)) + exit(1) + + else: + if os.path.exists("{0}/..{1}".format(self._directory, href_img)): + page_img = open("{0}/..{1}".format(self._directory, href_img), "r") + img_ok = True + else: + if os.path.exists("{0}/..{1}".format(self._directory, href_a)): + page_img = open("{0}/..{1}".format(self._directory, href_a), "r") + img_ok = True self._logger.debug("{0} : Status code for image {1} : {2}".format(self._name, href_img, page_img.status_code)) - if page_img.status_code == 200: + if img_ok is True: media=self._addOrUpdateMedia(href_img, page_img) new_img["id"]=media["id"] new_img["new_src"]=media["rendered"]