Add --id option to geekquote, make it always allow multiline quotes

This commit is contained in:
Daniel Berlin 2003-10-22 22:05:34 +00:00
parent de63973a2f
commit 0b380f56fe
2 changed files with 17 additions and 11 deletions

View File

@ -38,6 +38,7 @@ import plugins
import re import re
import sets import sets
import urllib2 import urllib2
import getopt
import utils import utils
import debug import debug
@ -259,27 +260,31 @@ class Http(callbacks.Privmsg):
debug.recoverableException() debug.recoverableException()
irc.error(msg, debug.exnToString(e)) irc.error(msg, debug.exnToString(e))
_geekquotere = re.compile('<p class="qt">(.*?)</p>')
_mlgeekquotere = re.compile('<p class="qt">(.*?)</p>', re.M | re.DOTALL) _mlgeekquotere = re.compile('<p class="qt">(.*?)</p>', re.M | re.DOTALL)
def geekquote(self, irc, msg, args): def geekquote(self, irc, msg, args):
"""[<multiline>] """[--id=<value>]
Returns a random geek quote from bash.org; the optional argument Returns a random geek quote from bash.org; the optional argument
<multiline> specifies whether multi-line quotes (which are longer --id specifies which quote to retrieve.
than other quotes, generally) are to be allowed.
""" """
multiline = privmsgs.getArgs(args, needed=0, optional=1) (optlist, rest) = getopt.getopt(args, '', ['id='])
id = 'random1'
for (option, arg) in optlist:
if option == '--id':
try:
id = int(arg)
except ValueError, e:
irc.error(msg, 'Invalid id: %s' % e)
return
try: try:
fd = urllib2.urlopen('http://bash.org/?random1') fd = urllib2.urlopen('http://bash.org/?%s' % id)
except urllib2.URLError: except urllib2.URLError:
irc.error(msg, 'Error connecting to geekquote server.') irc.error(msg, 'Error connecting to geekquote server.')
return return
html = fd.read() html = fd.read()
fd.close() fd.close()
if multiline: m = self._mlgeekquotere.search(html)
m = self._mlgeekquotere.search(html)
else:
m = self._geekquotere.search(html)
if m is None: if m is None:
irc.error(msg, 'No quote found.') irc.error(msg, 'No quote found.')
return return

View File

@ -52,7 +52,8 @@ class HttpTest(PluginTestCase, PluginDocumentation):
def testGeekquote(self): def testGeekquote(self):
self.assertNotError('geekquote') self.assertNotError('geekquote')
self.assertNotError('geekquote multiline') self.assertNotError('geekquote --id=4848')
self.assertError('geekquote --id=48a')
def testAcronym(self): def testAcronym(self):
self.assertNotError('acronym PERL') self.assertNotError('acronym PERL')