mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-17 14:10:41 +01:00
Converted RSS to the new registry dealio.
This commit is contained in:
parent
1714359f0a
commit
52bdcaf41d
116
plugins/RSS.py
116
plugins/RSS.py
@ -30,8 +30,7 @@
|
|||||||
###
|
###
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Provides basic functionality for handling RSS/RDF feeds. Depends on the Alias
|
Provides basic functionality for handling RSS/RDF feeds.
|
||||||
module for user-friendliness.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__revision__ = "$Id$"
|
__revision__ = "$Id$"
|
||||||
@ -49,8 +48,8 @@ import utils
|
|||||||
import ircmsgs
|
import ircmsgs
|
||||||
import ircutils
|
import ircutils
|
||||||
import privmsgs
|
import privmsgs
|
||||||
|
import registry
|
||||||
import callbacks
|
import callbacks
|
||||||
import configurable
|
|
||||||
|
|
||||||
def configure(onStart, afterConnect, advanced):
|
def configure(onStart, afterConnect, advanced):
|
||||||
from questions import expect, anything, something, yn
|
from questions import expect, anything, something, yn
|
||||||
@ -61,53 +60,64 @@ def configure(onStart, afterConnect, advanced):
|
|||||||
name = something('What\'s the name of the website?')
|
name = something('What\'s the name of the website?')
|
||||||
url = something('What\'s the URL of the RSS feed?')
|
url = something('What\'s the URL of the RSS feed?')
|
||||||
onStart.append('rss add %s %s' % (name, url))
|
onStart.append('rss add %s %s' % (name, url))
|
||||||
#onStart.append('alias lock %s' % name)
|
|
||||||
|
|
||||||
def SpaceOnRightStrType(s):
|
class StringWithSpaceOnRight(registry.String):
|
||||||
s = configurable.StrType(s)
|
def setValue(self, v):
|
||||||
if s.rstrip() == s:
|
if v.rstrip() == v:
|
||||||
s += ' '
|
v += ' '
|
||||||
return s
|
registry.String.setValue(self, v)
|
||||||
|
|
||||||
class RSS(callbacks.Privmsg, configurable.Mixin):
|
conf.registerPlugin('RSS')
|
||||||
|
conf.registerChannelValue(conf.supybot.plugins.RSS, 'bold', registry.Boolean(
|
||||||
|
True, """Determines whether the bot will bold the title of the feed when it
|
||||||
|
announces new news."""))
|
||||||
|
conf.registerChannelValue(conf.supybot.plugins.RSS, 'headlineSeparator',
|
||||||
|
registry.StringSurroundedBySpaces(' || ', """Determines what string is used
|
||||||
|
to separate headlines in new feeds."""))
|
||||||
|
conf.registerChannelValue(conf.supybot.plugins.RSS, 'announcementPrefix',
|
||||||
|
StringWithSpaceOnRight('New news from ', """Determines what prefix is
|
||||||
|
prepended (if any) to the new news item announcements made in the
|
||||||
|
channel."""))
|
||||||
|
conf.registerChannelValue(conf.supybot.plugins.RSS, 'announce',
|
||||||
|
registry.SpaceSeparatedListOfStrings([], """Determines which RSS feeds
|
||||||
|
should be announced in the channel; valid input is a list of strings
|
||||||
|
(either registered RSS feeds or RSS feed URLs) separated by spaces."""))
|
||||||
|
conf.registerGlobalValue(conf.supybot.plugins.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.registerGroup(conf.supybot.plugins.RSS, 'feeds')
|
||||||
|
|
||||||
|
def registerFeed(name, url):
|
||||||
|
conf.supybot.plugins.RSS.feeds.register(name, registry.String(url, ''))
|
||||||
|
|
||||||
|
class RSS(callbacks.Privmsg):
|
||||||
threaded = True
|
threaded = True
|
||||||
configurables = configurable.Dictionary(
|
|
||||||
[('announce-news-feeds', configurable.SpaceSeparatedStrListType,
|
|
||||||
[], """Gives the bot a space-separated list of feeds for which it
|
|
||||||
should announce updates to the channel. The feeds should be
|
|
||||||
either URLs or names of feeds already added to this plugin."""),
|
|
||||||
('announce-news-bold', configurable.BoolType, True,
|
|
||||||
"""Determines whether the bot will bold the title of the feed when
|
|
||||||
it announces new additions."""),
|
|
||||||
('headline-separator', configurable.SpaceSurroundedStrType, ' :: ',
|
|
||||||
"""Determines what string is used to seperate headlines in
|
|
||||||
feeds."""),
|
|
||||||
('announce-news-prefix', SpaceOnRightStrType,
|
|
||||||
'New news from ',
|
|
||||||
"""Sets the prefix to be added (if any) to the new news item
|
|
||||||
announcements made to the channel."""),]
|
|
||||||
)
|
|
||||||
globalConfigurables = configurable.Dictionary(
|
|
||||||
[('wait-period', configurable.PositiveIntType, 1800,
|
|
||||||
"""Indicates how many seconds the bot will wait between retrieving
|
|
||||||
RSS feeds; requests made within this period will return cached
|
|
||||||
results."""),]
|
|
||||||
)
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
callbacks.Privmsg.__init__(self)
|
callbacks.Privmsg.__init__(self)
|
||||||
self.feedNames = sets.Set()
|
self.feedNames = sets.Set()
|
||||||
self.lastRequest = {}
|
self.lastRequest = {}
|
||||||
self.cachedFeeds = {}
|
self.cachedFeeds = {}
|
||||||
|
L = conf.supybot.plugins.RSS.feeds.getValues(fullNames=False)
|
||||||
|
for (name, url) in registry._cache.iteritems():
|
||||||
|
name = name.lower()
|
||||||
|
if name.startswith('supybot.plugins.rss.feeds.'):
|
||||||
|
name = rsplit(name, '.', 1)[-1]
|
||||||
|
v = registry.String('', 'help is not needed here')
|
||||||
|
v.set(url)
|
||||||
|
url = v()
|
||||||
|
self.makeFeedCommand(name, url)
|
||||||
|
|
||||||
def __call__(self, irc, msg):
|
def __call__(self, irc, msg):
|
||||||
callbacks.Privmsg.__call__(self, irc, msg)
|
callbacks.Privmsg.__call__(self, irc, msg)
|
||||||
irc = callbacks.IrcObjectProxyRegexp(irc, msg)
|
irc = callbacks.IrcObjectProxyRegexp(irc, msg)
|
||||||
feeds = self.configurables.getChannels('announce-news-feeds')
|
L = conf.supybot.plugins.RSS.announce.getValues(fullNames=False)
|
||||||
for (channel, d) in feeds.iteritems():
|
for (channel, v) in L:
|
||||||
sep = self.configurables.get('headline-separator', channel)
|
feeds = v()
|
||||||
bold = self.configurables.get('announce-news-bold', channel)
|
bold = self.registryValue('bold', channel)
|
||||||
prefix = self.configurables.get('announce-news-prefix', channel)
|
sep = self.registryValue('headlineSeparator', channel)
|
||||||
for name in d:
|
prefix = self.registryValue('announcementPrefix', channel)
|
||||||
|
for name in feeds:
|
||||||
if self.isCommand(callbacks.canonicalName(name)):
|
if self.isCommand(callbacks.canonicalName(name)):
|
||||||
url = self.getCommand(name).url
|
url = self.getCommand(name).url
|
||||||
else:
|
else:
|
||||||
@ -132,7 +142,7 @@ class RSS(callbacks.Privmsg, configurable.Mixin):
|
|||||||
|
|
||||||
def getFeed(self, url):
|
def getFeed(self, url):
|
||||||
now = time.time()
|
now = time.time()
|
||||||
wait = self.globalConfigurables.get('wait-period')
|
wait = self.registryValue('waitPeriod')
|
||||||
if url not in self.lastRequest or now - self.lastRequest[url] > wait:
|
if url not in self.lastRequest or now - self.lastRequest[url] > wait:
|
||||||
try:
|
try:
|
||||||
self.log.info('Downloading new feed from %s', url)
|
self.log.info('Downloading new feed from %s', url)
|
||||||
@ -147,24 +157,17 @@ class RSS(callbacks.Privmsg, configurable.Mixin):
|
|||||||
def getHeadlines(self, feed):
|
def getHeadlines(self, feed):
|
||||||
return [utils.htmlToText(d['title'].strip()) for d in feed['items']]
|
return [utils.htmlToText(d['title'].strip()) for d in feed['items']]
|
||||||
|
|
||||||
def add(self, irc, msg, args):
|
def makeFeedCommand(self, name, url):
|
||||||
"""<name> <url>
|
|
||||||
|
|
||||||
Adds a command to this plugin that will look up the RSS feed at the
|
|
||||||
given URL.
|
|
||||||
"""
|
|
||||||
(name, url) = privmsgs.getArgs(args, required=2)
|
|
||||||
docstring = """takes no arguments
|
docstring = """takes no arguments
|
||||||
|
|
||||||
Reports the titles for %s at the RSS feed <%s>. RSS feeds are only
|
Reports the titles for %s at the RSS feed <%s>. RSS feeds are only
|
||||||
looked up every half hour at the most (since that's how most
|
looked up every supybot.plugins.RSS.waitPeriod seconds, which defaults
|
||||||
websites prefer it).
|
to 1800 (30 minutes) since that's what most websites prefer.
|
||||||
""" % (name, url)
|
""" % (name, url)
|
||||||
name = callbacks.canonicalName(name)
|
name = callbacks.canonicalName(name)
|
||||||
if hasattr(self, name):
|
if hasattr(self, name):
|
||||||
s = 'I already have a command in this plugin named %s' % name
|
s = 'I already have a command in this plugin named %s' % name
|
||||||
irc.error(s)
|
raise callbacks.Error, s
|
||||||
return
|
|
||||||
def f(self, irc, msg, args):
|
def f(self, irc, msg, args):
|
||||||
args.insert(0, url)
|
args.insert(0, url)
|
||||||
self.rss(irc, msg, args)
|
self.rss(irc, msg, args)
|
||||||
@ -172,6 +175,16 @@ class RSS(callbacks.Privmsg, configurable.Mixin):
|
|||||||
f.url = url # Used by __call__.
|
f.url = url # Used by __call__.
|
||||||
self.feedNames.add(name)
|
self.feedNames.add(name)
|
||||||
setattr(self.__class__, name, f)
|
setattr(self.__class__, name, f)
|
||||||
|
registerFeed(name, url)
|
||||||
|
|
||||||
|
def add(self, irc, msg, args):
|
||||||
|
"""<name> <url>
|
||||||
|
|
||||||
|
Adds a command to this plugin that will look up the RSS feed at the
|
||||||
|
given URL.
|
||||||
|
"""
|
||||||
|
(name, url) = privmsgs.getArgs(args, required=2)
|
||||||
|
self.makeFeedCommand(name, url)
|
||||||
irc.replySuccess()
|
irc.replySuccess()
|
||||||
|
|
||||||
def remove(self, irc, msg, args):
|
def remove(self, irc, msg, args):
|
||||||
@ -194,6 +207,7 @@ class RSS(callbacks.Privmsg, configurable.Mixin):
|
|||||||
Gets the title components of the given RSS feed.
|
Gets the title components of the given RSS feed.
|
||||||
"""
|
"""
|
||||||
url = privmsgs.getArgs(args)
|
url = privmsgs.getArgs(args)
|
||||||
|
self.log.debug('Fetching %s', url)
|
||||||
feed = self.getFeed(url)
|
feed = self.getFeed(url)
|
||||||
if ircutils.isChannel(msg.args[0]):
|
if ircutils.isChannel(msg.args[0]):
|
||||||
channel = msg.args[0]
|
channel = msg.args[0]
|
||||||
@ -204,7 +218,7 @@ class RSS(callbacks.Privmsg, configurable.Mixin):
|
|||||||
irc.error('Couldn\'t get RSS feed')
|
irc.error('Couldn\'t get RSS feed')
|
||||||
return
|
return
|
||||||
headlines = imap(utils.htmlToText, headlines)
|
headlines = imap(utils.htmlToText, headlines)
|
||||||
sep = self.configurables.get('headline-separator', channel)
|
sep = self.registryValue('headlineSeparator', channel)
|
||||||
irc.reply(sep.join(headlines))
|
irc.reply(sep.join(headlines))
|
||||||
|
|
||||||
def info(self, irc, msg, args):
|
def info(self, irc, msg, args):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user