Logo Search packages:      
Sourcecode: ibid version File versions  Download package


# Copyright (c) 2008-2010, Michael Gorven, Stefano Rivera, Max Rabkin
# Released under terms of the MIT/X/Expat Licence. See COPYING for details.

from urllib2 import urlopen, HTTPError
from urllib import urlencode, quote
from httplib import BadStatusLine
from urlparse import urljoin
from random import choice, shuffle, randint
from sys import exc_info
from subprocess import Popen, PIPE
import logging
import re

from ibid.compat import ElementTree
from ibid.config import Option, BoolOption
from ibid.plugins import Processor, match, RPC
from ibid.utils.html import get_html_parse_tree
from ibid.utils import file_in_path, unicode_output

log = logging.getLogger('plugins.quotes')

help = {}

help['fortune'] = u'Returns a random fortune.'
class Fortune(Processor, RPC):
    feature = 'fortune'

    fortune = Option('fortune', 'Path of the fortune executable', 'fortune')

    def __init__(self, name):
        super(Fortune, self).__init__(name)

    def setup(self):
        if not file_in_path(self.fortune):
            raise Exception("Cannot locate fortune executable")

    def handler(self, event):
        fortune = self.remote_fortune()
        if fortune:
            event.addresponse(u"Couldn't execute fortune")

    def remote_fortune(self):
        fortune = Popen(self.fortune, stdout=PIPE, stderr=PIPE)
        output, error = fortune.communicate()
        code = fortune.wait()

        output = unicode_output(output.strip(), 'replace')

        if code == 0:
            return output
            return None

help['bash'] = u'Retrieve quotes from bash.org.'
class Bash(Processor):
    u"bash[.org] [(random|<number>)]"

    feature = 'bash'

    public_browse = BoolOption('public_browse', 'Allow random quotes in public', True)

    def bash(self, event, id):
        id = id is None and u'random' or id.lower()

        if id == u'random' and event.public and not self.public_browse:
            event.addresponse(u'Sorry, not in public. PM me')

        soup = get_html_parse_tree('http://bash.org/?%s' % id)

        number = u"".join(soup.find('p', 'quote').find('b').contents)
        output = [u'%s:' % number]

        body = soup.find('p', 'qt')
        if not body:
            event.addresponse(u"There's no such quote, but if you keep talking like that maybe there will be")
            for line in body.contents:
                line = unicode(line).strip()
                if line != u'<br />':
            event.addresponse(u'\n'.join(output), conflate=False)

help['fml'] = u'Retrieves quotes from fmylife.com.'
class FMLException(Exception):

