Fixed googleinfo updating.

This commit is contained in:
Jeremy Fincher 2003-07-30 20:07:25 +00:00
parent 1230769fe0
commit a6f8ddab18

View File

@ -70,6 +70,21 @@ def configure(onStart, afterConnect, advanced):
print 'You\'ll need to get a key before you can use this plugin.' print 'You\'ll need to get a key before you can use this plugin.'
print 'You can apply for a key from http://www.google.com/apis/' print 'You can apply for a key from http://www.google.com/apis/'
totalSearches = 0
totalTime = 0
last24hours = structures.queue()
def search(*args, **kwargs):
global totalSearches, totalTime, last24hours
data = google.doGoogleSearch(*args, **kwargs)
now = time.time()
totalSearches += 1
totalTime += data.meta.searchTime
last24hours.enqueue(now)
while last24hours and now - last24hours.peek() > 86400:
last24hours.dequeue()
return data
class GooglePrivmsg(callbacks.Privmsg): class GooglePrivmsg(callbacks.Privmsg):
threaded = True threaded = True
def __init__(self): def __init__(self):
@ -78,16 +93,7 @@ class GooglePrivmsg(callbacks.Privmsg):
self.totalTime = 0 self.totalTime = 0
self.last24hours = structures.queue() self.last24hours = structures.queue()
def _searched(self, data):
now = time.time()
self.total += 1
self.totalTime += data.meta.searchTime
self.last24hours.enqueue(now)
while self.last24hours and now - self.last24hours.peek() > 86400:
self.last24hours.dequeue()
def formatData(self, data): def formatData(self, data):
self._searched(data)
time = '(search took %s seconds)' % data.meta.searchTime time = '(search took %s seconds)' % data.meta.searchTime
results = [] results = []
for result in data.results: for result in data.results:
@ -112,6 +118,7 @@ class GooglePrivmsg(callbacks.Privmsg):
key = privmsgs.getArgs(args) key = privmsgs.getArgs(args)
google.setLicense(key) google.setLicense(key)
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
googlelicensekey = privmsgs.checkCapability(googlelicensekey, 'admin') googlelicensekey = privmsgs.checkCapability(googlelicensekey, 'admin')
def google(self, irc, msg, args): def google(self, irc, msg, args):
@ -127,7 +134,7 @@ class GooglePrivmsg(callbacks.Privmsg):
for (option, argument) in optlist: for (option, argument) in optlist:
kwargs[option[2:]] = argument kwargs[option[2:]] = argument
searchString = privmsgs.getArgs(rest) searchString = privmsgs.getArgs(rest)
data = google.doGoogleSearch(searchString, **kwargs) data = search(searchString, **kwargs)
irc.reply(msg, self.formatData(data)) irc.reply(msg, self.formatData(data))
def metagoogle(self, irc, msg, args): def metagoogle(self, irc, msg, args):
@ -142,7 +149,7 @@ class GooglePrivmsg(callbacks.Privmsg):
for option, argument in optlist: for option, argument in optlist:
kwargs[option[2:]] = argument kwargs[option[2:]] = argument
searchString = privmsgs.getArgs(rest) searchString = privmsgs.getArgs(rest)
data = google.doGoogleSearch(searchString, **kwargs) data = search(searchString, **kwargs)
meta = data.meta meta = data.meta
categories = [d['fullViewableName'] for d in meta.directoryCategories] categories = [d['fullViewableName'] for d in meta.directoryCategories]
categories = [repr(s.replace('_', ' ')) for s in categories] categories = [repr(s.replace('_', ' ')) for s in categories]
@ -170,7 +177,7 @@ class GooglePrivmsg(callbacks.Privmsg):
results = [] results = []
for arg in args: for arg in args:
data = google.doGoogleSearch(arg) data = search(arg)
results.append((data.meta.estimatedTotalResultsCount, arg)) results.append((data.meta.estimatedTotalResultsCount, arg))
results.sort() results.sort()
results.reverse() results.reverse()
@ -184,9 +191,7 @@ class GooglePrivmsg(callbacks.Privmsg):
""" """
(site, s) = privmsgs.getArgs(args, needed=2) (site, s) = privmsgs.getArgs(args, needed=2)
searchString = 'site:%s %s' % (site, s) searchString = 'site:%s %s' % (site, s)
data = google.doGoogleSearch(searchString, data = search(searchString, language='lang_en', safeSearch=1)
language='lang_en',
safeSearch=1)
irc.reply(msg, self.formatData(data)) irc.reply(msg, self.formatData(data))
def googlespell(self, irc, msg, args): def googlespell(self, irc, msg, args):
@ -204,13 +209,13 @@ class GooglePrivmsg(callbacks.Privmsg):
Returns interesting information about this Google module. Mostly Returns interesting information about this Google module. Mostly
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.
""" """
last24hours = len(self.last24hours) recent = len(last24hours)
irc.reply(msg, 'This google module has been called %s time%stotal; '\ irc.reply(msg, 'This google module has been called %s time%stotal; '\
'%s time%sin the past 24 hours. ' \ '%s time%sin the past 24 hours. ' \
'Google has spent %s seconds searching for me.' % \ 'Google has spent %s seconds searching for me.' % \
(self.total, self.total != 1 and 's ' or ' ', (totalSearches, totalSearches != 1 and 's ' or ' ',
last24hours, last24hours != 1 and 's ' or ' ', recent, recent != 1 and 's ' or ' ',
self.totalTime)) totalTime))
class GooglePrivmsgRegexp(callbacks.PrivmsgRegexp): class GooglePrivmsgRegexp(callbacks.PrivmsgRegexp):
@ -218,7 +223,7 @@ class GooglePrivmsgRegexp(callbacks.PrivmsgRegexp):
def googleSnarfer(self, irc, msg, match): def googleSnarfer(self, irc, msg, match):
r"^google\s+(.*)$" r"^google\s+(.*)$"
searchString = match.group(1) searchString = match.group(1)
data = google.doGoogleSearch(searchString, safeSearch=1) data = search(searchString, safeSearch=1)
if data.results: if data.results:
url = data.results[0].URL url = data.results[0].URL
irc.queueMsg(ircmsgs.privmsg(ircutils.replyTo(msg), url)) irc.queueMsg(ircmsgs.privmsg(ircutils.replyTo(msg), url))