aboutsummaryrefslogtreecommitdiff
path: root/rss/rss_item.py
blob: 8ead20e65569d975f8de1ca98a016d8a0ddd1413 (plain)
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
#!/usr/bin/env python3

import lxml.etree as ET

class RssItem(object):
    """Class for storing an RSS 2.0 item."""

    ELEMENTS = ["title", "link", "description", "author", "category", "comments",
                "enclosure", "guid", "pubDate", "source"]

    def __init__(self):
        """Define available attributes."""
        for element in self.ELEMENTS:
            setattr(self, element, None)
        self.timestamp = None
        self.item = None

    def assemble_item(self):
        """Assemble an RSS 2.0 item."""
        self.item = ET.Element("item")
        for element in self.ELEMENTS:
            attr = getattr(self, element)
            if attr is not None:
                self.item.append(attr)

    def dump_item(self):
        """Dump RSS item XML."""
        if self.item is None:
            self.assemble_item()
        return ET.tostring(self.item).decode("utf-8")