File: ogp.py

package info (click to toggle)
python-nbxmpp 6.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,360 kB
  • sloc: python: 20,137; makefile: 4
file content (57 lines) | stat: -rw-r--r-- 1,746 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright (C) 2022 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of nbxmpp.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import annotations

from typing import TYPE_CHECKING

from dataclasses import fields

from nbxmpp import Node
from nbxmpp.modules.base import BaseModule
from nbxmpp.namespaces import Namespace
from nbxmpp.structs import MessageProperties
from nbxmpp.structs import OpenGraphData
from nbxmpp.structs import StanzaHandler

if TYPE_CHECKING:
    from nbxmpp.client import Client
    from nbxmpp.protocol import Message


class OpenGraph(BaseModule):
    def __init__(self, client: Client):
        BaseModule.__init__(self, client)

        self._client = client
        self.handlers = [
            StanzaHandler(
                name="message",
                callback=self._process_message_opengraph,
                ns=Namespace.RDF,
                priority=15,
            ),
        ]

    def _process_message_opengraph(
        self, _client: Client, stanza: Message, properties: MessageProperties
    ) -> None:
        for desc in stanza.getTags("Description", namespace=Namespace.RDF):
            about = desc.getAttr("about") or desc.getNamespacedAttr(
                "about", Namespace.RDF
            )
            if about:
                properties.open_graph[about] = self._process_description(desc)

    @staticmethod
    def _process_description(description: Node) -> OpenGraphData:
        data = OpenGraphData()
        for field in fields(OpenGraphData):
            tag = field.name
            node = description.getTag(tag, namespace=Namespace.OPEN_GRAPH)
            if node is not None:
                setattr(data, tag, node.getData())
        return data