Limnoria/plugins/RSS.py

140 lines
5.7 KiB
Python
Raw Normal View History

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.
###
"""
Provides basic functionality for handling RSS/RDF feeds. Depends on the Alias
module for user-friendliness.
2003-04-06 13:42:41 +02:00
"""
import plugins
2003-04-06 13:42:41 +02:00
2003-04-06 13:49:53 +02:00
import time
2003-04-06 13:42:41 +02:00
import rssparser
2003-04-19 23:40:39 +02:00
import utils
2003-04-06 13:42:41 +02:00
import privmsgs
import callbacks
def configure(onStart, afterConnect, advanced):
from questions import expect, anything, something, yn
onStart.append('load RSS')
if yn('RSS depends on the Alias module. Is that module loaded?') == 'n':
if yn('Do you want to load that module now?') == 'y':
onStart.append('load Alias')
else:
print 'You can still use the RSS module, but you won\'t be asked'
print 'any further questions.'
return
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?')
onStart.append('alias %s "rss %s"' % (name, url))
2003-04-08 09:52:24 +02:00
onStart.append('freeze %s' % name)
2003-08-20 18:26:23 +02:00
2003-08-28 19:55:01 +02:00
example = utils.wrapLines("""
<jemfinch> @list RSS
<supybot> rssinfo, rsstitles
<jemfinch> @rssinfo http://arstechnica.com/etc/rdf/ars.rdf
<supybot> Title: Ars Technica; URL: <http://www.arstechnica.com>; Description: The PC enthusiast's resource; Last updated 1 hour, 2 minutes, and 46 seconds ago.
<jemfinch> @rsstitles http://arstechnica.com/etc/rdf/ars.rdf
<supybot> Macintosh Browser Smackdown :: CAIB releases Columbia accident report :: The power of subpoena: hi, you like porn? :: Et Cetera: forget the bullets, it's tooth time :: Ars Emporium Update (updated / extended) :: Google gets even cooler :: Trade secrets trump free speech protection :: OneNote student pricing gives insight into Microsoft's Tablet strategy :: Et Cetera: Diamonds on my nines and golden bullets
""")
2003-04-06 13:42:41 +02:00
class RSS(callbacks.Privmsg):
threaded = True
def __init__(self):
callbacks.Privmsg.__init__(self)
self.lastRequest = {}
self.cachedFeeds = {}
def rss(self, irc, msg, args):
"""<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)
self.cachedFeeds[url] = results
self.lastRequest[url] = now
feed = self.cachedFeeds[url]
headlines = [d['title'].strip().replace('\n', ' ') \
for d in feed['items']]
2003-04-17 12:06:11 +02:00
headlines = map(utils.htmlToText, headlines)
if not headlines:
irc.error(msg, 'Error grabbing RSS feed')
return
irc.reply(msg, ' :: '.join(headlines))
2003-08-20 18:26:23 +02:00
def info(self, irc, msg, args):
"""<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
self.lastRequest[url] = now
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'
else:
2003-04-19 23:40:39 +02:00
when = "time unavailable"
# 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
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=78: