2010-03-01 01:39:29 +00:00
|
|
|
import re
|
|
|
|
|
2010-04-23 03:47:41 +00:00
|
|
|
from util import hook, http
|
2010-02-22 17:25:01 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-03-01 01:39:29 +00:00
|
|
|
@hook.command
|
|
|
|
def calc(inp):
|
|
|
|
'''.calc <term> -- returns Google Calculator result'''
|
|
|
|
if not inp:
|
|
|
|
return calc.__doc__
|
2010-02-22 17:25:01 +00:00
|
|
|
|
2010-04-23 03:47:41 +00:00
|
|
|
page = http.get('http://www.google.com/search', q=inp)
|
2010-02-22 17:25:01 +00:00
|
|
|
|
2010-03-01 01:39:29 +00:00
|
|
|
# ugh, scraping HTML with regexes
|
|
|
|
m = re.search(r'<h2 class=r style="font-size:138%"><b>(.*?)</b>', page)
|
2010-02-22 17:25:01 +00:00
|
|
|
|
2010-03-01 01:39:29 +00:00
|
|
|
if m is None:
|
|
|
|
return "could not calculate " + inp
|
2010-02-22 17:25:01 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
result = m.group(1).replace("<font size=-2> </font>", ",")
|
|
|
|
result = result.replace(" × 10<sup>", "E").replace("</sup>", "")
|
|
|
|
result = result.replace("\xa0", ",")
|
2010-03-01 01:39:29 +00:00
|
|
|
return result
|