Merge branch 'syndication_extension' of https://github.com/ksato9700/python-feedgen
Signed-off-by: Lars Kiesow <lkiesow@uos.de>
This commit is contained in:
commit
99e4ed856a
3 changed files with 133 additions and 14 deletions
|
@ -27,13 +27,15 @@ if __name__ == '__main__':
|
|||
print_enc ('Usage: %s ( <file>.atom | atom | <file>.rss | rss | podcast )' % \
|
||||
'python -m feedgen')
|
||||
print_enc ('')
|
||||
print_enc (' atom -- Generate ATOM test output and print it to stdout.')
|
||||
print_enc (' rss -- Generate RSS test output and print it to stdout.')
|
||||
print_enc (' <file>.atom -- Generate ATOM test feed and write it to file.atom.')
|
||||
print_enc (' <file>.rss -- Generate RSS test teed and write it to file.rss.')
|
||||
print_enc (' podcast -- Generate Podcast test output and print it to stdout.')
|
||||
print_enc (' dc.atom -- Generate DC extension test output (atom format) and print it to stdout.')
|
||||
print_enc (' dc.rss -- Generate DC extension test output (rss format) and print it to stdout.')
|
||||
print_enc (' atom -- Generate ATOM test output and print it to stdout.')
|
||||
print_enc (' rss -- Generate RSS test output and print it to stdout.')
|
||||
print_enc (' <file>.atom -- Generate ATOM test feed and write it to file.atom.')
|
||||
print_enc (' <file>.rss -- Generate RSS test teed and write it to file.rss.')
|
||||
print_enc (' podcast -- Generate Podcast test output and print it to stdout.')
|
||||
print_enc (' dc.atom -- Generate DC extension test output (atom format) and print it to stdout.')
|
||||
print_enc (' dc.rss -- Generate DC extension test output (rss format) and print it to stdout.')
|
||||
print_enc (' syndication.atom -- Generate DC extension test output (atom format) and print it to stdout.')
|
||||
print_enc (' syndication.rss -- Generate DC extension test output (rss format) and print it to stdout.')
|
||||
print_enc ('')
|
||||
exit()
|
||||
|
||||
|
@ -86,16 +88,23 @@ if __name__ == '__main__':
|
|||
fe.podcast.itunes_author('Lars Kiesow')
|
||||
print_enc (fg.rss_str(pretty=True))
|
||||
|
||||
elif arg == 'dc.atom':
|
||||
elif arg.startswith('dc.'):
|
||||
fg.load_extension('dc')
|
||||
fg.dc.dc_contributor('Lars Kiesow')
|
||||
fe.dc.dc_contributor('Lars Kiesow')
|
||||
print_enc (fg.atom_str(pretty=True))
|
||||
if arg.endswith('.atom'):
|
||||
print_enc (fg.atom_str(pretty=True))
|
||||
else:
|
||||
print_enc (fg.rss_str(pretty=True))
|
||||
|
||||
elif arg == 'dc.rss':
|
||||
fg.load_extension('dc')
|
||||
fg.dc.dc_contributor('Lars Kiesow')
|
||||
print_enc (fg.rss_str(pretty=True))
|
||||
elif arg.startswith('syndication'):
|
||||
fg.load_extension('syndication')
|
||||
fg.syndication.update_period('daily')
|
||||
fg.syndication.update_frequency(2)
|
||||
fg.syndication.update_base('2000-01-01T12:00+00:00')
|
||||
if arg.endswith('.rss'):
|
||||
print_enc (fg.rss_str(pretty=True))
|
||||
else:
|
||||
print_enc (fg.atom_str(pretty=True))
|
||||
|
||||
elif arg.endswith('atom'):
|
||||
fg.atom_file(arg)
|
||||
|
|
60
feedgen/ext/syndication.py
Normal file
60
feedgen/ext/syndication.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright 2015 Kenichi Sato <ksato9700@gmail.com>
|
||||
#
|
||||
|
||||
'''
|
||||
Extends FeedGenerator to support Syndication module
|
||||
|
||||
See below for details
|
||||
http://web.resource.org/rss/1.0/modules/syndication/
|
||||
'''
|
||||
|
||||
from lxml import etree
|
||||
from feedgen.ext.base import BaseExtension
|
||||
|
||||
SYNDICATION_NS = 'http://purl.org/rss/1.0/modules/syndication/'
|
||||
PERIOD_TYPE = ('hourly', 'daily', 'weekly', 'monthly', 'yearly')
|
||||
|
||||
|
||||
def _set_value(channel, name, value):
|
||||
if value:
|
||||
newelem = etree.SubElement(channel, '{%s}' % SYNDICATION_NS + name)
|
||||
newelem.text = value
|
||||
|
||||
|
||||
class SyndicationExtension(BaseExtension):
|
||||
def __init__(self):
|
||||
self._update_period = None
|
||||
self._update_freq = None
|
||||
self._update_base = None
|
||||
|
||||
def extend_ns(self):
|
||||
return {'sy': SYNDICATION_NS}
|
||||
|
||||
def extend_rss(self, rss_feed):
|
||||
channel = rss_feed[0]
|
||||
_set_value(channel, 'UpdatePeriod', self._update_period)
|
||||
_set_value(channel, 'UpdateFrequency', str(self._update_freq))
|
||||
_set_value(channel, 'UpdateBase', self._update_base)
|
||||
|
||||
def update_period(self, value):
|
||||
if value not in PERIOD_TYPE:
|
||||
raise ValueError('Invalid update period value')
|
||||
self._update_period = value
|
||||
return self._update_period
|
||||
|
||||
def update_frequency(self, value):
|
||||
if type(value) is not int or value <= 0:
|
||||
raise ValueError('Invalid update frequency value')
|
||||
self._update_freq = value
|
||||
return self._update_freq
|
||||
|
||||
def update_base(self, value):
|
||||
# the value should be in W3CDTF format
|
||||
self._update_base = value
|
||||
return self._update_base
|
||||
|
||||
|
||||
class SyndicationEntryExtension(BaseExtension):
|
||||
pass
|
|
@ -0,0 +1,50 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Tests for extensions
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from ..feed import FeedGenerator
|
||||
from lxml import etree
|
||||
|
||||
|
||||
class TestExtensionSyndication(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.fg = FeedGenerator()
|
||||
self.fg.load_extension('syndication')
|
||||
self.fg.title('title')
|
||||
self.fg.link(href='http://example.com', rel='self')
|
||||
self.fg.description('description')
|
||||
|
||||
def test_update_period(self):
|
||||
for period_type in ('hourly', 'daily', 'weekly',
|
||||
'monthly', 'yearly'):
|
||||
self.fg.syndication.update_period(period_type)
|
||||
root = etree.fromstring(self.fg.rss_str())
|
||||
a = root.xpath('/rss/channel/sy:UpdatePeriod',
|
||||
namespaces={
|
||||
'sy':'http://purl.org/rss/1.0/modules/syndication/'
|
||||
})
|
||||
assert a[0].text == period_type
|
||||
|
||||
def test_update_frequency(self):
|
||||
for frequency in (1, 100, 2000, 100000):
|
||||
self.fg.syndication.update_frequency(frequency)
|
||||
root = etree.fromstring(self.fg.rss_str())
|
||||
a = root.xpath('/rss/channel/sy:UpdateFrequency',
|
||||
namespaces={
|
||||
'sy':'http://purl.org/rss/1.0/modules/syndication/'
|
||||
})
|
||||
assert a[0].text == str(frequency)
|
||||
|
||||
def test_update_base(self):
|
||||
base = '2000-01-01T12:00+00:00'
|
||||
self.fg.syndication.update_base(base)
|
||||
root = etree.fromstring(self.fg.rss_str())
|
||||
a = root.xpath('/rss/channel/sy:UpdateBase',
|
||||
namespaces={
|
||||
'sy':'http://purl.org/rss/1.0/modules/syndication/'
|
||||
})
|
||||
assert a[0].text == base
|
Loading…
Reference in a new issue