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.
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
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
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):
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')
58 reply
= "There is no quote by that id."
61 quote
= dom
.getElementsByTagName('miniqdb')[0].getElementsByTagName('quote')[0]
62 lines
= int(quote
.getAttribute('lines'))
64 reply
= root
+'/quote.php?id='+str(id) + ' (' + str(lines
) + ' lines)'
66 reply
= quote
.firstChild
.data
.replace('<','<').replace('>','>')
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
):
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
):
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')
99 reply
= root
+'/quote.php?id='+str(id) + ' (' + str(lines
) + ' lines)'
101 reply
= quote
.firstChild
.data
.replace('<','<').replace('>','>')
102 for line
in reply
.split('\n'):
103 irc
.reply(line
, False, False)
109 # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: