2003-04-06 13:42:41 +02:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-04-06 13:42:41 +02:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
2004-01-21 20:11:27 +01:00
|
|
|
Provides basic functionality for handling RSS/RDF feeds.
|
2003-04-06 13:42:41 +02:00
|
|
|
"""
|
|
|
|
|
2004-09-02 06:06:20 +02:00
|
|
|
import supybot
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
2004-09-02 06:06:20 +02:00
|
|
|
__author__ = supybot.authors.jemfinch
|
2003-11-25 09:23:47 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-04-06 13:42:41 +02:00
|
|
|
|
2003-12-03 22:21:43 +01:00
|
|
|
import sets
|
2003-04-06 13:49:53 +02:00
|
|
|
import time
|
2004-07-21 21:12:57 +02:00
|
|
|
import getopt
|
2004-04-01 17:00:04 +02:00
|
|
|
import sgmllib
|
2004-02-10 03:49:52 +01:00
|
|
|
import threading
|
2003-11-15 05:46:09 +01:00
|
|
|
from itertools import imap
|
2003-04-06 13:49:53 +02:00
|
|
|
|
2003-04-06 13:42:41 +02:00
|
|
|
import rssparser
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.world as world
|
2004-10-29 03:35:37 +02:00
|
|
|
from supybot.commands import *
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.registry as registry
|
|
|
|
import supybot.callbacks as callbacks
|
2003-04-06 13:42:41 +02:00
|
|
|
|
2004-08-03 07:52:03 +02:00
|
|
|
class AnnouncedFeeds(registry.SpaceSeparatedListOfStrings):
|
|
|
|
List = callbacks.CanonicalNameSet
|
2004-09-24 01:53:51 +02:00
|
|
|
|
2004-01-21 20:11:27 +01:00
|
|
|
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',
|
2004-01-23 14:28:53 +01:00
|
|
|
registry.StringWithSpaceOnRight('New news from ', """Determines what prefix
|
|
|
|
is prepended (if any) to the new news item announcements made in the
|
2004-01-21 20:11:27 +01:00
|
|
|
channel."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.RSS, 'announce',
|
2004-08-03 07:52:03 +02:00
|
|
|
AnnouncedFeeds([], """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."""))
|
2004-01-21 20:11:27 +01:00
|
|
|
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."""))
|
2004-08-03 07:52:03 +02:00
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.RSS, 'feeds',
|
|
|
|
AnnouncedFeeds([], """Determines what feeds should be accessible as
|
|
|
|
commands."""))
|
2004-09-24 01:53:51 +02:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.RSS, 'showLinks',
|
|
|
|
registry.Boolean(False, """Determines whether the bot will list the link
|
|
|
|
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.registerGroup(conf.supybot.plugins.RSS, 'announce')
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.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."""))
|
2004-07-21 21:12:57 +02:00
|
|
|
|
2005-01-03 19:45:07 +01:00
|
|
|
def getFeedName(irc, msg, args, state):
|
|
|
|
if not registry.isValidRegistryName(args[0]):
|
|
|
|
irc.errorInvalid('feed name', name,
|
|
|
|
'Feed names must not include spaces.')
|
|
|
|
state.args.append(callbacks.canonicalName(args.pop(0)))
|
|
|
|
|
|
|
|
addConverter('feedName', getFeedName)
|
|
|
|
|
2004-01-21 20:11:27 +01:00
|
|
|
class RSS(callbacks.Privmsg):
|
2004-08-06 11:52:36 +02:00
|
|
|
"""This plugin is useful both for announcing updates to RSS feeds in a
|
|
|
|
channel, and for retrieving the headlines of RSS feeds via command. Use
|
|
|
|
the "add" command to add feeds to this plugin, and use the "announce"
|
|
|
|
command to determine what feeds should be announced in a given channel."""
|
2003-04-06 13:42:41 +02:00
|
|
|
threaded = True
|
2003-04-08 09:08:46 +02:00
|
|
|
def __init__(self):
|
2004-10-29 03:35:37 +02:00
|
|
|
self.__parent = super(RSS, self)
|
|
|
|
self.__parent.__init__()
|
2004-09-02 06:06:20 +02:00
|
|
|
self.feedNames = callbacks.CanonicalNameSet()
|
2004-04-09 07:22:56 +02:00
|
|
|
self.locks = {}
|
2003-04-08 09:08:46 +02:00
|
|
|
self.lastRequest = {}
|
2003-04-14 07:17:49 +02:00
|
|
|
self.cachedFeeds = {}
|
2004-04-13 20:25:49 +02:00
|
|
|
self.gettingLockLock = threading.Lock()
|
2004-08-03 07:52:03 +02:00
|
|
|
for name in self.registryValue('feeds'):
|
|
|
|
self._registerFeed(name)
|
2004-12-18 21:28:52 +01:00
|
|
|
try:
|
|
|
|
url = self.registryValue('feeds.%s' % name)
|
|
|
|
except registry.NonExistentRegistryEntry:
|
|
|
|
self.log.warning('%s is not a registered feed, removing.',name)
|
|
|
|
continue
|
2004-08-03 07:52:03 +02:00
|
|
|
self.makeFeedCommand(name, url)
|
2004-08-24 06:47:34 +02:00
|
|
|
self.getFeed(url) # So announced feeds don't announce on startup.
|
2004-08-03 07:52:03 +02:00
|
|
|
|
|
|
|
def _registerFeed(self, name, url=''):
|
|
|
|
self.registryValue('feeds').add(name)
|
|
|
|
group = self.registryValue('feeds', value=False)
|
|
|
|
group.register(name, registry.String(url, ''))
|
2003-04-08 09:08:46 +02:00
|
|
|
|
2004-01-04 13:34:01 +01:00
|
|
|
def __call__(self, irc, msg):
|
2004-10-29 03:35:37 +02:00
|
|
|
self.__parent.__call__(irc, msg)
|
2004-07-31 14:14:13 +02:00
|
|
|
irc = callbacks.SimpleProxy(irc, msg)
|
2004-04-05 23:36:27 +02:00
|
|
|
newFeeds = {}
|
2004-08-24 06:47:34 +02:00
|
|
|
for channel in irc.state.channels:
|
|
|
|
feeds = self.registryValue('announce', channel)
|
2004-01-21 20:11:27 +01:00
|
|
|
for name in feeds:
|
2004-04-15 16:17:07 +02:00
|
|
|
commandName = callbacks.canonicalName(name)
|
|
|
|
if self.isCommand(commandName):
|
|
|
|
name = commandName
|
2004-01-04 13:34:01 +01:00
|
|
|
url = self.getCommand(name).url
|
|
|
|
else:
|
|
|
|
url = name
|
2004-02-10 03:49:52 +01:00
|
|
|
if self.willGetNewFeed(url):
|
2004-04-09 17:43:51 +02:00
|
|
|
newFeeds.setdefault((url, name), []).append(channel)
|
|
|
|
for ((url, name), channels) in newFeeds.iteritems():
|
2004-04-09 07:22:56 +02:00
|
|
|
# We check if we can acquire the lock right here because if we
|
|
|
|
# don't, we'll possibly end up spawning a lot of threads to get
|
|
|
|
# the feed, because this thread may run for a number of bytecodes
|
|
|
|
# before it switches to a thread that'll get the lock in
|
|
|
|
# _newHeadlines.
|
2004-04-13 20:25:49 +02:00
|
|
|
if self.acquireLock(url, blocking=False):
|
2004-04-09 07:22:56 +02:00
|
|
|
try:
|
|
|
|
t = threading.Thread(target=self._newHeadlines,
|
|
|
|
name='Fetching <%s>' % url,
|
|
|
|
args=(irc, channels, name, url))
|
2004-12-06 04:29:45 +01:00
|
|
|
self.log.info('Checking for announcements at <%s>', url)
|
2004-04-09 07:22:56 +02:00
|
|
|
world.threadsSpawned += 1
|
|
|
|
t.setDaemon(True)
|
|
|
|
t.start()
|
|
|
|
finally:
|
2004-04-13 20:25:49 +02:00
|
|
|
self.releaseLock(url)
|
2004-04-09 17:13:26 +02:00
|
|
|
time.sleep(0.1) # So other threads can run.
|
2004-02-10 03:49:52 +01:00
|
|
|
|
2004-09-24 01:53:51 +02:00
|
|
|
def buildHeadlines(self, headlines, channel, config='announce.showLinks'):
|
|
|
|
newheadlines = []
|
2005-01-09 17:42:20 +01:00
|
|
|
if self.registryValue(config, channel):
|
2004-09-24 01:53:51 +02:00
|
|
|
for headline in headlines:
|
|
|
|
if headline[1]:
|
|
|
|
newheadlines.append('%s <%s>' % headline)
|
|
|
|
else:
|
|
|
|
newheadlines.append('%s' % headline[0])
|
|
|
|
else:
|
|
|
|
for headline in headlines:
|
|
|
|
newheadlines = ['%s' % h[0] for h in headlines]
|
|
|
|
return newheadlines
|
|
|
|
|
2004-04-05 23:36:27 +02:00
|
|
|
def _newHeadlines(self, irc, channels, name, url):
|
2004-02-10 03:49:52 +01:00
|
|
|
try:
|
2004-04-09 07:22:56 +02:00
|
|
|
# We acquire the lock here so there's only one announcement thread
|
|
|
|
# in this code at any given time. Otherwise, several announcement
|
|
|
|
# threads will getFeed (all blocking, in turn); then they'll all
|
|
|
|
# want to sent their news messages to the appropriate channels.
|
2004-04-09 17:13:26 +02:00
|
|
|
# Note that we're allowed to acquire this lock twice within the
|
|
|
|
# same thread because it's an RLock and not just a normal Lock.
|
2004-04-13 20:25:49 +02:00
|
|
|
self.acquireLock(url)
|
2004-02-10 03:49:52 +01:00
|
|
|
try:
|
2004-04-09 07:22:56 +02:00
|
|
|
oldresults = self.cachedFeeds[url]
|
|
|
|
oldheadlines = self.getHeadlines(oldresults)
|
|
|
|
except KeyError:
|
|
|
|
oldheadlines = []
|
|
|
|
newresults = self.getFeed(url)
|
|
|
|
newheadlines = self.getHeadlines(newresults)
|
2004-06-01 18:22:48 +02:00
|
|
|
def canonicalize(headline):
|
2004-09-24 01:53:51 +02:00
|
|
|
return (tuple(headline[0].lower().split()), headline[1])
|
2004-06-01 18:22:48 +02:00
|
|
|
oldheadlines = sets.Set(map(canonicalize, oldheadlines))
|
|
|
|
for (i, headline) in enumerate(newheadlines):
|
|
|
|
if canonicalize(headline) in oldheadlines:
|
2004-04-20 11:51:55 +02:00
|
|
|
newheadlines[i] = None
|
|
|
|
newheadlines = filter(None, newheadlines) # Removes Nones.
|
2004-04-09 07:22:56 +02:00
|
|
|
if newheadlines:
|
|
|
|
for channel in channels:
|
|
|
|
bold = self.registryValue('bold', channel)
|
|
|
|
sep = self.registryValue('headlineSeparator', channel)
|
|
|
|
prefix = self.registryValue('announcementPrefix', channel)
|
|
|
|
pre = '%s%s: ' % (prefix, name)
|
|
|
|
if bold:
|
|
|
|
pre = ircutils.bold(pre)
|
2004-09-11 23:17:54 +02:00
|
|
|
sep = ircutils.bold(sep)
|
2005-01-09 17:42:20 +01:00
|
|
|
headlines = self.buildHeadlines(newheadlines, channel)
|
|
|
|
irc.replies(headlines, prefixer=pre, joiner=sep,
|
2004-04-09 07:22:56 +02:00
|
|
|
to=channel, prefixName=False, private=True)
|
|
|
|
finally:
|
2004-04-13 20:25:49 +02:00
|
|
|
self.releaseLock(url)
|
2004-07-21 21:12:57 +02:00
|
|
|
|
2004-02-10 03:49:52 +01:00
|
|
|
def willGetNewFeed(self, url):
|
2004-01-04 13:34:01 +01:00
|
|
|
now = time.time()
|
2004-01-21 20:11:27 +01:00
|
|
|
wait = self.registryValue('waitPeriod')
|
2004-01-04 13:34:01 +01:00
|
|
|
if url not in self.lastRequest or now - self.lastRequest[url] > wait:
|
2004-02-10 03:49:52 +01:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2004-02-10 04:14:46 +01:00
|
|
|
|
2004-04-13 20:25:49 +02:00
|
|
|
def acquireLock(self, url, blocking=True):
|
|
|
|
try:
|
|
|
|
self.gettingLockLock.acquire()
|
|
|
|
try:
|
|
|
|
lock = self.locks[url]
|
|
|
|
except KeyError:
|
|
|
|
lock = threading.RLock()
|
|
|
|
self.locks[url] = lock
|
|
|
|
return lock.acquire(blocking=blocking)
|
|
|
|
finally:
|
|
|
|
self.gettingLockLock.release()
|
|
|
|
|
|
|
|
def releaseLock(self, url):
|
|
|
|
self.locks[url].release()
|
2004-07-21 21:12:57 +02:00
|
|
|
|
2004-02-10 03:49:52 +01:00
|
|
|
def getFeed(self, url):
|
2004-08-04 08:19:31 +02:00
|
|
|
def error(s):
|
|
|
|
return {'items': [{'title': s}]}
|
2004-04-09 07:22:56 +02:00
|
|
|
try:
|
|
|
|
# This is the most obvious place to acquire the lock, because a
|
|
|
|
# malicious user could conceivably flood the bot with rss commands
|
|
|
|
# and DoS the website in question.
|
2004-04-13 20:25:49 +02:00
|
|
|
self.acquireLock(url)
|
2004-04-09 07:22:56 +02:00
|
|
|
if self.willGetNewFeed(url):
|
2004-02-10 04:14:46 +01:00
|
|
|
try:
|
2004-08-30 05:33:46 +02:00
|
|
|
self.log.debug('Downloading new feed from <%s>', url)
|
2004-02-10 04:14:46 +01:00
|
|
|
results = rssparser.parse(url)
|
|
|
|
except sgmllib.SGMLParseError:
|
|
|
|
self.log.exception('Uncaught exception from rssparser:')
|
2004-08-19 01:15:27 +02:00
|
|
|
raise callbacks.Error, 'Invalid (unparsable) RSS feed.'
|
2004-08-04 08:19:31 +02:00
|
|
|
except socket.timeout:
|
|
|
|
return error('Timeout downloading feed.')
|
2004-02-10 04:14:46 +01:00
|
|
|
self.cachedFeeds[url] = results
|
|
|
|
self.lastRequest[url] = time.time()
|
2004-04-09 07:22:56 +02:00
|
|
|
try:
|
|
|
|
return self.cachedFeeds[url]
|
|
|
|
except KeyError:
|
|
|
|
self.lastRequest[url] = 0
|
2004-08-04 08:19:31 +02:00
|
|
|
return error('Unable to download feed.')
|
2004-04-09 07:22:56 +02:00
|
|
|
finally:
|
2004-04-13 20:25:49 +02:00
|
|
|
self.releaseLock(url)
|
2004-01-04 13:34:01 +01:00
|
|
|
|
|
|
|
def getHeadlines(self, feed):
|
2004-08-17 04:01:26 +02:00
|
|
|
headlines = []
|
|
|
|
for d in feed['items']:
|
|
|
|
if 'title' in d:
|
|
|
|
title = utils.htmlToText(d['title']).strip()
|
2004-09-02 06:06:20 +02:00
|
|
|
link = d.get('link')
|
2004-09-24 01:53:51 +02:00
|
|
|
if link:
|
|
|
|
headlines.append((title, link))
|
2004-08-17 04:01:26 +02:00
|
|
|
else:
|
2004-09-24 01:53:51 +02:00
|
|
|
headlines.append((title, None))
|
2004-08-17 04:01:26 +02:00
|
|
|
return headlines
|
2004-01-04 13:34:01 +01:00
|
|
|
|
2004-01-21 20:11:27 +01:00
|
|
|
def makeFeedCommand(self, name, url):
|
2004-10-29 03:35:37 +02:00
|
|
|
docstring = """[<number of headlines>]
|
2003-12-03 22:21:43 +01:00
|
|
|
|
2004-04-19 21:24:52 +02:00
|
|
|
Reports the titles for %s at the RSS feed <%s>. If
|
|
|
|
<number of headlines> is given, returns only that many headlines.
|
|
|
|
RSS feeds are only looked up every supybot.plugins.RSS.waitPeriod
|
|
|
|
seconds, which defaults to 1800 (30 minutes) since that's what most
|
|
|
|
websites prefer.
|
2003-12-03 22:21:43 +01:00
|
|
|
""" % (name, url)
|
2004-04-13 20:25:49 +02:00
|
|
|
if url not in self.locks:
|
|
|
|
self.locks[url] = threading.RLock()
|
2004-09-30 10:41:07 +02:00
|
|
|
if hasattr(self.__class__, name) and \
|
|
|
|
not hasattr(getattr(self, name), 'url'):
|
2004-09-13 03:07:04 +02:00
|
|
|
s = 'I already have a command in this plugin named %s.' % name
|
2004-01-21 20:11:27 +01:00
|
|
|
raise callbacks.Error, s
|
2003-12-03 22:21:43 +01:00
|
|
|
def f(self, irc, msg, args):
|
|
|
|
args.insert(0, url)
|
|
|
|
self.rss(irc, msg, args)
|
2004-01-16 17:39:00 +01:00
|
|
|
f = utils.changeFunctionName(f, name, docstring)
|
2004-01-04 13:34:01 +01:00
|
|
|
f.url = url # Used by __call__.
|
2003-12-03 22:21:43 +01:00
|
|
|
self.feedNames.add(name)
|
|
|
|
setattr(self.__class__, name, f)
|
2004-08-03 07:52:03 +02:00
|
|
|
self._registerFeed(name, url)
|
2004-07-21 21:12:57 +02:00
|
|
|
|
2004-10-29 03:35:37 +02:00
|
|
|
def add(self, irc, msg, args, name, url):
|
2004-01-21 20:11:27 +01:00
|
|
|
"""<name> <url>
|
|
|
|
|
|
|
|
Adds a command to this plugin that will look up the RSS feed at the
|
|
|
|
given URL.
|
|
|
|
"""
|
|
|
|
self.makeFeedCommand(name, url)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2005-01-03 19:45:07 +01:00
|
|
|
add = wrap(add, ['feedName', 'url'])
|
2003-12-03 22:21:43 +01:00
|
|
|
|
2004-10-29 03:35:37 +02:00
|
|
|
def remove(self, irc, msg, args, name):
|
2003-12-03 22:21:43 +01:00
|
|
|
"""<name>
|
|
|
|
|
|
|
|
Removes the command for looking up RSS feeds at <name> from
|
|
|
|
this plugin.
|
|
|
|
"""
|
|
|
|
if name not in self.feedNames:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('That\'s not a valid RSS feed command name.')
|
2003-12-03 22:21:43 +01:00
|
|
|
return
|
2004-04-22 04:59:15 +02:00
|
|
|
self.feedNames.remove(name)
|
2003-12-03 22:21:43 +01:00
|
|
|
delattr(self.__class__, name)
|
2004-04-22 04:59:15 +02:00
|
|
|
conf.supybot.plugins.RSS.feeds.unregister(name)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2005-01-03 19:45:07 +01:00
|
|
|
remove = wrap(remove, ['feedName'])
|
2004-02-07 23:38:49 +01:00
|
|
|
|
2004-10-29 03:35:37 +02:00
|
|
|
def announce(self, irc, msg, args, channel, optlist, rest):
|
2004-07-21 21:12:57 +02:00
|
|
|
"""[<channel>] [--remove] [<name|url> ...]
|
2004-02-07 23:38:49 +01:00
|
|
|
|
2005-01-03 05:56:37 +01:00
|
|
|
Adds the list of <name|url> to the current list of announced feeds in
|
|
|
|
the channel given. Valid feeds include the names of registered feeds
|
|
|
|
as well as URLs for a RSS feeds. <channel> is only necessary if the
|
|
|
|
message isn't sent in the channel itself. If no arguments are
|
|
|
|
specified, replies with the current list of feeds to announce. If
|
|
|
|
--remove is given, the specified feeds will be removed from the list
|
|
|
|
of feeds to announce.
|
2004-02-07 23:38:49 +01:00
|
|
|
"""
|
2004-07-21 21:12:57 +02:00
|
|
|
remove = False
|
|
|
|
announce = conf.supybot.plugins.RSS.announce
|
|
|
|
for (option, _) in optlist:
|
2004-10-29 03:35:37 +02:00
|
|
|
if option == 'remove':
|
2005-01-03 05:56:37 +01:00
|
|
|
if not rest:
|
|
|
|
raise callbacks.ArgumentError
|
2004-07-21 21:12:57 +02:00
|
|
|
remove = True
|
2005-01-03 05:56:37 +01:00
|
|
|
def addFeed(feed):
|
|
|
|
if feed not in feeds:
|
|
|
|
feeds.add(feed)
|
|
|
|
def removeFeed(feed):
|
|
|
|
if feed in feeds:
|
|
|
|
feeds.remove(feed)
|
|
|
|
if rest:
|
|
|
|
if remove:
|
|
|
|
updater = removeFeed
|
2004-07-21 21:12:57 +02:00
|
|
|
else:
|
2005-01-03 05:56:37 +01:00
|
|
|
updater = addFeed
|
|
|
|
feeds = announce.get(channel)()
|
|
|
|
for feed in rest:
|
|
|
|
updater(feed)
|
|
|
|
announce.get(channel).setValue(feeds)
|
|
|
|
irc.replySuccess()
|
2004-07-21 21:12:57 +02:00
|
|
|
elif not rest:
|
|
|
|
feeds = utils.commaAndify(announce.get(channel)())
|
|
|
|
irc.reply(feeds or 'I am currently not announcing any feeds.')
|
|
|
|
return
|
2004-10-29 03:35:37 +02:00
|
|
|
announce = wrap(announce, [('checkChannelCapability', 'op'),
|
2005-01-03 19:45:07 +01:00
|
|
|
getopts({'remove':''}),
|
|
|
|
any(first('url', 'feedName'))])
|
2004-07-21 21:12:57 +02:00
|
|
|
|
2004-10-29 03:35:37 +02:00
|
|
|
def rss(self, irc, msg, args, url, n):
|
2004-04-19 21:24:52 +02:00
|
|
|
"""<url> [<number of headlines>]
|
2003-04-08 09:08:46 +02:00
|
|
|
|
|
|
|
Gets the title components of the given RSS feed.
|
2004-04-19 21:24:52 +02:00
|
|
|
If <number of headlines> is given, return only that many headlines.
|
2003-04-08 09:08:46 +02:00
|
|
|
"""
|
2004-01-21 20:11:27 +01:00
|
|
|
self.log.debug('Fetching %s', url)
|
2004-01-04 13:34:01 +01:00
|
|
|
feed = self.getFeed(url)
|
2004-12-16 08:56:57 +01:00
|
|
|
if irc.isChannel(msg.args[0]):
|
2004-01-04 13:34:01 +01:00
|
|
|
channel = msg.args[0]
|
|
|
|
else:
|
|
|
|
channel = None
|
|
|
|
headlines = self.getHeadlines(feed)
|
2003-09-07 07:37:49 +02:00
|
|
|
if not headlines:
|
2004-09-28 09:43:55 +02:00
|
|
|
irc.error('Couldn\'t get RSS feed.')
|
2003-04-14 07:17:49 +02:00
|
|
|
return
|
2004-09-24 01:53:51 +02:00
|
|
|
headlines = self.buildHeadlines(headlines, channel, 'showLinks')
|
2004-04-19 21:24:52 +02:00
|
|
|
if n:
|
|
|
|
headlines = headlines[:n]
|
2004-01-21 20:11:27 +01:00
|
|
|
sep = self.registryValue('headlineSeparator', channel)
|
2004-09-13 00:33:39 +02:00
|
|
|
if self.registryValue('bold', channel):
|
|
|
|
sep = ircutils.bold(sep)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(sep.join(headlines))
|
2004-10-29 03:35:37 +02:00
|
|
|
rss = wrap(rss, ['url', additional('int')])
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-10-29 03:35:37 +02:00
|
|
|
def info(self, irc, msg, args, url):
|
2004-04-21 09:38:18 +02:00
|
|
|
"""<url|feed>
|
2003-04-14 07:17:49 +02:00
|
|
|
|
|
|
|
Returns information from the given RSS feed, namely the title,
|
|
|
|
URL, description, and last update date, if available.
|
|
|
|
"""
|
2004-04-21 09:38:18 +02:00
|
|
|
try:
|
|
|
|
url = self.registryValue('feeds.%s' % url)
|
|
|
|
except registry.NonExistentRegistryEntry:
|
|
|
|
pass
|
2004-01-04 13:34:01 +01:00
|
|
|
feed = self.getFeed(url)
|
2003-04-14 07:17:49 +02:00
|
|
|
info = feed['channel']
|
|
|
|
if not info:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('I couldn\'t retrieve that RSS feed.')
|
2003-04-14 07:17:49 +02:00
|
|
|
return
|
|
|
|
# check the 'modified' key, if it's there, convert it here first
|
2003-04-19 23:40:39 +02:00
|
|
|
if 'modified' in feed:
|
|
|
|
seconds = time.mktime(feed['modified'])
|
2003-08-28 19:55:01 +02:00
|
|
|
now = time.mktime(time.gmtime())
|
|
|
|
when = utils.timeElapsed(now - seconds) + ' ago'
|
2003-04-14 07:17:49 +02:00
|
|
|
else:
|
2005-01-03 19:45:07 +01:00
|
|
|
when = 'time unavailable'
|
2003-04-14 07:17:49 +02:00
|
|
|
# The rest of the entries are all available in the channel key
|
2003-04-19 23:40:39 +02:00
|
|
|
response = 'Title: %s; URL: <%s>; ' \
|
|
|
|
'Description: %s; Last updated %s.' % (
|
2003-08-20 18:26:23 +02:00
|
|
|
info.get('title', 'unavailable').strip(),
|
2003-04-19 23:40:39 +02:00
|
|
|
info.get('link', 'unavailable').strip(),
|
|
|
|
info.get('description', 'unavailable').strip(),
|
|
|
|
when)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(' '.join(response.split()))
|
2005-01-03 19:45:07 +01:00
|
|
|
info = wrap(info, [first('url', 'feedName')])
|
2003-04-06 13:42:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
Class = RSS
|
|
|
|
|
2003-04-14 07:17:49 +02:00
|
|
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=78:
|