2003-04-06 13:42:41 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# 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.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
2003-04-08 09:08:46 +02:00
|
|
|
Provides basic functionality for handling RSS/RDF feeds. Depends on the Alias
|
2003-08-30 21:53:53 +02:00
|
|
|
module for user-friendliness.
|
2003-04-06 13:42:41 +02:00
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import 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
|
2003-12-03 22:21:43 +01:00
|
|
|
import types
|
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
|
|
|
|
|
2003-12-03 22:21:43 +01:00
|
|
|
import conf
|
2003-04-19 23:40:39 +02:00
|
|
|
import utils
|
2003-04-06 13:42:41 +02:00
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
|
|
|
|
2003-04-08 09:08:46 +02:00
|
|
|
def configure(onStart, afterConnect, advanced):
|
|
|
|
from questions import expect, anything, something, yn
|
|
|
|
onStart.append('load RSS')
|
|
|
|
prompt = 'Would you like to add an RSS feed?'
|
|
|
|
while yn(prompt) == 'y':
|
|
|
|
prompt = 'Would you like to add another RSS feed?'
|
2003-08-28 23:27:15 +02:00
|
|
|
name = something('What\'s the name of the website?')
|
|
|
|
url = something('What\'s the URL of the RSS feed?')
|
2003-12-03 22:21:43 +01:00
|
|
|
onStart.append('rss add %s %s' % (name, url))
|
|
|
|
#onStart.append('alias lock %s' % name)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-06 13:42:41 +02:00
|
|
|
class RSS(callbacks.Privmsg):
|
|
|
|
threaded = True
|
2003-04-08 09:08:46 +02:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
2003-12-03 22:21:43 +01:00
|
|
|
self.feedNames = sets.Set()
|
2003-04-08 09:08:46 +02:00
|
|
|
self.lastRequest = {}
|
2003-04-14 07:17:49 +02:00
|
|
|
self.cachedFeeds = {}
|
2003-04-08 09:08:46 +02:00
|
|
|
|
2003-12-03 22:21:43 +01:00
|
|
|
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)
|
|
|
|
docstring = """takes no arguments
|
|
|
|
|
|
|
|
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
|
|
|
|
websites prefer it).
|
|
|
|
""" % (name, url)
|
|
|
|
name = callbacks.canonicalName(name)
|
2003-12-04 08:03:37 +01:00
|
|
|
if hasattr(self, name):
|
|
|
|
s = 'I already have a command in this plugin named %s' % name
|
|
|
|
irc.error(msg, s)
|
|
|
|
return
|
2003-12-03 22:21:43 +01:00
|
|
|
def f(self, irc, msg, args):
|
|
|
|
args.insert(0, url)
|
|
|
|
self.rss(irc, msg, args)
|
|
|
|
f = types.FunctionType(f.func_code, f.func_globals,
|
2003-12-16 04:10:02 +01:00
|
|
|
name, closure=f.func_closure)
|
2003-12-03 22:21:43 +01:00
|
|
|
f.__doc__ = docstring
|
|
|
|
self.feedNames.add(name)
|
|
|
|
setattr(self.__class__, name, f)
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
|
|
|
|
def remove(self, irc, msg, args):
|
|
|
|
"""<name>
|
|
|
|
|
|
|
|
Removes the command for looking up RSS feeds at <name> from
|
|
|
|
this plugin.
|
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
|
|
|
name = callbacks.canonicalName(name)
|
|
|
|
if name not in self.feedNames:
|
|
|
|
irc.error(msg, 'That\'s not a valid RSS feed command name.')
|
|
|
|
return
|
|
|
|
delattr(self.__class__, name)
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
|
2003-10-21 06:22:54 +02:00
|
|
|
def rss(self, irc, msg, args):
|
2003-04-08 09:08:46 +02:00
|
|
|
"""<url>
|
|
|
|
|
|
|
|
Gets the title components of the given RSS feed.
|
|
|
|
"""
|
|
|
|
url = privmsgs.getArgs(args)
|
|
|
|
now = time.time()
|
|
|
|
if url not in self.lastRequest or now - self.lastRequest[url] > 1800:
|
|
|
|
results = rssparser.parse(url)
|
2003-04-14 07:17:49 +02:00
|
|
|
self.cachedFeeds[url] = results
|
|
|
|
self.lastRequest[url] = now
|
|
|
|
|
|
|
|
feed = self.cachedFeeds[url]
|
2003-11-21 20:07:14 +01:00
|
|
|
headlines = [utils.htmlToText(d['title'].strip())
|
2003-04-14 07:17:49 +02:00
|
|
|
for d in feed['items']]
|
2003-09-07 07:37:49 +02:00
|
|
|
if not headlines:
|
2003-12-09 21:46:40 +01:00
|
|
|
irc.error(msg, 'Couldn\'t get RSS feed')
|
2003-04-14 07:17:49 +02:00
|
|
|
return
|
2003-11-15 05:46:09 +01:00
|
|
|
headlines = imap(utils.htmlToText, headlines)
|
2003-09-07 08:01:25 +02:00
|
|
|
irc.reply(msg, ' :: '.join(headlines))
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-10-21 06:22:54 +02:00
|
|
|
def info(self, irc, msg, args):
|
2003-04-14 07:17:49 +02:00
|
|
|
"""<url>
|
|
|
|
|
|
|
|
Returns information from the given RSS feed, namely the title,
|
|
|
|
URL, description, and last update date, if available.
|
|
|
|
"""
|
|
|
|
url = privmsgs.getArgs(args)
|
|
|
|
now = time.time()
|
|
|
|
if url not in self.lastRequest or now - self.lastRequest[url] > 1800:
|
|
|
|
results = rssparser.parse(url)
|
|
|
|
self.cachedFeeds[url] = results
|
2003-04-08 09:08:46 +02:00
|
|
|
self.lastRequest[url] = now
|
2003-04-14 07:17:49 +02:00
|
|
|
|
|
|
|
feed = self.cachedFeeds[url]
|
|
|
|
info = feed['channel']
|
|
|
|
if not info:
|
|
|
|
irc.error(msg, 'Error grabbing RSS feed')
|
|
|
|
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:
|
2003-04-19 23:40:39 +02: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)
|
2003-04-19 09:01:01 +02:00
|
|
|
irc.reply(msg, ' '.join(response.split()))
|
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:
|