diff options
Diffstat (limited to 'rss/atom_feed.py')
-rw-r--r-- | rss/atom_feed.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/rss/atom_feed.py b/rss/atom_feed.py new file mode 100644 index 00000000..622ecc08 --- /dev/null +++ b/rss/atom_feed.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +import lxml.etree as ET + +class AtomFeed(object): + """Class for storing atom:feed data and metadata. + + https://tools.ietf.org/html/rfc4287. + + """ + + # pylint: disable=invalid-name,too-many-instance-attributes + + def __init__(self): + """Define available attributes.""" + self.author = None # atom:author + self.generator = None # atom:generator, optional + self.icon = None # atom:icon, optional + self.logo = None # atom:logo, optional + self.id_text = None # atom:id, just use URI + self.id = None # atom:id + self.links = [] # list of atom:link + self.title_text = None # the text of atom:title + self.title = None # atom:title + self.subtitle_text = None # the text of atom:subtitle + self.subtitle = None # atom:subtitle + self.updated_datetime = None # update time as a datetime object + self.updated = None # atom:updated + self.entries = [] # list of atom:entry, in reverse time order + self.feed = None # atom:feed, assembled + + def assemble_feed(self, FEED_MAX_ENTRIES): + """Assemble atom:feed.""" + # pylint: disable=multiple-statements + self.feed = ET.Element("feed", xmlns="http://www.w3.org/2005/Atom") + self.feed.append(self.title) + if self.subtitle is not None: self.feed.append(self.subtitle) + for link in self.links: + self.feed.append(link) + self.feed.append(self.updated) + self.feed.append(self.id) + self.feed.append(self.author) + if self.icon is not None: self.feed.append(self.icon) + if self.logo is not None: self.feed.append(self.icon) + if self.generator is not None: self.feed.append(self.generator) + # include at most FEED_MAX_ENTRIES entries in the feed + for entry in self.entries[:FEED_MAX_ENTRIES]: + self.feed.append(entry.entry) + + def dump_feed(self, FEED_MAX_ENTRIES): + """Dump atom:feed XML.""" + if self.feed is None: + self.assemble_feed(FEED_MAX_ENTRIES) + return ET.tostring(self.feed).decode("utf-8") |