RSS: Add support for feed attributes in template string

See https://feedparser.readthedocs.io/en/latest/common-rss-elements.html#accessing-common-channel-elements
This commit is contained in:
Valentin Lorentz 2023-07-26 14:21:30 +02:00
parent 8168c52939
commit bb3d456fdf
3 changed files with 16 additions and 5 deletions

View File

@ -68,7 +68,9 @@ conf.registerChannelValue(RSS, 'format',
will use for displaying headlines of a RSS feed that is triggered will use for displaying headlines of a RSS feed that is triggered
manually. In addition to fields defined by feedparser ($published manually. In addition to fields defined by feedparser ($published
(the entry date), $title, $link, $description, $id, etc.), the following (the entry date), $title, $link, $description, $id, etc.), the following
variables can be used: $feed_name, $date (parsed date, as defined in variables can be used: $feed_name (the configured name)
$feed_title/$feed_subtitle/$feed_author/$feed_language/$feed_link,
$date (parsed date, as defined in
supybot.reply.format.time)"""))) supybot.reply.format.time)""")))
conf.registerChannelValue(RSS, 'announceFormat', conf.registerChannelValue(RSS, 'announceFormat',
registry.String(_('News from $feed_name: $title <$link>'), registry.String(_('News from $feed_name: $title <$link>'),

View File

@ -493,10 +493,11 @@ class RSS(callbacks.Plugin):
template = self.registryValue(key_name, channel, network) template = self.registryValue(key_name, channel, network)
date = entry.get('published_parsed') date = entry.get('published_parsed')
date = utils.str.timestamp(date) date = utils.str.timestamp(date)
s = string.Template(template).safe_substitute( kwargs = {"feed_%s" % k: v for (k, v) in feed.data.items() if
entry, isinstance(v, str)}
feed_name=feed.name, kwargs["feed_name"] = feed.name
date=date) kwargs.update(entry)
s = string.Template(template).safe_substitute(entry, **kwargs, date=date)
return self._normalize_entry(s) return self._normalize_entry(s)
def announce_entry(self, irc, channel, feed, entry): def announce_entry(self, irc, channel, feed, entry):

View File

@ -359,6 +359,14 @@ class RSSTestCase(ChannelPluginTestCase):
self.assertRegexp('rss http://xkcd.com/rss.xml', self.assertRegexp('rss http://xkcd.com/rss.xml',
'On the other hand, the refractor\'s') 'On the other hand, the refractor\'s')
@mock_urllib
def testFeedAttribute(self, mock):
timeFastForward(1.1)
with conf.supybot.plugins.RSS.format.context('$feed_title: $title'):
mock._data = xkcd_new
self.assertRegexp('rss http://xkcd.com/rss.xml',
r'xkcd\.com: Telescopes')
@mock_urllib @mock_urllib
def testBadlyFormedFeedWithNoItems(self, mock): def testBadlyFormedFeedWithNoItems(self, mock):
# This combination will cause the RSS command to show the last parser # This combination will cause the RSS command to show the last parser