Google: Restore @calc and @phonebook.

This commit is contained in:
Valentin Lorentz 2013-02-03 07:53:55 +00:00
parent 34ea319747
commit 5b432a781e
2 changed files with 80 additions and 4 deletions

View File

@ -296,6 +296,70 @@ class Google(callbacks.PluginRegexp):
url = r'http://www.google.com/ig/calculator?hl=en&q=' + s
return url
_calcRe1 = re.compile(r'<table.*class="?obcontainer"?[^>]*>(.*?)</table>', re.I)
_calcRe2 = re.compile(r'<h\d class="?r"?[^>]*>(?:<b>)?(.*?)(?:</b>)?</h\d>', re.I | re.S)
_calcSupRe = re.compile(r'<sup>(.*?)</sup>', re.I)
_calcFontRe = re.compile(r'<font size=-2>(.*?)</font>')
_calcTimesRe = re.compile(r'&(?:times|#215);')
@internationalizeDocstring
def calc(self, irc, msg, args, expr):
"""<expression>
Uses Google's calculator to calculate the value of <expression>.
"""
urlig = self._googleUrlIG(expr)
js = utils.web.getUrl(urlig).decode('utf8')
# Convert JavaScript to JSON. Ouch.
js = js \
.replace('lhs:','"lhs":') \
.replace('rhs:','"rhs":') \
.replace('error:','"error":') \
.replace('icc:','"icc":') \
.replace('\\', '\\\\')
js = json.loads(js)
url = self._googleUrl(expr)
html = utils.web.getUrl(url).decode('utf8')
match = self._calcRe1.search(html)
if match is None:
match = self._calcRe2.search(html)
if match is not None:
s = match.group(1)
s = self._calcSupRe.sub(r'^(\1)', s)
s = self._calcFontRe.sub(r',', s)
s = self._calcTimesRe.sub(r'*', s)
s = utils.web.htmlToText(s)
if ' = ' in s: # Extra check, since the regex seems to fail.
irc.reply(s)
return
elif js['lhs'] and js['rhs']:
# Outputs the original result. Might look ugly.
irc.reply("%s = %s" % (js['lhs'], js['rhs'],))
return
irc.reply(_('Google says: Error: %s.') % (js['error'],))
irc.reply('Google\'s calculator didn\'t come up with anything.')
calc = wrap(calc, ['text'])
_phoneRe = re.compile(r'Phonebook.*?<font size=-1>(.*?)<a href')
@internationalizeDocstring
def phonebook(self, irc, msg, args, phonenumber):
"""<phone number>
Looks <phone number> up on Google.
"""
url = self._googleUrl(phonenumber)
html = utils.web.getUrl(url).decode('utf8')
m = self._phoneRe.search(html)
if m is not None:
s = m.group(1)
s = s.replace('<b>', '')
s = s.replace('</b>', '')
s = utils.web.htmlToText(s)
irc.reply(s)
else:
irc.reply(_('Google\'s phonebook didn\'t come up with anything.'))
phonebook = wrap(phonebook, ['text'])
Class = Google

View File

@ -1,6 +1,6 @@
###
# Copyright (c) 2002-2004, Jeremiah Fincher
# Copyright (c) 2008-2009, James McCoy
# Copyright (c) 2008-2009, James Vega
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -33,6 +33,21 @@ from supybot.test import *
class GoogleTestCase(ChannelPluginTestCase):
plugins = ('Google', 'Config')
if network:
def testCalcHandlesMultiplicationSymbol(self):
self.assertNotRegexp('google calc seconds in a century', r'215')
def testCalc(self):
self.assertNotRegexp('google calc e^(i*pi)+1', r'didn\'t')
self.assertNotRegexp('google calc 1 usd in gbp', r'didn\'t')
def testHtmlHandled(self):
self.assertNotRegexp('google calc '
'the speed of light '
'in microns / fortnight', '<sup>')
self.assertNotRegexp('google calc '
'the speed of light '
'in microns / fortnight', '&times;')
def testSearch(self):
self.assertNotError('google foo')
self.assertRegexp('google dupa', r'dupa')
@ -54,9 +69,6 @@ class GoogleTestCase(ChannelPluginTestCase):
self.assertRegexp('fight supybot moobot', r'.*supybot.*: \d+')
self.assertNotError('fight ... !')
def testTranslate(self):
self.assertRegexp('translate en es hello world', 'mundo')
def testCalcDoesNotHaveExtraSpaces(self):
self.assertNotRegexp('google calc 1000^2', r'\s+,\s+')