diff --git a/plugins/RSS/config.py b/plugins/RSS/config.py index d816705a2..510584755 100644 --- a/plugins/RSS/config.py +++ b/plugins/RSS/config.py @@ -45,6 +45,10 @@ def configure(advanced): class FeedNames(registry.SpaceSeparatedListOfStrings): List = callbacks.CanonicalNameSet +class FeedItemSortOrder(registry.OnlySomeStrings): + """Valid values include 'asInFeed', 'oldestFirst', 'newestFirst'.""" + validStrings = ('asInFeed', 'oldestFirst', 'newestFirst') + RSS = conf.registerPlugin('RSS') conf.registerChannelValue(RSS, 'bold', registry.Boolean( 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 wait between retrieving RSS feeds; requests made within this period will 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', FeedNames([], _("""Determines what feeds should be accessible as commands."""))) diff --git a/plugins/RSS/plugin.py b/plugins/RSS/plugin.py index 5945145a8..8d0332a7f 100644 --- a/plugins/RSS/plugin.py +++ b/plugins/RSS/plugin.py @@ -300,11 +300,27 @@ class RSS(callbacks.Plugin): return conv else: 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): headlines = [] conv = self._getConverter(feed) - for d in feed['items']: + for d in self._sortFeedItems(feed['items']): if 'title' in d: title = conv(d['title']) link = d.get('link')