diff --git a/plugins/gcalc.py b/plugins/gcalc.py index 6212385..410f126 100644 --- a/plugins/gcalc.py +++ b/plugins/gcalc.py @@ -1,5 +1,3 @@ -import re - from util import hook, http @@ -7,15 +5,13 @@ from util import hook, http def calc(inp): '''.calc -- returns Google Calculator result''' - page = http.get('http://www.google.com/search', q=inp) + h = http.get_html('http://www.google.com/search', q=inp) - # ugh, scraping HTML with regexes - m = re.search(r'

(.*?)', page) + m = h.xpath('//h2[@class="r"]/text()') - if m is None: + if not m: return "could not calculate " + inp - result = m.group(1).replace(" ", ",") - result = result.replace(" × 10", "E").replace("", "") - result = result.replace("\xa0", ",") - return result + res = ' '.join(m[0].split()) + + return res