diff --git a/plugins/Google/plugin.py b/plugins/Google/plugin.py index 29bd09438..fe24f9fc1 100644 --- a/plugins/Google/plugin.py +++ b/plugins/Google/plugin.py @@ -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']*>(.*?)', re.I) + _calcRe2 = re.compile(r']*>(?:)?(.*?)(?:)?', re.I | re.S) + _calcSupRe = re.compile(r'(.*?)', re.I) + _calcFontRe = re.compile(r'(.*?)') + _calcTimesRe = re.compile(r'&(?:times|#215);') + @internationalizeDocstring + def calc(self, irc, msg, args, expr): + """ + + Uses Google's calculator to calculate the value of . + """ + 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.*?(.*?) + + Looks 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('', '') + s = s.replace('', '') + 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 diff --git a/plugins/Google/test.py b/plugins/Google/test.py index 788e4e844..9d9495984 100644 --- a/plugins/Google/test.py +++ b/plugins/Google/test.py @@ -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', '') + self.assertNotRegexp('google calc ' + 'the speed of light ' + 'in microns / fortnight', '×') + 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+')