RSS: sort and display feed items by date

This commit is contained in:
Sebastian Kayser 2012-05-28 19:30:44 +02:00 committed by Valentin Lorentz
parent dfad31135e
commit 2486be4464
2 changed files with 25 additions and 1 deletions

View File

@ -45,6 +45,10 @@ def configure(advanced):
class FeedNames(registry.SpaceSeparatedListOfStrings): class FeedNames(registry.SpaceSeparatedListOfStrings):
List = callbacks.CanonicalNameSet List = callbacks.CanonicalNameSet
class FeedItemSortOrder(registry.OnlySomeStrings):
"""Valid values include 'asInFeed', 'oldestFirst', 'newestFirst'."""
validStrings = ('asInFeed', 'oldestFirst', 'newestFirst')
RSS = conf.registerPlugin('RSS') RSS = conf.registerPlugin('RSS')
conf.registerChannelValue(RSS, 'bold', registry.Boolean( conf.registerChannelValue(RSS, 'bold', registry.Boolean(
True, _("""Determines whether the bot will bold the title of the feed when True, _("""Determines whether the bot will bold the title of the feed when
@ -64,6 +68,10 @@ conf.registerGlobalValue(RSS, 'waitPeriod',
registry.PositiveInteger(1800, _("""Indicates how many seconds the bot will registry.PositiveInteger(1800, _("""Indicates how many seconds the bot will
wait between retrieving RSS feeds; requests made within this period will wait between retrieving RSS feeds; requests made within this period will
return cached results."""))) return cached results.""")))
conf.registerGlobalValue(RSS, 'sortFeedItems',
FeedItemSortOrder('asInFeed', _("""Determines whether feed items should be
sorted by their update timestamp or kept in the same order as they appear
in a feed.""")))
conf.registerGlobalValue(RSS, 'feeds', conf.registerGlobalValue(RSS, 'feeds',
FeedNames([], _("""Determines what feeds should be accessible as FeedNames([], _("""Determines what feeds should be accessible as
commands."""))) commands.""")))

View File

@ -300,11 +300,27 @@ class RSS(callbacks.Plugin):
return conv return conv
else: else:
return lambda s: toText(s).strip() return lambda s: toText(s).strip()
def _sortFeedItems(self, items):
"""Return feed items, sorted according to sortFeedItems."""
order = self.registryValue('sortFeedItems')
if order not in ['oldestFirst', 'newestFirst']:
return items
if order == 'oldestFirst':
reverse = False
if order == 'newestFirst':
reverse = True
try:
sitems = sorted(items, key=lambda i: i['updated'], reverse=reverse)
except KeyError:
# feedparser normalizes required timestamp fields in ATOM and RSS
# to the "updated" field. Feeds missing it are unsortable by date.
return items
return sitems
def getHeadlines(self, feed): def getHeadlines(self, feed):
headlines = [] headlines = []
conv = self._getConverter(feed) conv = self._getConverter(feed)
for d in feed['items']: for d in self._sortFeedItems(feed['items']):
if 'title' in d: if 'title' in d:
title = conv(d['title']) title = conv(d['title'])
link = d.get('link') link = d.get('link')