Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: if social card not found, try to retrieve length from remote URL #225

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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