Daniel Brötzmann pushed to branch generate-ogp-previews at gajim / gajim


Commits:
41a8facb by cal0pteryx at 2026-02-16T21:36:41+01:00
other: OpenGraph: Parse fallback title/description

- - - - -


1 changed file:

- gajim/common/open_graph_parser.py


Changes:

=====================================
gajim/common/open_graph_parser.py
=====================================
@@ -16,6 +16,7 @@
     "og:type",
     "og:site_name",
 ]
+ALLOWED_PROPERTIES = ["description"]
 
 
 class OpenGraphParser(HTMLParser):
@@ -23,33 +24,62 @@ def __init__(self) -> None:
         super().__init__()
 
         self._attributes: dict[str, str] = {}
+        self._fallback_title: str | None = None
+        self._fallback_description: str | None = None
+
+        self._reading_title = False
 
     def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) 
-> None:
+        if tag == "title":
+            self._reading_title = True
+
         if tag != "meta":
             return
 
         attrs_dict = dict(attrs)
-        if not (prop := attrs_dict.get("property")):
+
+        prop = attrs_dict.get("property")
+        name = attrs_dict.get("name")
+        if not prop and not name:
             return
 
-        if prop not in ALLOWED_OG_PROPERTIES:
+        if prop not in ALLOWED_OG_PROPERTIES and name not in 
ALLOWED_PROPERTIES:
             return
 
         if not (content := attrs_dict.get("content")):
             return
 
-        self._attributes[prop.removeprefix("og:")] = content
+        if prop:
+            self._attributes[prop.removeprefix("og:")] = content
+        if name:
+            self._fallback_description = content
 
     def handle_endtag(self, tag: str) -> None:
+        if tag == "title":
+            self._reading_title = False
+
         if tag == "head":
             raise EOF
 
+    def handle_data(self, data: str) -> None:
+        if self._reading_title:
+            self._fallback_title = data
+
     def parse(self, text: str) -> OpenGraphData | None:
         try:
             self.feed(text)
         except EOF:
             pass
 
+        if "title" not in self._attributes and self._fallback_title is not 
None:
+            self._attributes["title"] = self._fallback_title
+
+        if (
+            "description" not in self._attributes
+            and self._fallback_description is not None
+        ):
+            self._attributes["description"] = self._fallback_description
+
         if not self._attributes:
             return None
 



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/41a8facbd6180ad584cd5b071c07e55b13340f7c

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/41a8facbd6180ad584cd5b071c07e55b13340f7c
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to