Add size of enclosure to RSS feed

This commit is contained in:
Jake Howard 2023-09-04 21:39:14 +01:00
parent 947612ab28
commit fa065648d7
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 32 additions and 7 deletions

View file

@ -36,6 +36,7 @@ from .utils import (
extract_text, extract_text,
get_site_title, get_site_title,
get_table_of_contents, get_table_of_contents,
get_url_mime_type,
truncate_string, truncate_string,
) )
@ -189,6 +190,16 @@ class BaseContentPage(BasePage, MetadataMixin):
def get_meta_image_url(self, request: HttpRequest) -> Optional[str]: def get_meta_image_url(self, request: HttpRequest) -> Optional[str]:
return self.hero_url("regular", "|format-png") 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: def get_meta_title(self) -> str:
return self.html_title return self.html_title

View file

@ -1,7 +1,8 @@
from dataclasses import dataclass from dataclasses import dataclass
from itertools import islice, pairwise from itertools import islice, pairwise
from typing import Iterable, Type from typing import Iterable, Optional, Type
import requests
from bs4 import BeautifulSoup, SoupStrainer from bs4 import BeautifulSoup, SoupStrainer
from django.conf import settings from django.conf import settings
from django.http.request import HttpRequest from django.http.request import HttpRequest
@ -107,3 +108,11 @@ def heading_id(heading: str) -> str:
@django_cache_decorator(time=300) @django_cache_decorator(time=300)
def get_site_title() -> str: def get_site_title() -> str:
return Site.objects.values_list("site_name", flat=True).first() 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

View file

@ -115,17 +115,22 @@ class AllPagesFeed(Feed):
return getattr(item, "summary", None) or item.title return getattr(item, "summary", None) or item.title
def item_enclosure_url(self, item: BasePage) -> Optional[str]: 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 "" 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("/"): if image_url and image_url.startswith("/"):
return self.request.build_absolute_uri(hero_image_url) 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 item_enclosure_length = 0