h/plugins/gcalc.py

18 lines
333 B
Python
Raw Normal View History

from util import hook, http
2010-02-22 17:25:01 +00:00
2010-03-01 02:32:41 +00:00
@hook.command
def calc(inp):
'''.calc <term> -- returns Google Calculator result'''
2010-02-22 17:25:01 +00:00
2011-11-07 20:16:56 +00:00
h = http.get_html('http://www.google.com/search', q=inp)
2010-02-22 17:25:01 +00:00
2011-11-07 20:16:56 +00:00
m = h.xpath('//h2[@class="r"]/text()')
2010-02-22 17:25:01 +00:00
2011-11-07 20:16:56 +00:00
if not m:
return "could not calculate " + inp
2010-02-22 17:25:01 +00:00
2011-11-07 20:16:56 +00:00
res = ' '.join(m[0].split())
return res