Coverage for slidge / slixfix / link_preview / stanza.py: 98%
60 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-02-15 09:02 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-02-15 09:02 +0000
1# Slixmpp: The Slick XMPP Library
2# Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout
3# This file is part of Slixmpp.
4# See the file LICENSE for copying permission.
6from slixmpp.stanza.message import Message
7from slixmpp.xmlstream import ElementBase, register_stanza_plugin
10class LinkPreview(ElementBase):
11 name = "Description"
12 namespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
13 plugin_attrib = "link_preview"
14 plugin_multi_attrib = "link_previews"
15 interfaces = {"about", "title", "description", "url", "image", "type", "site_name"}
17 def _set_og(self, el: ElementBase, value: str) -> None:
18 el.xml.text = value
19 self.xml.append(el.xml)
21 def _get_og(self, el: type[ElementBase]) -> str | None:
22 child = self.xml.find(f"{{{el.namespace}}}{el.name}")
23 if child is None:
24 return None
25 return child.text
27 def set_title(self, v: str) -> None:
28 self._set_og(Title(), v)
30 def get_title(self) -> str | None:
31 return self._get_og(Title)
33 def set_description(self, v: str) -> None:
34 self._set_og(Description(), v)
36 def get_description(self) -> str | None:
37 return self._get_og(Description)
39 def set_url(self, v: str) -> None:
40 self._set_og(Url(), v)
42 def get_url(self) -> str | None:
43 return self._get_og(Url)
45 def set_image(self, v: str) -> None:
46 self._set_og(Image(), v)
48 def get_image(self) -> str | None:
49 return self._get_og(Image)
51 def set_type(self, v: str) -> None:
52 self._set_og(Type_(), v)
54 def get_type(self) -> str | None:
55 return self._get_og(Type_)
57 def set_site_name(self, v: str) -> None:
58 self._set_og(SiteName(), v)
60 def get_site_name(self) -> str | None:
61 return self._get_og(SiteName)
63 def get_about(self) -> str | None:
64 return self.xml.attrib.get(f"{{{self.namespace}}}about")
67class OpenGraphMixin(ElementBase):
68 namespace = "https://ogp.me/ns#"
71class Title(OpenGraphMixin):
72 name = plugin_attrib = "title"
75class Description(OpenGraphMixin):
76 name = plugin_attrib = "description"
79class Url(OpenGraphMixin):
80 name = plugin_attrib = "url"
83class Image(OpenGraphMixin):
84 name = plugin_attrib = "image"
87class Type_(OpenGraphMixin):
88 name = plugin_attrib = "type"
91class SiteName(OpenGraphMixin):
92 name = plugin_attrib = "site_name"
95def register_plugin() -> None:
96 for plugin in Title, Description, Url, Image, Type_, SiteName:
97 register_stanza_plugin(plugin, Title)
98 register_stanza_plugin(Message, LinkPreview, iterable=True)