Skip to content

Commit

Permalink
improvement: if social card not found, try to retrieve length from re…
Browse files Browse the repository at this point in the history
…mote URL (#225)

If the social card has been previously published, it works.
  • Loading branch information
Guts authored Dec 17, 2023
2 parents e505ce2 + 132f9a9 commit 93564f4
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,13 @@ def get_image(self, in_page: Page, base_url: str) -> Optional[Tuple[str, str, in
if img_local_path.is_file():
img_length = img_local_path.stat().st_size
else:
logger.debug(f"Social card: {img_local_path} still not exists.")
img_length = None
logger.debug(
f"Social card: {img_local_path} still not exists. Trying to "
f"retrieve length from remote image: {img_url}"
"Note that would work only if the social card image has been "
"published before)."
)
img_length = self.get_remote_image_length(image_url=img_url)

return (
img_url,
Expand Down Expand Up @@ -575,7 +580,7 @@ def get_remote_image_length(
http_method: str = "HEAD",
attempt: int = 0,
ssl_context: ssl.SSLContext = None,
) -> int:
) -> Optional[int]:
"""Retrieve length for remote images (starting with 'http' \
in meta.image or meta.illustration). \
It tries to perform a HEAD request and get the length from the headers. \
Expand All @@ -591,7 +596,7 @@ def get_remote_image_length(
:type ssl_context: ssl.SSLContext, optional
:return: image length as str or None
:rtype: int
:rtype: Optional[int]
"""
# prepare request
req = request.Request(
Expand Down

0 comments on commit 93564f4

Please sign in to comment.