diff --git a/lib/WPImport.py b/lib/WPImport.py index 3f343cc..d13fe60 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -39,20 +39,22 @@ class WPimport: for i in range(0, len(webpage)): try: r = self._request.get(webpage[i]) - except Exception as err: + if r.status_code == 200: + self._logger.info("{0} : ({1}/{2}) : Page is importing : {3}".format(self._name, i+1, len(webpage), webpage[i])) + soup = BeautifulSoup(r.content, self._parser) + articlebody = soup.find_all("div", class_="articlebody") + if len(articlebody) > 0: + self._addOrUpdatePost(soup) + else: + self._addOrUpdateFeaturedMedia(soup) + else: + self._logger.error("{0} : Connection error for get url {1} with status code : {2}".format(self._name, webpage[i], r.status_code)) + self._logger.debug("{0} : {1}".format(self._name, r.content)) + except ConnectionError as err: self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, webpage[i], err)) exit(1) - if r.status_code == 200: - self._logger.info("{0} : ({1}/{2}) : Page is importing : {3}".format(self._name, i+1, len(webpage), webpage[i])) - soup = BeautifulSoup(r.content, self._parser) - articlebody = soup.find_all("div", class_="articlebody") - if len(articlebody) > 0: - self._addOrUpdatePost(soup) - else: - self._addOrUpdateFeaturedMedia(soup) - else: - self._logger.error("{0} : Connection error for get url {1} with status code : {2}".format(self._name, webpage[i], r.status_code)) - self._logger.debug("{0} : {1}".format(self._name, r.content)) + except Exception as err: + self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, webpage[i], err)) def fromDirectory(self, directory="", number_thread=1, max_thread=1): @@ -137,60 +139,65 @@ class WPimport: params = {"search":h2, "type":"post"} try: page = self._request.get("{1}://{0}/wp-json/wp/v2/search".format(self._wordpress, self._protocol), auth=self._basic, params=params) - except Exception as err: + if page.status_code == 200: + result = page.json() + if len(result) > 0: + if h2 == 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)) + 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)) + 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)) + except ConnectionError as err: self._logger.error("{0} : Connection error : {1}".format(self._name, err)) exit(1) - if page.status_code == 200: - result = page.json() - if len(result) > 0: - if h2 == 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) - except Exception as err: - self._logger.error("{0} : Connection error for get featured media : {1}".format(self._name, err)) - exit(1) - 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) - except Exception as err: - self._logger.error("{0} : Connection error search featured media : {1}".format(self._name, err)) - exit(1) - 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)) - except Exception as err: - self._logger.error("{0} : Connection error for post media featured : {1}".format(self._name, err)) - exit(1) - 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)) - - else: - self._logger.info("{0} : No media found for {1}".format(self._name, h2)) - 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)) - - 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)) - - 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)) - + except Exception as err: + self._logger.error("{0} : Connection error : {1}".format(self._name, err)) ## Association image to post