This commit is contained in:
Jeremy Fincher 2004-09-06 08:42:00 +00:00
parent 887e92a12b
commit 2db0cd197c

View File

@ -92,6 +92,8 @@ def configure(advanced):
def search(log, queries, **kwargs): def search(log, queries, **kwargs):
# We have to keep stats here, rather than in formatData or elsewhere,
# because not all searching functions use formatData -- fight, lucky, etc.
assert not isinstance(queries, basestring), 'Old code: queries is a list.' assert not isinstance(queries, basestring), 'Old code: queries is a list.'
try: try:
for (i, query) in enumerate(queries): for (i, query) in enumerate(queries):
@ -101,6 +103,10 @@ def search(log, queries, **kwargs):
if proxy: if proxy:
kwargs['http_proxy'] = proxy kwargs['http_proxy'] = proxy
data = google.doGoogleSearch(' '.join(queries), **kwargs) data = google.doGoogleSearch(' '.join(queries), **kwargs)
searches = conf.supybot.plugins.Google.state.searches() + 1
conf.supybot.plugins.Google.state.searches.setValue(searches)
time = conf.supybot.plugins.Google.state.time() + data.meta.searchTime
conf.supybot.plugins.Google.state.time.setValue(time)
return data return data
except socket.error, e: except socket.error, e:
if e.args[0] == 110: if e.args[0] == 110:
@ -179,29 +185,21 @@ conf.registerGlobalValue(conf.supybot.plugins.Google.state, 'time',
registry.Float(0.0, """Used to keep the total amount of time Google has registry.Float(0.0, """Used to keep the total amount of time Google has
spent searching for this bot. You shouldn't modify this.""")) spent searching for this bot. You shouldn't modify this."""))
searches = conf.supybot.plugins.Google.state.searches()
totalTime = conf.supybot.plugins.Google.state.time()
class Google(callbacks.PrivmsgCommandAndRegexp): class Google(callbacks.PrivmsgCommandAndRegexp):
threaded = True threaded = True
regexps = sets.Set(['googleSnarfer', 'googleGroups']) regexps = sets.Set(['googleSnarfer', 'googleGroups'])
def __init__(self): def __init__(self):
callbacks.PrivmsgCommandAndRegexp.__init__(self) callbacks.PrivmsgCommandAndRegexp.__init__(self)
self.total = self.registryValue('state.searches') self.last24hours = structures.TimeoutQueue(86400)
self.totalTime = self.registryValue('state.time')
self.last24hours = structures.queue()
google.setLicense(self.registryValue('licenseKey')) google.setLicense(self.registryValue('licenseKey'))
def die(self):
self.setRegistryValue('state.searches', self.total)
self.setRegistryValue('state.time', self.totalTime)
def formatData(self, data, bold=True, max=0): def formatData(self, data, bold=True, max=0):
if isinstance(data, basestring): if isinstance(data, basestring):
return data return data
self.total += 1 self.last24hours.enqueue(None)
self.totalTime += data.meta.searchTime
now = time.time()
while self.last24hours and now - self.last24hours.peek() > 86400:
self.last24hours.dequeue()
self.last24hours.enqueue(now)
t = 'Search took %s seconds' % data.meta.searchTime t = 'Search took %s seconds' % data.meta.searchTime
results = [] results = []
if max: if max:
@ -360,11 +358,12 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
useful for making sure you don't go over your 1000 requests/day limit. useful for making sure you don't go over your 1000 requests/day limit.
""" """
recent = len(self.last24hours) recent = len(self.last24hours)
irc.reply('This google module has been called %s total; ' time = self.registryValue('state.time')
searches = self.registryValue('state.searches')
irc.reply('This google module has made %s total; '
'%s in the past 24 hours. ' '%s in the past 24 hours. '
'Google has spent %s seconds searching for me.' % 'Google has spent %s seconds searching for me.' %
(utils.nItems('time', self.total), (utils.nItems('search', searches), recent, time))
utils.nItems('time', recent), self.totalTime))
def googleSnarfer(self, irc, msg, match): def googleSnarfer(self, irc, msg, match):
r"^google\s+(.*)$" r"^google\s+(.*)$"