From fa065648d7ae213a9a5136d15422dbc6b009b232 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 4 Sep 2023 21:39:14 +0100 Subject: [PATCH] Add size of enclosure to RSS feed --- website/common/models.py | 11 +++++++++++ website/common/utils.py | 11 ++++++++++- website/common/views.py | 17 +++++++++++------ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/website/common/models.py b/website/common/models.py index a75f289..2558990 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -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 diff --git a/website/common/utils.py b/website/common/utils.py index e6789ab..15c3dee 100644 --- a/website/common/utils.py +++ b/website/common/utils.py @@ -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 diff --git a/website/common/views.py b/website/common/views.py index 80d8f07..9cac06c 100644 --- a/website/common/views.py +++ b/website/common/views.py @@ -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