Add size of enclosure to RSS feed
This commit is contained in:
parent
947612ab28
commit
fa065648d7
3 changed files with 32 additions and 7 deletions
|
@ -36,6 +36,7 @@ from .utils import (
|
|||
extract_text,
|
||||
get_site_title,
|
||||
get_table_of_contents,
|
||||
get_url_mime_type,
|
||||
truncate_string,
|
||||
)
|
||||
|
||||
|
@ -189,6 +190,16 @@ class BaseContentPage(BasePage, MetadataMixin):
|
|||
def get_meta_image_url(self, request: HttpRequest) -> Optional[str]:
|
||||
return self.hero_url("regular", "|format-png")
|
||||
|
||||
def get_meta_image_mime(self) -> Optional[str]:
|
||||
if self.hero_unsplash_photo_id is not None:
|
||||
return get_url_mime_type(self.hero_url("regular"))
|
||||
|
||||
elif self.hero_image_id is not None:
|
||||
# We force these to PNG in `get_meta_image_url`
|
||||
return "image/png"
|
||||
|
||||
return None
|
||||
|
||||
def get_meta_title(self) -> str:
|
||||
return self.html_title
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
from dataclasses import dataclass
|
||||
from itertools import islice, pairwise
|
||||
from typing import Iterable, Type
|
||||
from typing import Iterable, Optional, Type
|
||||
|
||||
import requests
|
||||
from bs4 import BeautifulSoup, SoupStrainer
|
||||
from django.conf import settings
|
||||
from django.http.request import HttpRequest
|
||||
|
@ -107,3 +108,11 @@ def heading_id(heading: str) -> str:
|
|||
@django_cache_decorator(time=300)
|
||||
def get_site_title() -> str:
|
||||
return Site.objects.values_list("site_name", flat=True).first()
|
||||
|
||||
|
||||
@django_cache_decorator(time=21600)
|
||||
def get_url_mime_type(url: str) -> Optional[str]:
|
||||
try:
|
||||
return requests.head(url).headers.get("Content-Type")
|
||||
except requests.exceptions.RequestException:
|
||||
return None
|
||||
|
|
|
@ -115,17 +115,22 @@ class AllPagesFeed(Feed):
|
|||
return getattr(item, "summary", None) or item.title
|
||||
|
||||
def item_enclosure_url(self, item: BasePage) -> Optional[str]:
|
||||
if not hasattr(item, "hero_image_url"):
|
||||
if not hasattr(item, "get_meta_image_url"):
|
||||
return ""
|
||||
|
||||
hero_image_url = item.hero_image_url()
|
||||
image_url = item.get_meta_image_url(self.request)
|
||||
|
||||
if hero_image_url and hero_image_url.startswith("/"):
|
||||
return self.request.build_absolute_uri(hero_image_url)
|
||||
if image_url and image_url.startswith("/"):
|
||||
return self.request.build_absolute_uri(image_url)
|
||||
|
||||
return hero_image_url
|
||||
return image_url
|
||||
|
||||
def item_enclosure_mime_type(self, item: BasePage) -> str:
|
||||
if not hasattr(item, "get_meta_image_mime"):
|
||||
return ""
|
||||
|
||||
return item.get_meta_image_mime() or ""
|
||||
|
||||
item_enclosure_mime_type = ""
|
||||
item_enclosure_length = 0
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue