Google: Use 'json' module and apply .decode() on data.

This commit is contained in:
Valentin Lorentz 2012-08-04 19:50:30 +02:00
parent a89ff32297
commit 978a702346

View File

@ -44,26 +44,7 @@ import supybot.callbacks as callbacks
from supybot.i18n import PluginInternationalization, internationalizeDocstring from supybot.i18n import PluginInternationalization, internationalizeDocstring
_ = PluginInternationalization('Google') _ = PluginInternationalization('Google')
simplejson = None import json
try:
simplejson = utils.python.universalImport('json')
except ImportError:
pass
try:
# The 3rd party simplejson module was included in Python 2.6 and renamed to
# json. Unfortunately, this conflicts with the 3rd party json module.
# Luckily, the 3rd party json module has a different interface so we test
# to make sure we aren't using it.
if simplejson is None or hasattr(simplejson, 'read'):
simplejson = utils.python.universalImport('simplejson',
'local.simplejson')
except ImportError:
raise callbacks.Error, \
'You need Python2.6 or the simplejson module installed to use ' \
'this plugin. Download the module at ' \
'<http://undefined.org/python/#simplejson>.'
class Google(callbacks.PluginRegexp): class Google(callbacks.PluginRegexp):
threaded = True threaded = True
@ -132,14 +113,13 @@ class Google(callbacks.PluginRegexp):
if 'rsz' not in opts: if 'rsz' not in opts:
opts['rsz'] = 'large' opts['rsz'] = 'large'
fd = utils.web.getUrlFd('%s?%s' % (self._gsearchUrl, text = utils.web.getUrl('%s?%s' % (self._gsearchUrl,
urllib.urlencode(opts)), urllib.urlencode(opts)),
headers) headers=headers).decode('utf8')
json = simplejson.load(fd) data = json.loads(text)
fd.close() if data['responseStatus'] != 200:
if json['responseStatus'] != 200:
raise callbacks.Error, _('We broke The Google!') raise callbacks.Error, _('We broke The Google!')
return json return data
def formatData(self, data, bold=True, max=0, onetoone=False): def formatData(self, data, bold=True, max=0, onetoone=False):
if isinstance(data, basestring): if isinstance(data, basestring):
@ -174,9 +154,9 @@ class Google(callbacks.PluginRegexp):
opts = dict(opts) opts = dict(opts)
data = self.search(text, msg.args[0], {'smallsearch': True}) data = self.search(text, msg.args[0], {'smallsearch': True})
if data['responseData']['results']: if data['responseData']['results']:
url = data['responseData']['results'][0]['unescapedUrl'].encode('utf-8') url = data['responseData']['results'][0]['unescapedUrl']
if opts.has_key('snippet'): if opts.has_key('snippet'):
snippet = data['responseData']['results'][0]['content'].encode('utf-8') snippet = data['responseData']['results'][0]['content']
snippet = " | " + utils.web.htmlToText(snippet, tagReplace='') snippet = " | " + utils.web.htmlToText(snippet, tagReplace='')
else: else:
snippet = "" snippet = ""
@ -288,7 +268,7 @@ class Google(callbacks.PluginRegexp):
Uses Google's calculator to calculate the value of <expression>. Uses Google's calculator to calculate the value of <expression>.
""" """
urlig = self._googleUrlIG(expr) urlig = self._googleUrlIG(expr)
js = utils.web.getUrl(urlig) js = utils.web.getUrl(urlig).decode('utf8')
# fix bad google json # fix bad google json
js = js \ js = js \
.replace('lhs:','"lhs":') \ .replace('lhs:','"lhs":') \
@ -296,7 +276,7 @@ class Google(callbacks.PluginRegexp):
.replace('error:','"error":') \ .replace('error:','"error":') \
.replace('icc:','"icc":') \ .replace('icc:','"icc":') \
.replace('\\', '\\\\') .replace('\\', '\\\\')
js = simplejson.loads(js) js = json.loads(js)
# Currency conversion # Currency conversion
if js['icc'] == True: if js['icc'] == True:
@ -304,7 +284,7 @@ class Google(callbacks.PluginRegexp):
return return
url = self._googleUrl(expr) url = self._googleUrl(expr)
html = utils.web.getUrl(url) html = utils.web.getUrl(url).decode('utf8')
match = self._calcRe1.search(html) match = self._calcRe1.search(html)
if match is None: if match is None:
match = self._calcRe2.search(html) match = self._calcRe2.search(html)
@ -328,7 +308,7 @@ class Google(callbacks.PluginRegexp):
Looks <phone number> up on Google. Looks <phone number> up on Google.
""" """
url = self._googleUrl(phonenumber) url = self._googleUrl(phonenumber)
html = utils.web.getUrl(url) html = utils.web.getUrl(url).decode('utf8')
m = self._phoneRe.search(html) m = self._phoneRe.search(html)
if m is not None: if m is not None:
s = m.group(1) s = m.group(1)