From b991c8679bfbfb35eb643295d2c8ed0ad11c09a5 Mon Sep 17 00:00:00 2001 From: Daniel Folkinshteyn Date: Mon, 12 Dec 2011 14:57:10 -0500 Subject: [PATCH] Google: use web scraping as fallback to ig api ig api doesn't have everything (for one, timezones), and also, in case the IG api ever dies. Conflicts: plugins/Google/plugin.py --- plugins/Google/plugin.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/plugins/Google/plugin.py b/plugins/Google/plugin.py index b6466ced3..7e0caa5b6 100644 --- a/plugins/Google/plugin.py +++ b/plugins/Google/plugin.py @@ -322,27 +322,53 @@ class Google(callbacks.PluginRegexp): googleSnarfer = urlSnarfer(googleSnarfer) def _googleUrl(self, s): + s = s.replace('+', '%2B') + s = s.replace(' ', '+') + url = r'http://google.com/search?q=' + s + return url + + def _googleUrlIG(self, s): s = s.replace('+', '%2B') s = s.replace(' ', '+') url = r'http://www.google.com/ig/calculator?hl=en&q=' + s return url @internationalizeDocstring + _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);') def calc(self, irc, msg, args, expr): """ Uses Google's calculator to calculate the value of . """ - url = self._googleUrl(expr) - js = utils.web.getUrl(url) + urlig = self._googleUrlIG(expr) + js = utils.web.getUrl(urlig) # fix bad google json js = js.replace('lhs:','"lhs":').replace('rhs:','"rhs":').replace('error:','"error":').replace('icc:','"icc":') js = simplejson.loads(js) if js['error'] == '': irc.reply("%s = %s" % (js['lhs'], js['rhs'],)) + return + + url = self._googleUrl(expr) + html = utils.web.getUrl(url) + 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) + irc.reply(s) else: 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.*?(.*?)