Google: add some extra matching capability to google calc

now should be able to display any 'special' result from google.
This commit is contained in:
Daniel Folkinshteyn 2010-12-01 16:52:01 -05:00
parent 8b702543fa
commit 42464d8180
3 changed files with 8 additions and 3 deletions

View File

@ -319,7 +319,8 @@ class Google(callbacks.PluginRegexp):
url = r'http://google.com/search?q=' + s url = r'http://google.com/search?q=' + s
return url return url
_calcRe = re.compile(r'<h\d class="?r"?.*?<b>(.*?)</b>', re.I) _calcRe1 = re.compile(r'<table.*class="?obcontainer"?[^>]*>(.*?)</table>', re.I)
_calcRe2 = re.compile(r'<h\d class="?r"?.*?<b>(.*?)</b>', re.I)
_calcSupRe = re.compile(r'<sup>(.*?)</sup>', re.I) _calcSupRe = re.compile(r'<sup>(.*?)</sup>', re.I)
_calcFontRe = re.compile(r'<font size=-2>(.*?)</font>') _calcFontRe = re.compile(r'<font size=-2>(.*?)</font>')
_calcTimesRe = re.compile(r'&(?:times|#215);') _calcTimesRe = re.compile(r'&(?:times|#215);')
@ -330,12 +331,15 @@ class Google(callbacks.PluginRegexp):
""" """
url = self._googleUrl(expr) url = self._googleUrl(expr)
html = utils.web.getUrl(url) html = utils.web.getUrl(url)
match = self._calcRe.search(html) match = self._calcRe1.search(html)
if match is None:
match = self._calcRe2.search(html)
if match is not None: if match is not None:
s = match.group(1) s = match.group(1)
s = self._calcSupRe.sub(r'^(\1)', s) s = self._calcSupRe.sub(r'^(\1)', s)
s = self._calcFontRe.sub(r',', s) s = self._calcFontRe.sub(r',', s)
s = self._calcTimesRe.sub(r'*', s) s = self._calcTimesRe.sub(r'*', s)
s = utils.web.htmlToText(s)
irc.reply(s) irc.reply(s)
else: else:
irc.reply('Google\'s calculator didn\'t come up with anything.') irc.reply('Google\'s calculator didn\'t come up with anything.')

View File

@ -39,6 +39,7 @@ class GoogleTestCase(ChannelPluginTestCase):
def testCalc(self): def testCalc(self):
self.assertNotRegexp('google calc e^(i*pi)+1', r'didn\'t') self.assertNotRegexp('google calc e^(i*pi)+1', r'didn\'t')
self.assertNotRegexp('google calc 1 usd in gbp', r'didn\'t') self.assertNotRegexp('google calc 1 usd in gbp', r'didn\'t')
self.assertRegexp('google calc current time in usa', r'Time in.*USA')
def testHtmlHandled(self): def testHtmlHandled(self):
self.assertNotRegexp('google calc ' self.assertNotRegexp('google calc '

View File

@ -1,3 +1,3 @@
"""stick the various versioning attributes in here, so we only have to change """stick the various versioning attributes in here, so we only have to change
them once.""" them once."""
version = '0.83.4.1+gribble (2010-10-10T17:52:04-0400)' version = '0.83.4.1+gribble (2010-12-01T16:53:08-0500)'