changed the name of the program and tagline to match the new git repo
[miniqdb-supybot.git] / plugin.py
blob5c4c5f16c06a0a4d23ae1f8d780281e461eb4465
1 ###
2 # miniqdb-supybot - A Supybot plugin to access miniqdb's API
3 # Copyright (C) 2008 Ian Weller <ianweller@gmail.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 ###
20 import supybot.utils as utils
21 import supybot.conf as conf
22 from supybot.commands import *
23 import supybot.plugins as plugins
24 import supybot.ircutils as ircutils
25 import supybot.callbacks as callbacks
26 import urllib
27 import httplib
28 import urlparse
29 import re
30 from xml.dom import minidom
33 class Miniqdb(callbacks.Plugin):
34 """Use this plugin in conjunction with a miniqdb instance on a server. You
35 can use the "quote" command to view the link and information about a
36 quote."""
37 threaded = True
39 def _prompt_user_passwd(self, host, realm):
40 username = conf.supybot.plugins.Miniqdb.auth.username()
41 password = conf.supybot.plugins.Miniqdb.auth.password()
42 return (username, password)
43 urllib.FancyURLopener.prompt_user_passwd = _prompt_user_passwd
45 def quote(self, irc, msg, args, id):
46 """<id>
48 Returns a link to and information about quote <id>."""
49 root = conf.supybot.plugins.Miniqdb.miniqdbRoot()
50 maxlines = conf.supybot.plugins.Miniqdb.maxLines()
51 url = str(root) + '/api.php?method=rest&act=quote&id=' +str(id)
52 opener = urllib.FancyURLopener()
53 xml = opener.open(url).read()
54 dom = minidom.parseString(xml)
55 errors = dom.getElementsByTagName('miniqdb')[0].getElementsByTagName('error')
56 prefixNick = False
57 if errors != []:
58 reply = "There is no quote by that id."
59 prefixNick = True
60 else:
61 quote = dom.getElementsByTagName('miniqdb')[0].getElementsByTagName('quote')[0]
62 lines = int(quote.getAttribute('lines'))
63 if lines > maxlines:
64 reply = root+'/quote.php?id='+str(id) + ' (' + str(lines) + ' lines)'
65 else:
66 reply = quote.firstChild.data.replace('&lt;','<').replace('&gt;','>')
67 for line in reply.split('\n'):
68 irc.reply(line, False, prefixNick)
69 quote = wrap(quote, ['id'])
71 def stats(self, irc, msg, args):
72 """takes no arguments
74 Returns some statistics from the QDB."""
75 root = conf.supybot.plugins.Miniqdb.miniqdbRoot()
76 maxlines = conf.supybot.plugins.Miniqdb.maxLines()
77 url = str(root) + '/api.php?method=rest&act=stats'
78 opener = urllib.FancyURLopener()
79 xml = opener.open(url).read()
80 dom = minidom.parseString(xml)
81 stats = dom.getElementsByTagName('miniqdb')[0].getElementsByTagName('stats')[0]
82 count = stats.getAttribute('count')
83 irc.reply(format("There are %s quotes in the database.", count))
85 def random(self, irc, msg, args):
86 """takes no arguments
88 Returns a random quote."""
89 root = conf.supybot.plugins.Miniqdb.miniqdbRoot()
90 maxlines = conf.supybot.plugins.Miniqdb.maxLines()
91 url = str(root) + '/api.php?method=rest&act=random&count=1'
92 opener = urllib.FancyURLopener()
93 xml = opener.open(url).read()
94 dom = minidom.parseString(xml)
95 quote = dom.getElementsByTagName('miniqdb')[0].getElementsByTagName('quote')[0]
96 lines = int(quote.getAttribute('lines'))
97 id = quote.getAttribute('id')
98 if lines > maxlines:
99 reply = root+'/quote.php?id='+str(id) + ' (' + str(lines) + ' lines)'
100 else:
101 reply = quote.firstChild.data.replace('&lt;','<').replace('&gt;','>')
102 for line in reply.split('\n'):
103 irc.reply(line, False, False)
106 Class = Miniqdb
109 # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: