Add Source Element to Feed Entries

This patch implements the source element for feed entries/items. Note
that only the set of RSS elements (URL and title) are implemented right
now. ATOM supports additional elements which cannot be set at the
moment.

This fixes #79
This commit is contained in:
Lars Kiesow 2018-08-12 21:07:47 +02:00
parent 1b301f67ad
commit c6c7165a97
No known key found for this signature in database
GPG key ID: 5DAFE8D9C823CE73
2 changed files with 32 additions and 0 deletions

View file

@ -173,6 +173,15 @@ class FeedEntry(object):
rights = etree.SubElement(entry, 'rights') rights = etree.SubElement(entry, 'rights')
rights.text = self.__atom_rights rights.text = self.__atom_rights
if self.__atom_source:
source = etree.SubElement(entry, 'source')
if self.__atom_source.get('title'):
source_title = etree.SubElement(source, 'title')
source_title.text = self.__atom_source['title']
if self.__atom_source.get('link'):
etree.SubElement(source, 'link',
href=self.__atom_source['link'])
if extensions: if extensions:
for ext in self.__extensions.values() or []: for ext in self.__extensions.values() or []:
if ext.get('atom'): if ext.get('atom'):
@ -233,6 +242,10 @@ class FeedEntry(object):
if self.__rss_pubDate: if self.__rss_pubDate:
pubDate = etree.SubElement(entry, 'pubDate') pubDate = etree.SubElement(entry, 'pubDate')
pubDate.text = formatRFC2822(self.__rss_pubDate) pubDate.text = formatRFC2822(self.__rss_pubDate)
if self.__rss_source:
source = etree.SubElement(entry, 'source',
url=self.__rss_source['url'])
source.text = self.__rss_source['title']
if extensions: if extensions:
for ext in self.__extensions.values() or []: for ext in self.__extensions.values() or []:
@ -614,6 +627,22 @@ class FeedEntry(object):
self.__rss_comments = comments self.__rss_comments = comments
return self.__rss_comments return self.__rss_comments
def source(self, url=None, title=None):
'''Get or set the source for the current feed entry.
Note that ATOM feeds support a lot more sub elements than title and URL
(which is what RSS supports) but these are currently not supported.
Patches are welcome.
:param url: Link to the source.
:param title: Title of the linked resource
:returns: Source element as dictionaries.
'''
if url is not None and title is not None:
self.__rss_source = {'url': url, 'title': title}
self.__atom_source = {'link': url, 'title': title}
return self.__rss_source
def enclosure(self, url=None, length=None, type=None): def enclosure(self, url=None, length=None, type=None):
'''Get or set the value of enclosure which describes a media object '''Get or set the value of enclosure which describes a media object
that is attached to the item. This is a RSS only value which is that is attached to the item. This is a RSS only value which is

View file

@ -91,6 +91,9 @@ class TestSequenceFunctions(unittest.TestCase):
assert fe.pubDate().year == 2017 assert fe.pubDate().year == 2017
fe.rights('asdfx') fe.rights('asdfx')
assert fe.rights() == 'asdfx' assert fe.rights() == 'asdfx'
source = fe.source(url='https://example.com', title='Test')
assert source.get('title') == 'Test'
assert source.get('url') == 'https://example.com'
fe.comments('asdfx') fe.comments('asdfx')
assert fe.comments() == 'asdfx' assert fe.comments() == 'asdfx'
fe.enclosure(url='http://lkiesow.de', type='text/plain', length='1') fe.enclosure(url='http://lkiesow.de', type='text/plain', length='1')