add album test wip

This commit is contained in:
Valentin CZERYBA 2023-08-17 00:18:45 +02:00
parent 5959ab5b2e
commit 418bea3778
2 changed files with 40 additions and 63 deletions

View File

@ -263,8 +263,9 @@ if __name__ == '__main__':
for i in wordpress:
importWp.setUrl(i)
importWp.fromFile(files=args.file.split(","))
exit(0)
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.file.split(",")[0]))
menuWp.fromFile("{0}".format(args.file.split(",")[0]))
if len(args.directory) > 0:
try:
with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex:

View File

@ -120,73 +120,49 @@ class WPimport:
albumtitle = albumbody.find("h2").get_text()
self._logger.debug("{0} : Title of the album : {1}".format(self._name, albumtitle))
albumdesc = albumbody.find("div", class_="albumdesc").find("p")
img_a = albumbody.find_all("a", {"target": "_blank"})
img_a = albumbody.find_all("img")
list_img = []
if self._no_image is False:
self._logger.debug("{0} : Number of image's tag : {1}".format(self._name, len(img_a)))
for i in img_a:
new_img = {}
img = i.find_all("img")
self._logger.debug("{0} : Number of image's tag : {1}".format(self._name, len(img)))
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:
if len(href_img_o.netloc) > 0:
img_ok = False
page_img = self._request.get(href_img)
href_img = i.get("src")
href_img_o = urlparse(href_img)
new_img["old_src"]=href_img
try:
if len(href_img_o.netloc) > 0:
img_ok = False
page_img = self._request.get(href_img)
if page_img.status_code == 200:
img_ok = True
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
self._logger.debug("{0} : Status code for image {1} : {2}".format(self._name, href_img, page_img.status_code))
if img_ok is True:
media=self._addOrUpdateMedia(href_img, page_img)
new_img["id"]=media["id"]
new_img["new_src"]=media["rendered"]
list_img.append(new_img)
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)
content_html = ""
if len(list_img) > 0:
content_html = "<!-- wp:paragraph --><p>{0}</p><!-- /wp:paragraph -->\n\n".format(albumdesc)
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)
content_html = content_html + "<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped\"><!-- wp:image {'id':{0},\"sizeSlug\":\"large\",\"linkDestination\":\"none\"} --><figure class=\"wp-block-image size-large'><img src=\"{1}\" alt=\"\" class=\"wp-image-{0}\"/></figure><!-- /wp:image --> \n\n".format(list_img[0]["id"],list_img[1]["new_src"])
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 img_ok is True:
media=self._addOrUpdateMedia(href_img, page_img)
new_img["id"]=media["id"]
new_img["new_src"]=media["rendered"]
list_img.append(new_img)
if href_img != href_a:
media=self._addOrUpdateMedia(href_a, page_img)
new_img["id"]=media["id"]
new_img["new_src"]=media["rendered"]
list_img.append(new_img)
if page_img.status_code not in [200, 404]:
self._logger.error("{0} : Connection error with status code for get image : {1}".format(self._name, page_img.status_code))
self._logger.debug("{0} : {1}".format(self._name, page_img.content))
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)
content_html = "<!-- wp:paragraph --><p>{0}</p><!-- /wp:paragraph -->\n\n".format(albumdesc)
for i in range(1, len(list_img)):
content_html = content_html + "<!-- wp:image {'id':{0},\"sizeSlug\":\"large\",\"linkDestination\":\"none\"} --><figure class=\"wp-block-image size-large\"><img src='{0}' alt=\"\" class='wp-image-{1}'/></figure><!-- /wp:image -->\n\n".format(list_img[i]["id"],list_img[i]["new_src"])
content_html = content_html + "<!-- /wp:gallery -->"
content_html = content_html + "<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped\"><!-- wp:image {'id':{0},\"sizeSlug\":\"large\",\"linkDestination\":\"none\"} --><figure class=\"wp-block-image size-large'><img src=\"{1}\" alt=\"\" class=\"wp-image-{0}\"/></figure><!-- /wp:image --> \n\n".format(list_img[0]["id"],list_img[1]["new_src"])
for i in range(1, len(list_img)):
content_html = content_html + "<!-- wp:image {'id':{0},\"sizeSlug\":\"large\",\"linkDestination\":\"none\"} --><figure class=\"wp-block-image size-large\"><img src='{0}' alt=\"\" class='wp-image-{1}'/></figure><!-- /wp:image -->\n\n".format(list_img[i]["id"],list_img[i]["new_src"])
content_html = content_html + "<!-- /wp:gallery -->"
self._logger.info("{0} : content html : {1}".format(self._name, content_html))