class FMyLife(Processor):
    u"""fml (<number> | [random] | flop | top | last | love | money | kids | work | health | sex | miscellaneous )"""

    feature = "fml"

    api_url = Option('fml_api_url', 'FML API URL base', 'http://api.betacie.com/')
    # The Ibid API Key, registered by Stefano Rivera:
    api_key = Option('fml_api_key', 'FML API Key', '4b39a7fcaf01c')
    fml_lang = Option('fml_lang', 'FML Lanugage', 'en')

    public_browse = BoolOption('public_browse', 'Allow random quotes in public', True)

    failure_messages = (
            u'Today, I tried to get a quote for %(nick)s but failed. FML',
            u'Today, FML is down. FML',
            u"Sorry, it's broken, the FML admins must having a really bad day",

    def remote_get(self, id):
        url = urljoin(self.api_url, 'view/%s?%s' % (
            id.isalnum() and id + '/nocomment' or quote(id),
            urlencode({'language': self.fml_lang, 'key': self.api_key}))
        f = urlopen(url)
            tree = ElementTree.parse(f)
        except SyntaxError:
            class_, e, tb = exc_info()
            new_exc = FMLException(u'XML Parsing Error: %s' % unicode(e))
            raise new_exc.__class__, new_exc, tb

        if tree.find('.//error'):
            raise FMLException(tree.findtext('.//error'))

        item = tree.find('.//item')
        if item:
            url = u"http://www.fmylife.com/%s/%s" % (
            text = item.find('text').text
            return u'%s\n- %s' % (text, url)

    def fml(self, event, id):
            body = self.remote_get(id)
        except (FMLException, HTTPError, BadStatusLine):
            event.addresponse(choice(self.failure_messages) % event.sender)

        if body:
        elif id.isdigit():
            event.addresponse(u'No such quote')
            event.addresponse(choice(self.failure_messages) % event.sender)

    def fml_default(self, event):
        if not event.public or self.public_browse:
            self.fml(event, 'random')
            event.addresponse(u'Sorry, not in public. PM me')

help["tfln"] = u"Looks up quotes from textsfromlastnight.com"
class TextsFromLastNight(Processor):
    u"""tfln [(random|<number>)]
    tfln (worst|best) [(today|this week|this month)]"""

    feature = 'tfln'

    public_browse = BoolOption('public_browse', 'Allow random quotes in public', True)

    random_pool = []

    def get_tfln(self, section):
        tree = get_html_parse_tree('http://textsfromlastnight.com/%s/' % section.lower())
        for div in tree.findAll('div', attrs={'class': 'post_wrap'}):
            id = int(div.get('id').split('_', 1)[1])
            message = []
            line = ''
            for a in div.findAll('div', attrs={'class': 'post_content'})[0].findAll('a'):
                if a['href'].startswith('/areacode/'):
                    line = u'%s: ' % a.contents[0]
                    message.append(line + a.contents[0])
            yield id, message

    def tfln(self, event, number, timeframe=None):
        number = number is None and u'random' or number.lower()

        if number == u'random' and not timeframe \
                and event.public and not self.public_browse:
            event.addresponse(u'Sorry, not in public. PM me')

        if number in (u'worst', u'best'):
            number += u'-nights'
            if timeframe.lower() in (u'week', u'month'):
                number += u'this-' + timeframe.lower()
        elif number.isdigit():
            number = 'view/%s' % number

        if number == u'random':
            if not self.random_pool:
                self.random_pool = [message for message in self.get_tfln(number)]

            message = self.random_pool.pop()
                message = self.get_tfln(number).next()
            except StopIteration:
                event.addresponse(u'No such quote')

        id, body = message
        if len(body) > 1:
            for line in body:
            event.addresponse(u'- http://textsfromlastnight.com/view/%i', id)
            event.addresponse(u'%(body)s\n- http://textsfromlastnight.com/view/%(id)i', {
                'id': id,
                'body': body[0],

    def tfln_url(self, event, id):
        self.tfln(event, id)

help["mlia"] = u"Looks up quotes from MyLifeIsAverage.com and MyLifeIsG.com"
class MyLifeIsAverage(Processor):
    u"""mlia [(<number> | random | recent | today | yesterday | this week | this month | this year )]
    mlig [(<number> | random | recent | today | yesterday | this week | this month | this year )]"""

    feature = 'mlia'

    public_browse = BoolOption('public_browse',
                               'Allow random quotes in public', True)

    random_pool = {}
    pages = {}

    def find_stories(self, url, site='mlia'):
        if isinstance(url, basestring):
            tree = get_html_parse_tree(url, treetype='etree')
            tree = url

        stories = [div for div in tree.findall('.//div')
                       if div.get(u'class') in
                            (u'story s', # mlia
                             u'stories', u'stories-wide')] # mlig

        for story in stories:
            if site == 'mlia':
                body = story.findtext('div').strip()
                body = story.findtext('div/span/span').strip()
            id = story.findtext('.//a')
            if isinstance(id, basestring) and id[1:].isdigit():
                id = int(id[1:])
                yield id, body

    def mlia(self, event, site, query):
        query = query is None and u'random' or query.lower()

        if query == u'random' and event.public and not self.public_browse:
            event.addresponse(u'Sorry, not in public. PM me')

        site = site.lower()
        url = {
                'mlia': 'http://mylifeisaverage.com/',
                'mlig': 'http://mylifeisg.com/',

        if query == u'random' or query is None:
            if not self.random_pool.get(site):
                if site == 'mlia':
                    purl = url + str(randint(1, self.pages.get(site, 1)))
                    purl = url + 'index.php?' + urlencode({
                            'page': randint(1, self.pages.get(site, 1))
                tree = get_html_parse_tree(purl, treetype='etree')
                self.random_pool[site] = [story for story
                        in self.find_stories(tree, site=site)]

                if site == 'mlia':
                    pagination = [ul for ul in tree.findall('.//ul')
                                           if ul.get(u'class') == u'pages'][0]
                    self.pages[site] = int(
                        [li for li in pagination.findall('li')
                            if li.get(u'class') == u'last'][0]
                    pagination = [div for div in tree.findall('.//div')
                                      if div.get(u'class') == u'pagination'][0]
                    self.pages[site] = sorted(int(a.text) for a
                            in pagination.findall('.//a')
                            if a.text.isdigit())[-1]

            story = self.random_pool[site].pop()

                if site == 'mlia':
                    if query.isdigit():
                        surl = url + '/s/' + query
                        surl = url + '/best/' + query
                    if query.isdigit():
                        surl = url + 'story.php?' + urlencode({'id': query})
                        surl = url + 'index.php?' + urlencode({'part': query})

                story = self.find_stories(surl, site=site).next()

            except StopIteration:
                event.addresponse(u'No such quote')

        id, body = story
        if site == 'mlia':
            url += 's/%i' % id
            url += 'story.php?id=%i' % id
        event.addresponse(u'%(body)s\n- %(url)s', {
            'url': url,
            'body': body,

    def mlia_url(self, event, site, id):
        self.mlia(event, 'mli' + site[0].lower(), id)

help['bible'] = u'Retrieves Bible verses'
class Bible(Processor):
    u"""bible <passages> [in <version>]
    <book> <verses> [in <version>]"""

    feature = 'bible'
    # http://labs.bible.org/api/ is an alternative
    # Their feature set is a little different, but they should be fairly
    # compatible
    api_url = Option('bible_api_url', 'Bible API URL base',

    psalm_pat = re.compile(r'\bpsalm\b', re.IGNORECASE)

    # The API doesn't seem to work with the apocrypha, even when looking in
    # versions that include it
    books = '|'.join(['Genesis', 'Exodus', 'Leviticus', 'Numbers', 'Deuteronomy',
    'Joshua', 'Judges', 'Ruth', '(?:1|2|I|II) Samuel', '(?:1|2|I|II) Kings',
    '(?:1|2|I|II) Chronicles', 'Ezra', 'Nehemiah', 'Esther', 'Job', 'Psalms?',
    'Proverbs', 'Ecclesiastes', 'Song(?: of (?:Songs|Solomon)?)?',
    'Canticles', 'Isaiah', 'Jeremiah', 'Lamentations',
    'Ezekiel', 'Daniel', 'Hosea', 'Joel', 'Amos', 'Obadiah', 'Jonah', 'Micah',
    'Nahum', 'Habakkuk', 'Zephaniah', 'Haggai', 'Zechariah', 'Malachi',
    'Matthew', 'Mark', 'Luke', 'John', 'Acts', 'Romans',
    '(?:1|2|I|II) Corinthians', 'Galatians', 'Ephesians', 'Philippians',
    'Colossians', '(?:1|2|I|II) Thessalonians', '(?:1|2|I|II) Timothy',
    'Titus', 'Philemon', 'Hebrews', 'James', '(?:1|2|I|II) Peter',
    '(?:1|2|3|I|II|III) John', 'Jude',
    'Revelations?(?: of (?:St.|Saint) John)?']).replace(' ', '\s*')

    def bible(self, event, passage, version=None):
        passage = self.psalm_pat.sub('psalms', passage)

        params = {'passage': passage.encode('utf-8'),
                  'type': 'xml',
                  'formatting': 'plain'}
        if version:
            params['version'] = version.lower().encode('utf-8')

        f = urlopen(self.api_url + '?' + urlencode(params))
        tree = ElementTree.parse(f)

        message = self.formatPassage(tree)
        if message:
        errors = list(tree.findall('.//error'))
        if errors:
            event.addresponse('There were errors: %s.', '. '.join(err.text for err in errors))
        elif not message:
            event.addresponse("I couldn't find that passage.")

    # Allow queries which are quite definitely bible references to omit "bible".
    # Specifically, they must start with the name of a book and be followed only
    # by book names, chapters and verses.
    @match(r'^((?:(?:' + books + ')(?:\d|[-:,]|\s)*)+?)(?:\s+(?:in|from)\s+(.*))?$')
    def bookbible(self, *args):

    def formatPassage(self, xml):
        message = []
        oldref = (None, None, None)
        for item in xml.findall('.//item'):
            ref, text = self.verseInfo(item)
            if oldref[0] != ref[0]:
                message.append(u'(%s %s:%s)' % ref)
            elif oldref[1] != ref[1]:
                message.append(u'(%s:%s)' % ref[1:])
                message.append(u'%s' % ref[2])
            oldref = ref


        return u' '.join(message)

    def verseInfo(self, xml):
        book, chapter, verse, text = map(xml.findtext,
                                        ('bookname', 'chapter', 'verse', 'text'))
        return ((book, chapter, verse), text)

# vi: set et sta sw=4 ts=4:

Generated by  Doxygen 1.6.0   Back to index