RSS: add option display headline timestamp.

This commit is contained in:
Daniel Folkinshteyn 2013-05-05 11:23:15 -04:00
parent 78659113c1
commit af1931b3db
2 changed files with 30 additions and 18 deletions

View File

@ -74,6 +74,12 @@ conf.registerChannelValue(RSS, 'showLinks',
along with the title of the feed when the rss command is called.
supybot.plugins.RSS.announce.showLinks affects whether links will be
listed when a feed is automatically announced."""))
conf.registerChannelValue(RSS, 'showPubDate',
registry.Boolean(False, """Determines whether the bot will list the
publication datetime stamp along with the title of the feed when the rss
command is called.
supybot.plugins.RSS.announce.showPubDate affects whether this will be
listed when a feed is automatically announced."""))
conf.registerGlobalValue(RSS, 'defaultNumberOfHeadlines',
registry.PositiveInteger(1, """Indicates how many headlines an rss feed
will output by default, if no number is provided."""))
@ -95,8 +101,12 @@ conf.registerChannelValue(RSS.announce, 'showLinks',
registry.Boolean(False, """Determines whether the bot will list the link
along with the title of the feed when a feed is automatically
announced."""))
conf.registerChannelValue(RSS.announce, 'showPubDate',
registry.Boolean(False, """Determines whether the bot will list the
publication datetime stamp along with the title of the feed when a feed
is automatically announced."""))
conf.registerGlobalValue(RSS.announce, 'cachePeriod',
registry.PositiveInteger(86400, """Maximum age of cached RSS headlines,
registry.PositiveInteger(604800, """Maximum age of cached RSS headlines,
in seconds. Headline cache is used to avoid re-announcing old news."""))

View File

@ -139,23 +139,24 @@ class RSS(callbacks.Plugin):
self.releaseLock(url)
time.sleep(0.1) # So other threads can run.
def buildHeadlines(self, headlines, channel, config='announce.showLinks'):
def buildHeadlines(self, headlines, channel, linksconfig='announce.showLinks', dateconfig='announce.showPubDate'):
newheadlines = []
if self.registryValue(config, channel):
for headline in headlines:
for headline in headlines:
link = ''
pubDate = ''
if self.registryValue(linksconfig, channel):
if headline[1]:
if self.registryValue('stripRedirect'):
h = re.sub('^.*http://', 'http://', headline[1])
link = ' <%s>' % (re.sub('^.*http://', 'http://', headline[1]),)
else:
h = headline[1]
newheadlines.append(format('%s %u',
headline[0],
h.encode('utf-8')))
else:
newheadlines.append(format('%s', headline[0]))
else:
for headline in headlines:
newheadlines = [format('%s', h[0]) for h in headlines]
link = ' <%s>' % (headline[1],)
if self.registryValue(dateconfig, channel):
if headline[2]:
pubDate = ' [%s]' % (headline[2],)
newheadlines.append(format('%s%s%s',
headline[0],
link.encode('utf-8'),
pubDate))
return newheadlines
def _newHeadlines(self, irc, channels, name, url):
@ -172,7 +173,7 @@ class RSS(callbacks.Plugin):
#oldresults = self.cachedFeeds[url]
#oldheadlines = self.getHeadlines(oldresults)
oldheadlines = self.cachedHeadlines[url]
oldheadlines = filter(lambda x: t - x[2] < self.registryValue('announce.cachePeriod'), oldheadlines)
oldheadlines = filter(lambda x: t - x[3] < self.registryValue('announce.cachePeriod'), oldheadlines)
except KeyError:
oldheadlines = []
newresults = self.getFeed(url)
@ -317,8 +318,9 @@ class RSS(callbacks.Plugin):
for d in feed['items']:
if 'title' in d:
title = conv(d['title'])
link = d.get('link') # defaults to None
headlines.append((title, link, t))
link = d.get('link')
pubDate = d.get('pubDate', d.get('updated'))
headlines.append((title, link, pubDate, t))
return headlines
def makeFeedCommand(self, name, url):
@ -430,7 +432,7 @@ class RSS(callbacks.Plugin):
if not headlines:
irc.error('Couldn\'t get RSS feed.')
return
headlines = self.buildHeadlines(headlines, channel, 'showLinks')
headlines = self.buildHeadlines(headlines, channel, 'showLinks', 'showPubDate')
if n:
headlines = headlines[:n]
else: