2003-04-08 21:16:18 +02:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-04-08 21:16:18 +02:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
2004-08-19 01:15:27 +02:00
|
|
|
Accesses Google for various things.
|
2003-04-08 21:16:18 +02:00
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-04-08 21:16:18 +02:00
|
|
|
|
2003-04-20 00:16:57 +02:00
|
|
|
import re
|
2004-09-09 16:04:44 +02:00
|
|
|
import cgi
|
2003-08-11 05:37:15 +02:00
|
|
|
import sets
|
2003-04-08 21:16:18 +02:00
|
|
|
import time
|
2003-04-17 12:05:22 +02:00
|
|
|
import getopt
|
2003-10-22 18:05:18 +02:00
|
|
|
import socket
|
2004-09-09 16:04:44 +02:00
|
|
|
import urllib
|
2003-12-16 22:20:27 +01:00
|
|
|
import xml.sax
|
2003-04-08 21:16:18 +02:00
|
|
|
|
2004-09-30 16:53:22 +02:00
|
|
|
import SOAPpy
|
2003-04-08 21:16:18 +02:00
|
|
|
import google
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.registry as registry
|
2004-01-20 00:42:50 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
2004-10-22 22:53:35 +02:00
|
|
|
import supybot.world as world
|
2004-10-16 05:39:42 +02:00
|
|
|
from supybot.commands import *
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.ircutils as ircutils
|
2004-08-05 20:29:26 +02:00
|
|
|
import supybot.webutils as webutils
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.callbacks as callbacks
|
|
|
|
import supybot.structures as structures
|
2003-04-08 21:16:18 +02:00
|
|
|
|
2004-01-30 00:58:27 +01:00
|
|
|
def configure(advanced):
|
2004-07-25 20:24:51 +02:00
|
|
|
from supybot.questions import output, expect, anything, something, yn
|
2004-01-31 23:24:43 +01:00
|
|
|
output('To use Google\'t Web Services, you must have a license key.')
|
|
|
|
if yn('Do you have a license key?'):
|
2003-09-03 11:45:11 +02:00
|
|
|
key = something('What is it?')
|
|
|
|
while len(key) != 32:
|
2004-01-31 23:24:43 +01:00
|
|
|
output('That\'s not a valid Google license key.')
|
|
|
|
if yn('Are you sure you have a valid Google license key?'):
|
2003-09-03 11:45:11 +02:00
|
|
|
key = something('What is it?')
|
|
|
|
else:
|
|
|
|
key = ''
|
|
|
|
break
|
|
|
|
if key:
|
2004-01-28 23:19:25 +01:00
|
|
|
conf.registerPlugin('Google', True)
|
2004-01-20 05:32:23 +01:00
|
|
|
conf.supybot.plugins.Google.licenseKey.setValue(key)
|
2004-01-31 23:24:43 +01:00
|
|
|
output("""The Google plugin has the functionality to watch for URLs
|
|
|
|
that match a specific pattern. (We call this a snarfer)
|
|
|
|
When supybot sees such a URL, it will parse the web page
|
|
|
|
for information and reply with the results.
|
|
|
|
|
|
|
|
Google has two available snarfers: Google Groups link
|
|
|
|
snarfing and a google search snarfer.""")
|
2003-11-17 19:09:42 +01:00
|
|
|
if yn('Do you want the Google Groups link snarfer enabled by '
|
2004-01-31 23:24:43 +01:00
|
|
|
'default?'):
|
2004-01-20 05:32:23 +01:00
|
|
|
conf.supybot.plugins.Google.groupsSnarfer.setValue(True)
|
2004-01-31 23:24:43 +01:00
|
|
|
if yn('Do you want the Google search snarfer enabled by default?'):
|
2004-01-20 05:32:23 +01:00
|
|
|
conf.supybot.plugins.Google.searchSnarfer.setValue(True)
|
2003-04-08 21:16:18 +02:00
|
|
|
else:
|
2004-01-31 23:24:43 +01:00
|
|
|
output("""You'll need to get a key before you can use this plugin.
|
|
|
|
You can apply for a key at http://www.google.com/apis/""")
|
2003-07-30 22:07:25 +02:00
|
|
|
|
2003-08-27 20:06:26 +02:00
|
|
|
|
2004-02-12 01:23:24 +01:00
|
|
|
def search(log, queries, **kwargs):
|
2004-09-06 10:42:00 +02:00
|
|
|
# We have to keep stats here, rather than in formatData or elsewhere,
|
|
|
|
# because not all searching functions use formatData -- fight, lucky, etc.
|
2004-02-12 01:23:24 +01:00
|
|
|
assert not isinstance(queries, basestring), 'Old code: queries is a list.'
|
2003-10-22 17:12:33 +02:00
|
|
|
try:
|
2004-02-12 01:23:24 +01:00
|
|
|
for (i, query) in enumerate(queries):
|
|
|
|
if len(query.split(None, 1)) > 1:
|
|
|
|
queries[i] = repr(query)
|
2004-08-03 06:29:25 +02:00
|
|
|
proxy = conf.supybot.protocols.http.proxy()
|
|
|
|
if proxy:
|
|
|
|
kwargs['http_proxy'] = proxy
|
2004-09-20 01:22:10 +02:00
|
|
|
query = ' '.join(queries).decode('utf-8')
|
|
|
|
data = google.doGoogleSearch(query, **kwargs)
|
2004-09-06 10:42:00 +02:00
|
|
|
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)
|
2004-09-16 14:11:54 +02:00
|
|
|
last24hours.enqueue(None)
|
2003-10-22 17:12:33 +02:00
|
|
|
return data
|
|
|
|
except socket.error, e:
|
|
|
|
if e.args[0] == 110:
|
2004-04-05 11:52:23 +02:00
|
|
|
raise callbacks.Error, 'Connection timed out to Google.com.'
|
2003-10-22 17:12:33 +02:00
|
|
|
else:
|
2004-04-05 11:52:23 +02:00
|
|
|
raise callbacks.Error, 'Error connecting to Google.com.'
|
2004-09-30 16:53:22 +02:00
|
|
|
except SOAPpy.HTTPError, e:
|
2004-08-31 21:16:20 +02:00
|
|
|
log.info('HTTP Error accessing Google: %s', e)
|
2004-04-05 11:52:23 +02:00
|
|
|
raise callbacks.Error, 'Error connecting to Google.com.'
|
2004-09-30 16:53:22 +02:00
|
|
|
except SOAPpy.faultType, e:
|
2004-11-30 05:36:54 +01:00
|
|
|
if 'Invalid authorization key' in e.faultstring:
|
|
|
|
raise callbacks.Error, 'Invalid Google license key.'
|
|
|
|
elif 'Problem looking up user record' in e.faultstring:
|
|
|
|
raise callbacks.Error, \
|
|
|
|
'Google seems to be having trouble looking up the user for '\
|
|
|
|
'your license key. This probably isn\'t a problem on your '\
|
|
|
|
'side; it\'s probably a bug on Google\'s side. It seems '\
|
|
|
|
'to happen intermittently.'
|
|
|
|
else:
|
2004-09-30 16:53:22 +02:00
|
|
|
log.exception('Unexpected SOAPpy error:')
|
2004-11-30 05:36:54 +01:00
|
|
|
raise callbacks.Error, \
|
|
|
|
'Unexpected error from Google; please report this to the ' \
|
|
|
|
'Supybot developers.'
|
2003-12-16 22:01:59 +01:00
|
|
|
except xml.sax.SAXException, e:
|
|
|
|
log.exception('Uncaught SAX error:')
|
2004-08-19 01:15:27 +02:00
|
|
|
raise callbacks.Error, 'Google returned an unparsable response. ' \
|
2003-12-16 22:01:59 +01:00
|
|
|
'The full traceback has been logged.'
|
2004-09-30 16:53:22 +02:00
|
|
|
except SOAPpy.Error, e:
|
2004-04-05 11:52:23 +02:00
|
|
|
log.exception('Uncaught SOAP exception in Google.search:')
|
|
|
|
raise callbacks.Error, 'Error connecting to Google.com.'
|
2003-08-20 10:54:29 +02:00
|
|
|
|
2004-01-20 05:32:23 +01:00
|
|
|
class LicenseKey(registry.String):
|
2004-02-21 23:06:31 +01:00
|
|
|
def setValue(self, s):
|
|
|
|
if s and len(s) != 32:
|
2004-01-20 05:32:23 +01:00
|
|
|
raise registry.InvalidRegistryValue, 'Invalid Google license key.'
|
2004-10-01 00:00:00 +02:00
|
|
|
try:
|
2004-10-10 06:19:03 +02:00
|
|
|
s = s or ''
|
|
|
|
registry.String.setValue(self, s)
|
2004-10-01 00:00:00 +02:00
|
|
|
if s:
|
|
|
|
google.setLicense(self.value)
|
|
|
|
except AttributeError:
|
2004-10-23 18:02:49 +02:00
|
|
|
if world and not world.dying: # At shutdown world can be None.
|
2004-10-22 22:53:35 +02:00
|
|
|
raise callbacks.Error, \
|
|
|
|
'It appears that the initial import of ' \
|
|
|
|
'our underlying google.py module has ' \
|
|
|
|
'failed. Once the cause of that problem ' \
|
|
|
|
'has been diagnosed and fixed, the bot ' \
|
|
|
|
'will need to be restarted in order to ' \
|
|
|
|
'load this plugin.'
|
2004-06-07 14:20:16 +02:00
|
|
|
|
|
|
|
class Language(registry.OnlySomeStrings):
|
|
|
|
validStrings = ['lang_' + s for s in 'ar zh-CN zh-TW cs da nl en et fi fr '
|
|
|
|
'de el iw hu is it ja ko lv lt no pt '
|
|
|
|
'pl ro ru es sv tr'.split()]
|
|
|
|
validStrings.append('')
|
|
|
|
def normalize(self, s):
|
|
|
|
if not s.startswith('lang_'):
|
|
|
|
s = 'lang_' + s
|
|
|
|
if not s.endswith('CN') or s.endswith('TW'):
|
|
|
|
s = s.lower()
|
|
|
|
else:
|
|
|
|
s = s.lower()[:-2] + s[-2:]
|
|
|
|
return s
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-01-20 00:42:50 +01:00
|
|
|
conf.registerPlugin('Google')
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'groupsSnarfer',
|
|
|
|
registry.Boolean(False, """Determines whether the groups snarfer is
|
|
|
|
enabled. If so, URLs at groups.google.com will be snarfed and their
|
|
|
|
group/title messaged to the channel."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'searchSnarfer',
|
|
|
|
registry.Boolean(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."""))
|
2004-09-14 22:56:47 +02:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'colorfulFilter',
|
|
|
|
registry.Boolean(False, """Determines whether the word 'google' in the
|
|
|
|
bot's output will be made colorful (like Google's logo)."""))
|
2004-01-20 00:42:50 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'bold',
|
|
|
|
registry.Boolean(True, """Determines whether results are bolded."""))
|
2004-01-20 05:32:23 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'maximumResults',
|
2004-01-20 00:42:50 +01:00
|
|
|
registry.PositiveInteger(10, """Determines the maximum number of results
|
|
|
|
returned from the google command."""))
|
2004-06-07 14:20:16 +02:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'defaultLanguage',
|
|
|
|
Language('lang_en', """Determines what default language is used in
|
|
|
|
searches. If left empty, no specific language will be requested."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'safeSearch',
|
|
|
|
registry.Boolean(True, "Determines whether safeSearch is on by default."))
|
2004-01-20 00:42:50 +01:00
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.Google, 'licenseKey',
|
2004-01-20 05:32:23 +01:00
|
|
|
LicenseKey('', """Sets the Google license key for using Google's Web
|
2004-01-20 00:42:50 +01:00
|
|
|
Services API. This is necessary before you can do any searching with this
|
2004-02-14 08:17:59 +01:00
|
|
|
module.""", private=True))
|
2004-01-20 05:32:23 +01:00
|
|
|
|
2004-08-31 07:53:05 +02:00
|
|
|
conf.registerGroup(conf.supybot.plugins.Google, 'state')
|
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.Google.state, 'searches',
|
2004-08-31 16:45:01 +02:00
|
|
|
registry.Integer(0, """Used to keep the total number of searches Google has
|
2004-08-31 07:53:05 +02:00
|
|
|
done for this bot. You shouldn't modify this."""))
|
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.Google.state, 'time',
|
|
|
|
registry.Float(0.0, """Used to keep the total amount of time Google has
|
|
|
|
spent searching for this bot. You shouldn't modify this."""))
|
|
|
|
|
2004-09-16 14:11:54 +02:00
|
|
|
last24hours = structures.TimeoutQueue(86400)
|
2004-09-06 10:42:00 +02:00
|
|
|
totalTime = conf.supybot.plugins.Google.state.time()
|
2004-09-16 14:11:54 +02:00
|
|
|
searches = conf.supybot.plugins.Google.state.searches()
|
2004-09-06 10:42:00 +02:00
|
|
|
|
2004-01-20 00:42:50 +01:00
|
|
|
class Google(callbacks.PrivmsgCommandAndRegexp):
|
2003-04-08 21:16:18 +02:00
|
|
|
threaded = True
|
2004-09-10 08:36:27 +02:00
|
|
|
callBefore = ['URL']
|
|
|
|
regexps = ['googleSnarfer', 'googleGroups']
|
2003-04-08 21:16:18 +02:00
|
|
|
def __init__(self):
|
2004-09-20 21:12:32 +02:00
|
|
|
self.__parent = super(Google, self)
|
|
|
|
self.__parent.__init__()
|
2004-08-31 16:45:01 +02:00
|
|
|
google.setLicense(self.registryValue('licenseKey'))
|
|
|
|
|
2004-09-20 21:12:32 +02:00
|
|
|
def callCommand(self, name, irc, msg, *L, **kwargs):
|
|
|
|
try:
|
|
|
|
self.__parent.callCommand(name, irc, msg, *L, **kwargs)
|
|
|
|
except xml.sax.SAXReaderNotAvailable, e:
|
|
|
|
irc.error('No XML parser available.')
|
|
|
|
|
2004-09-14 22:56:47 +02:00
|
|
|
_colorGoogles = {}
|
|
|
|
def _getColorGoogle(self, m):
|
|
|
|
s = m.group(1)
|
|
|
|
ret = self._colorGoogles.get(s)
|
|
|
|
if not ret:
|
|
|
|
L = list(s)
|
2004-10-03 12:00:46 +02:00
|
|
|
L[0] = ircutils.mircColor(L[0], 'blue')[:-1]
|
|
|
|
L[1] = ircutils.mircColor(L[1], 'red')[:-1]
|
|
|
|
L[2] = ircutils.mircColor(L[2], 'yellow')[:-1]
|
|
|
|
L[3] = ircutils.mircColor(L[3], 'blue')[:-1]
|
|
|
|
L[4] = ircutils.mircColor(L[4], 'green')[:-1]
|
2004-09-14 22:56:47 +02:00
|
|
|
L[5] = ircutils.mircColor(L[5], 'red')
|
|
|
|
ret = ''.join(L)
|
|
|
|
self._colorGoogles[s] = ret
|
|
|
|
return ircutils.bold(ret)
|
|
|
|
|
2004-09-16 14:13:59 +02:00
|
|
|
_googleRe = re.compile(r'\b(google)\b', re.I)
|
2004-09-14 22:56:47 +02:00
|
|
|
def outFilter(self, irc, msg):
|
|
|
|
if msg.command == 'PRIVMSG' and \
|
|
|
|
self.registryValue('colorfulFilter', msg.args[0]):
|
|
|
|
s = msg.args[1]
|
|
|
|
s = re.sub(self._googleRe, self._getColorGoogle, s)
|
2004-09-21 23:18:37 +02:00
|
|
|
msg = ircmsgs.privmsg(msg.args[0], s, msg=msg)
|
2004-09-14 22:56:47 +02:00
|
|
|
return msg
|
|
|
|
|
2004-01-01 20:09:02 +01:00
|
|
|
def formatData(self, data, bold=True, max=0):
|
2003-10-22 17:12:33 +02:00
|
|
|
if isinstance(data, basestring):
|
|
|
|
return data
|
2004-08-31 16:45:01 +02:00
|
|
|
t = 'Search took %s seconds' % data.meta.searchTime
|
2003-04-16 09:07:39 +02:00
|
|
|
results = []
|
2004-01-01 20:09:02 +01:00
|
|
|
if max:
|
|
|
|
data.results = data.results[:max]
|
2003-04-16 09:07:39 +02:00
|
|
|
for result in data.results:
|
|
|
|
title = utils.htmlToText(result.title.encode('utf-8'))
|
|
|
|
url = result.URL
|
|
|
|
if title:
|
2004-01-01 20:09:02 +01:00
|
|
|
if bold:
|
|
|
|
title = ircutils.bold(title)
|
|
|
|
results.append('%s: <%s>' % (title, url))
|
2003-04-16 09:07:39 +02:00
|
|
|
else:
|
|
|
|
results.append(url)
|
2003-04-08 21:16:18 +02:00
|
|
|
if not results:
|
2004-08-31 16:45:01 +02:00
|
|
|
return 'No matches found (%s)' % t
|
2003-04-08 21:16:18 +02:00
|
|
|
else:
|
2004-08-31 16:45:01 +02:00
|
|
|
return '%s: %s' % (t, '; '.join(results))
|
2003-04-08 21:16:18 +02:00
|
|
|
|
2004-10-16 05:39:42 +02:00
|
|
|
def lucky(self, irc, msg, args, text):
|
2004-07-02 15:53:33 +02:00
|
|
|
"""<search>
|
|
|
|
|
|
|
|
Does a google search, but only returns the first result.
|
|
|
|
"""
|
2004-10-16 05:39:42 +02:00
|
|
|
data = search(self.log, text)
|
2004-07-02 15:53:33 +02:00
|
|
|
if data.results:
|
|
|
|
url = data.results[0].URL
|
|
|
|
irc.reply(url)
|
|
|
|
else:
|
|
|
|
irc.reply('Google found nothing.')
|
2004-11-30 05:36:54 +01:00
|
|
|
lucky = wrap(lucky, [many('something')])
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-10-16 05:39:42 +02:00
|
|
|
def google(self, irc, msg, args, optlist, text):
|
2003-09-11 00:29:34 +02:00
|
|
|
"""<search> [--{language,restrict}=<value>] [--{notsafe,similar}]
|
2003-04-16 09:07:39 +02:00
|
|
|
|
|
|
|
Searches google.com for the given string. As many results as can fit
|
2003-09-09 10:23:36 +02:00
|
|
|
are included. --language accepts a language abbreviation; --restrict
|
|
|
|
restricts the results to certain classes of things; --similar tells
|
2003-09-11 00:29:34 +02:00
|
|
|
Google not to filter similar results. --notsafe allows possibly
|
|
|
|
work-unsafe results.
|
2003-04-08 21:16:18 +02:00
|
|
|
"""
|
2004-06-07 14:20:16 +02:00
|
|
|
kwargs = {}
|
|
|
|
if self.registryValue('safeSearch', channel=msg.args[0]):
|
|
|
|
kwargs['safeSearch'] = 1
|
|
|
|
lang = self.registryValue('defaultLanguage', channel=msg.args[0])
|
|
|
|
if lang:
|
|
|
|
kwargs['language'] = lang
|
2003-04-17 12:05:22 +02:00
|
|
|
for (option, argument) in optlist:
|
2004-10-16 05:39:42 +02:00
|
|
|
if option == 'notsafe':
|
2003-09-11 00:29:34 +02:00
|
|
|
kwargs['safeSearch'] = False
|
2004-10-16 05:39:42 +02:00
|
|
|
elif option == 'similar':
|
2003-09-09 10:23:36 +02:00
|
|
|
kwargs['filter'] = False
|
|
|
|
else:
|
2004-11-17 23:13:52 +01:00
|
|
|
kwargs[option] = argument
|
2004-01-20 00:42:50 +01:00
|
|
|
try:
|
2004-10-16 05:39:42 +02:00
|
|
|
data = search(self.log, text, **kwargs)
|
2004-01-20 00:42:50 +01:00
|
|
|
except google.NoLicenseKey, e:
|
2004-02-12 01:23:24 +01:00
|
|
|
irc.error('You must have a free Google web services license key '
|
|
|
|
'in order to use this command. You can get one at '
|
|
|
|
'<http://google.com/apis/>. Once you have one, you can '
|
|
|
|
'set it with the command '
|
|
|
|
'"config supybot.plugins.Google.licenseKey <key>".')
|
2004-01-20 00:42:50 +01:00
|
|
|
return
|
2004-01-21 02:33:59 +01:00
|
|
|
bold = self.registryValue('bold', msg.args[0])
|
|
|
|
max = self.registryValue('maximumResults', msg.args[0])
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(self.formatData(data, bold=bold, max=max))
|
2004-11-30 14:58:50 +01:00
|
|
|
google = wrap(google, [getopts({'language':'something',
|
|
|
|
'restrict':'something',
|
2004-10-16 05:39:42 +02:00
|
|
|
'notsafe':'', 'similar':''}),
|
2004-11-30 05:36:54 +01:00
|
|
|
many('something')])
|
2003-04-08 21:16:18 +02:00
|
|
|
|
2004-10-16 05:39:42 +02:00
|
|
|
def metagoogle(self, irc, msg, args, optlist, text):
|
2003-09-11 00:29:34 +02:00
|
|
|
"""<search> [--(language,restrict)=<value>] [--{similar,notsafe}]
|
2003-07-30 21:08:05 +02:00
|
|
|
|
|
|
|
Searches google and gives all the interesting meta information about
|
2003-09-11 00:29:34 +02:00
|
|
|
the search. See the help for the google command for a detailed
|
|
|
|
description of the parameters.
|
2003-07-30 21:08:05 +02:00
|
|
|
"""
|
|
|
|
kwargs = {'language': 'lang_en', 'safeSearch': 1}
|
|
|
|
for option, argument in optlist:
|
2004-10-16 05:39:42 +02:00
|
|
|
if option == 'notsafe':
|
2003-09-11 00:29:34 +02:00
|
|
|
kwargs['safeSearch'] = False
|
2004-10-16 05:39:42 +02:00
|
|
|
elif option == 'similar':
|
2003-09-11 00:29:34 +02:00
|
|
|
kwargs['filter'] = False
|
|
|
|
else:
|
|
|
|
kwargs[option[2:]] = argument
|
2004-10-16 05:39:42 +02:00
|
|
|
data = search(self.log, text, **kwargs)
|
2003-07-30 21:08:05 +02:00
|
|
|
meta = data.meta
|
|
|
|
categories = [d['fullViewableName'] for d in meta.directoryCategories]
|
2003-09-11 00:29:34 +02:00
|
|
|
categories = [utils.dqrepr(s.replace('_', ' ')) for s in categories]
|
|
|
|
if categories:
|
|
|
|
categories = utils.commaAndify(categories)
|
2003-07-30 21:08:05 +02:00
|
|
|
else:
|
2003-09-11 00:29:34 +02:00
|
|
|
categories = ''
|
2004-10-24 00:07:50 +02:00
|
|
|
s = 'Search for %s returned %s %s results in %s seconds.%s' % \
|
|
|
|
(utils.quoted(meta.searchQuery),
|
2003-07-30 21:08:05 +02:00
|
|
|
meta.estimateIsExact and 'exactly' or 'approximately',
|
|
|
|
meta.estimatedTotalResultsCount,
|
|
|
|
meta.searchTime,
|
|
|
|
categories and ' Categories include %s.' % categories)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2004-11-30 14:58:50 +01:00
|
|
|
metagoogle = wrap(metagoogle, [getopts({'language':'something',
|
|
|
|
'restrict':'something',
|
2004-10-16 05:39:42 +02:00
|
|
|
'notsafe':'', 'similar':''}),
|
2004-11-30 05:36:54 +01:00
|
|
|
many('something')])
|
2003-07-30 21:08:05 +02:00
|
|
|
|
2004-02-07 13:11:03 +01:00
|
|
|
_cacheUrlRe = re.compile('<code>([^<]+)</code>')
|
2004-10-16 05:39:42 +02:00
|
|
|
def cache(self, irc, msg, args, url):
|
2004-02-07 13:11:03 +01:00
|
|
|
"""<url>
|
|
|
|
|
|
|
|
Returns a link to the cached version of <url> if it is available.
|
|
|
|
"""
|
|
|
|
html = google.doGetCachedPage(url)
|
|
|
|
m = self._cacheUrlRe.search(html)
|
|
|
|
if m is not None:
|
|
|
|
url = m.group(1)
|
|
|
|
url = utils.htmlToText(url)
|
|
|
|
irc.reply(url)
|
|
|
|
else:
|
|
|
|
irc.error('Google seems to have no cache for that site.')
|
2004-10-16 05:39:42 +02:00
|
|
|
cache = wrap(cache, ['url'])
|
2004-02-07 13:11:03 +01:00
|
|
|
|
2003-10-21 06:44:44 +02:00
|
|
|
def fight(self, irc, msg, args):
|
2003-07-30 21:39:58 +02:00
|
|
|
"""<search string> <search string> [<search string> ...]
|
|
|
|
|
|
|
|
Returns the results of each search, in order, from greatest number
|
|
|
|
of results to least.
|
|
|
|
"""
|
|
|
|
|
|
|
|
results = []
|
|
|
|
for arg in args:
|
2004-02-12 01:23:24 +01:00
|
|
|
data = search(self.log, [arg])
|
2003-07-30 21:39:58 +02:00
|
|
|
results.append((data.meta.estimatedTotalResultsCount, arg))
|
|
|
|
results.sort()
|
|
|
|
results.reverse()
|
2004-09-16 13:44:56 +02:00
|
|
|
if self.registryValue('bold', msg.args[0]):
|
2004-09-15 05:50:53 +02:00
|
|
|
format = ircutils.bold
|
|
|
|
else:
|
|
|
|
format = repr
|
|
|
|
s = ', '.join(['%s: %s' % (format(s), i) for (i, s) in results])
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2003-07-30 21:39:58 +02:00
|
|
|
|
2004-10-16 05:39:42 +02:00
|
|
|
def spell(self, irc, msg, args, word):
|
2003-09-06 20:29:44 +02:00
|
|
|
"""<word>
|
|
|
|
|
|
|
|
Returns Google's spelling recommendation for <word>.
|
|
|
|
"""
|
2003-04-08 21:16:18 +02:00
|
|
|
result = google.doSpellingSuggestion(word)
|
|
|
|
if result:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(result)
|
2003-04-08 21:16:18 +02:00
|
|
|
else:
|
2004-04-15 01:33:18 +02:00
|
|
|
irc.reply('No spelling suggestion made. This could mean that '
|
|
|
|
'the word you gave is spelled right; it could also '
|
|
|
|
'mean that its spelling was too whacked out even for '
|
|
|
|
'Google to figure out.')
|
2004-10-16 05:39:42 +02:00
|
|
|
spell = wrap(spell, ['text'])
|
2003-04-08 21:16:18 +02:00
|
|
|
|
2004-08-31 22:22:57 +02:00
|
|
|
def stats(self, irc, msg, args):
|
2003-04-16 09:10:31 +02:00
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns interesting information about this Google module. Mostly
|
|
|
|
useful for making sure you don't go over your 1000 requests/day limit.
|
|
|
|
"""
|
2004-09-20 19:26:32 +02:00
|
|
|
recent = len(last24hours)
|
2004-09-06 10:42:00 +02:00
|
|
|
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. '
|
|
|
|
'Google has spent %s seconds searching for me.' %
|
|
|
|
(utils.nItems('search', searches), recent, time))
|
2004-10-16 05:39:42 +02:00
|
|
|
stats = wrap(stats)
|
2003-04-08 21:16:18 +02:00
|
|
|
|
2003-04-19 23:38:38 +02:00
|
|
|
def googleSnarfer(self, irc, msg, match):
|
|
|
|
r"^google\s+(.*)$"
|
2004-01-21 02:33:59 +01:00
|
|
|
if not self.registryValue('searchSnarfer', msg.args[0]):
|
2003-10-03 00:36:20 +02:00
|
|
|
return
|
2003-06-02 07:53:16 +02:00
|
|
|
searchString = match.group(1)
|
2003-10-22 19:43:04 +02:00
|
|
|
try:
|
2004-02-12 01:23:24 +01:00
|
|
|
data = search(self.log, [searchString], safeSearch=1)
|
2003-10-22 19:43:04 +02:00
|
|
|
except google.NoLicenseKey:
|
|
|
|
return
|
2003-06-02 07:53:16 +02:00
|
|
|
if data.results:
|
|
|
|
url = data.results[0].URL
|
2004-02-19 08:18:33 +01:00
|
|
|
irc.reply(url, prefixName=False)
|
2004-10-22 07:56:55 +02:00
|
|
|
googleSnarfer = urlSnarfer(googleSnarfer)
|
2003-04-19 23:38:38 +02:00
|
|
|
|
2004-12-10 05:53:16 +01:00
|
|
|
_ggThread = re.compile(r'Subject: <b>([^<]+)</b>', re.I)
|
|
|
|
_ggGroup = re.compile(r'<TITLE>Google Groups :\s*([^<]+)</TITLE>', re.I)
|
2004-08-04 04:33:57 +02:00
|
|
|
_ggThreadm = re.compile(r'view the <a href=([^>]+)>no', re.I)
|
|
|
|
_ggSelm = re.compile(r'selm=[^&]+', re.I)
|
2003-08-11 05:37:15 +02:00
|
|
|
def googleGroups(self, irc, msg, match):
|
2004-09-09 16:04:44 +02:00
|
|
|
r"http://groups.google.[\w.]+/\S+\?(\S+)"
|
2004-01-21 02:33:59 +01:00
|
|
|
if not self.registryValue('groupsSnarfer', msg.args[0]):
|
2003-10-21 06:44:44 +02:00
|
|
|
return
|
2004-09-09 16:04:44 +02:00
|
|
|
queries = cgi.parse_qsl(match.group(1))
|
2004-09-10 08:36:27 +02:00
|
|
|
queries = [q for q in queries if q[0] in ('threadm', 'selm')]
|
2004-09-09 16:04:44 +02:00
|
|
|
if not queries:
|
|
|
|
return
|
|
|
|
queries.append(('hl', 'en'))
|
|
|
|
url = 'http://groups.google.com/groups?%s' % urllib.urlencode(queries)
|
2004-08-20 08:52:45 +02:00
|
|
|
text = webutils.getUrl(url)
|
2003-11-24 07:12:09 +01:00
|
|
|
mThread = None
|
|
|
|
mGroup = None
|
2004-08-20 08:52:45 +02:00
|
|
|
if 'threadm=' in url:
|
2004-12-10 05:53:16 +01:00
|
|
|
# Let's just return from here until google decides to stop being
|
|
|
|
# in beta for their groups site
|
|
|
|
return
|
2004-08-04 04:33:57 +02:00
|
|
|
path = self._ggThreadm.search(text)
|
2003-06-02 07:48:57 +02:00
|
|
|
if path is None:
|
|
|
|
return
|
2004-08-04 04:33:57 +02:00
|
|
|
url = 'http://groups.google.com%s' % path.group(1)
|
2004-08-20 08:52:45 +02:00
|
|
|
text = webutils.getUrl(url)
|
2004-08-04 04:33:57 +02:00
|
|
|
mThread = self._ggThread.search(text)
|
|
|
|
mGroup = self._ggGroup.search(text)
|
2003-04-20 00:16:57 +02:00
|
|
|
if mThread and mGroup:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('Google Groups: %s, %s' % (mGroup.group(1),
|
2004-08-04 04:33:57 +02:00
|
|
|
mThread.group(1)), prefixName=False)
|
2003-04-20 00:16:57 +02:00
|
|
|
else:
|
2004-01-09 00:03:48 +01:00
|
|
|
irc.errorPossibleBug('That doesn\'t appear to be a proper '
|
|
|
|
'Google Groups page.')
|
2004-10-22 07:56:55 +02:00
|
|
|
googleGroups = urlSnarfer(googleGroups)
|
2003-04-20 00:16:57 +02:00
|
|
|
|
2004-10-22 22:35:45 +02:00
|
|
|
def _googleUrl(self, s):
|
|
|
|
s = s.replace('+', '%2B')
|
|
|
|
s = s.replace(' ', '+')
|
|
|
|
url = r'http://google.com/search?q=%s' % s
|
|
|
|
return url
|
|
|
|
|
2004-08-20 05:55:27 +02:00
|
|
|
_calcRe = re.compile(r'<td nowrap><font size=\+1><b>(.*?)</b>', re.I)
|
2004-08-20 06:08:00 +02:00
|
|
|
_calcSupRe = re.compile(r'<sup>(.*?)</sup>', re.I)
|
|
|
|
_calcFontRe = re.compile(r'<font size=-2>(.*?)</font>')
|
|
|
|
_calcTimesRe = re.compile(r'×')
|
2004-10-16 05:39:42 +02:00
|
|
|
def calc(self, irc, msg, args, expr):
|
2004-08-20 05:55:27 +02:00
|
|
|
"""<expression>
|
|
|
|
|
|
|
|
Uses Google's calculator to calculate the value of <expression>.
|
|
|
|
"""
|
2004-10-22 22:35:45 +02:00
|
|
|
url = self._googleUrl(expr)
|
2004-08-20 05:55:27 +02:00
|
|
|
html = webutils.getUrl(url)
|
|
|
|
match = self._calcRe.search(html)
|
|
|
|
if match is not None:
|
2004-08-20 06:08:00 +02:00
|
|
|
s = match.group(1)
|
|
|
|
s = self._calcSupRe.sub(r'^(\1)', s)
|
2004-08-20 20:01:36 +02:00
|
|
|
s = self._calcFontRe.sub(r',', s)
|
|
|
|
s = self._calcTimesRe.sub(r'*', s)
|
2004-08-20 06:08:00 +02:00
|
|
|
irc.reply(s)
|
2004-08-20 05:55:27 +02:00
|
|
|
else:
|
|
|
|
irc.reply('Google\'s calculator didn\'t come up with anything.')
|
2004-10-16 05:39:42 +02:00
|
|
|
calc = wrap(calc, ['text'])
|
2004-08-20 05:55:27 +02:00
|
|
|
|
2004-10-22 22:35:45 +02:00
|
|
|
_phoneRe = re.compile(r'Phonebook.*?<font size=-1>(.*?)<a href')
|
|
|
|
def phonebook(self, irc, msg, args, phonenumber):
|
|
|
|
"""<phone number>
|
|
|
|
|
|
|
|
Looks <phone number> up on Google.
|
|
|
|
"""
|
|
|
|
url = self._googleUrl(phonenumber)
|
|
|
|
html = webutils.getUrl(url)
|
|
|
|
m = self._phoneRe.search(html)
|
|
|
|
if m is not None:
|
|
|
|
s = m.group(1)
|
|
|
|
s = s.replace('<b>', '')
|
|
|
|
s = s.replace('</b>', '')
|
|
|
|
s = utils.htmlToText(s)
|
|
|
|
irc.reply(s)
|
|
|
|
else:
|
|
|
|
irc.reply('Google return not phonebook results.')
|
|
|
|
phonebook = wrap(phonebook, ['text'])
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-04-20 00:16:57 +02:00
|
|
|
|
2003-10-19 23:04:35 +02:00
|
|
|
Class = Google
|
2003-10-18 15:25:12 +02:00
|
|
|
|
2003-04-08 21:16:18 +02:00
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|