From b557035eac3b118e2d4f3b0b7b9050093ef0e1fc Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 31 Jul 2022 15:27:12 +0200 Subject: [PATCH] Make site_name optional of OG metadata --- app/templates/utils.html | 4 +++- app/utils/opengraph.py | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/templates/utils.html b/app/templates/utils.html index 23951fd..7987448 100644 --- a/app/templates/utils.html +++ b/app/templates/utils.html @@ -285,7 +285,9 @@
{{ og_meta.title }} - {{ og_meta.site_name }} + {% if og_meta.site_name %} + {{ og_meta.site_name }} + {% endif %}
{% endif %} diff --git a/app/utils/opengraph.py b/app/utils/opengraph.py index c25d65d..cde706f 100644 --- a/app/utils/opengraph.py +++ b/app/utils/opengraph.py @@ -17,7 +17,7 @@ class OpenGraphMeta(BaseModel): title: str image: str description: str - site_name: str + site_name: str | None = None def _scrap_og_meta(html: str) -> OpenGraphMeta | None: @@ -29,10 +29,10 @@ def _scrap_og_meta(html: str) -> OpenGraphMeta | None: raw = {} for field in OpenGraphMeta.__fields__.keys(): og_field = f"og:{field}" - if not ogs.get(og_field): + if not ogs.get(og_field) and field != "site_name": return None - raw[field] = ogs[og_field] + raw[field] = ogs.get(og_field, None) return OpenGraphMeta.parse_obj(raw)