Added bold and maximum-results configurable.

This commit is contained in:
Jeremy Fincher 2004-01-01 19:09:02 +00:00
parent 83995296b7
commit 2f55345012
2 changed files with 54 additions and 41 deletions

View File

@ -1,4 +1,4 @@
#re!/usr/bin/env python
#!/usr/bin/env python
###
# Copyright (c) 2002, Jeremiah Fincher
@ -142,7 +142,12 @@ class Google(callbacks.PrivmsgCommandAndRegexp, configurable.Mixin):
('search-snarfer', configurable.BoolType, False,
"""Determines whether the search snarfer is enabled. If so, messages
(even unaddressed ones) beginning with the word 'google' will result
in the first URL Google returns being sent to the channel.""")]
in the first URL Google returns being sent to the channel."""),
('bold', configurable.BoolType, True,
"""Determines whether results are bolded."""),
('maximum-results', configurable.PositiveIntType, 10,
"""Determines the maximum number of results returned from the
google command."""),]
)
def __init__(self):
configurable.Mixin.__init__(self)
@ -155,16 +160,20 @@ class Google(callbacks.PrivmsgCommandAndRegexp, configurable.Mixin):
configurable.Mixin.die(self)
callbacks.PrivmsgCommandAndRegexp.die(self)
def formatData(self, data):
def formatData(self, data, bold=True, max=0):
if isinstance(data, basestring):
return data
time = 'Search took %s seconds' % data.meta.searchTime
results = []
if max:
data.results = data.results[:max]
for result in data.results:
title = utils.htmlToText(result.title.encode('utf-8'))
url = result.URL
if title:
results.append('%s: <%s>' % (ircutils.bold(title), url))
if bold:
title = ircutils.bold(title)
results.append('%s: <%s>' % (title, url))
else:
results.append(url)
if not results:
@ -207,7 +216,9 @@ class Google(callbacks.PrivmsgCommandAndRegexp, configurable.Mixin):
kwargs[option[2:]] = argument
searchString = privmsgs.getArgs(rest)
data = search(self.log, searchString, **kwargs)
irc.reply(msg, self.formatData(data))
bold = self.configurables.get('bold', msg.args[0])
max = self.configurables.get('maximum-results', msg.args[0])
irc.reply(msg, self.formatData(data, bold=bold, max=max))
def metagoogle(self, irc, msg, args):
"""<search> [--(language,restrict)=<value>] [--{similar,notsafe}]

View File

@ -33,44 +33,46 @@ from testsupport import *
class GoogleTestCase(ChannelPluginTestCase, PluginDocumentation):
plugins = ('Google',)
def testNoNoLicenseKeyError(self):
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'google blah'))
self.assertNoResponse(' ')
if network:
def testNoNoLicenseKeyError(self):
self.assertNotError('google config groups-snarfer on')
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'google blah'))
self.assertNoResponse(' ')
def testGroupsSnarfer(self):
self.assertNotError('google config groups-snarfer on')
self.assertRegexp('http://groups.google.com/groups?dq=&hl=en&'
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
'0310132012.738e22fc%40posting.google.com',
r'comp\.lang\.python.*question: usage of __slots__')
self.assertRegexp('http://groups.google.com/groups?selm=ExDm.'
'8bj.23%40gated-at.bofh.it&oe=UTF-8&output=gplain',
r'linux\.kernel.*NFS client freezes')
self.assertRegexp('http://groups.google.com/groups?'
'q=kernel+hot-pants&hl=en&lr=&ie=UTF-8&oe=UTF-8&'
'selm=1.5.4.32.19970313170853.00674d60%40'
'adan.kingston.net&rnum=1',
r'Madrid Bluegrass Ramble')
self.assertRegexp('http://groups.google.com/groups?'
'selm=1.5.4.32.19970313170853.00674d60%40adan.'
'kingston.net&oe=UTF-8&output=gplain',
r'Madrid Bluegrass Ramble')
self.assertRegexp('http://groups.google.com/groups?'
'dq=&hl=en&lr=&ie=UTF-8&threadm=mailman.1010.'
'1069645289.702.python-list%40python.org'
'&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8'
'%26group%3Dcomp.lang.python',
r'comp\.lang\.python.*What exactly are bound')
def testConfig(self):
self.assertNotError('google config groups-snarfer off')
self.assertNoResponse('http://groups.google.com/groups?dq=&hl=en&'
def testGroupsSnarfer(self):
self.assertNotError('google config groups-snarfer on')
self.assertRegexp('http://groups.google.com/groups?dq=&hl=en&'
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
'0310132012.738e22fc%40posting.google.com')
self.assertNotError('google config groups-snarfer on')
self.assertNotError('http://groups.google.com/groups?dq=&hl=en&'
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
'0310132012.738e22fc%40posting.google.com')
'0310132012.738e22fc%40posting.google.com',
r'comp\.lang\.python.*question: usage of __slots__')
self.assertRegexp('http://groups.google.com/groups?selm=ExDm.'
'8bj.23%40gated-at.bofh.it&oe=UTF-8&output=gplain',
r'linux\.kernel.*NFS client freezes')
self.assertRegexp('http://groups.google.com/groups?'
'q=kernel+hot-pants&hl=en&lr=&ie=UTF-8&oe=UTF-8&'
'selm=1.5.4.32.19970313170853.00674d60%40'
'adan.kingston.net&rnum=1',
r'Madrid Bluegrass Ramble')
self.assertRegexp('http://groups.google.com/groups?'
'selm=1.5.4.32.19970313170853.00674d60%40adan.'
'kingston.net&oe=UTF-8&output=gplain',
r'Madrid Bluegrass Ramble')
self.assertRegexp('http://groups.google.com/groups?'
'dq=&hl=en&lr=&ie=UTF-8&threadm=mailman.1010.'
'1069645289.702.python-list%40python.org'
'&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8'
'%26group%3Dcomp.lang.python',
r'comp\.lang\.python.*What exactly are bound')
def testConfig(self):
self.assertNotError('google config groups-snarfer off')
self.assertNoResponse('http://groups.google.com/groups?dq=&hl=en&'
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
'0310132012.738e22fc%40posting.google.com')
self.assertNotError('google config groups-snarfer on')
self.assertNotError('http://groups.google.com/groups?dq=&hl=en&'
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
'0310132012.738e22fc%40posting.google.com')
def testInvalidKeyCaught(self):
self.assertNotError(