This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/gcalc.py
2012-02-27 22:46:47 -08:00

32 lines
723 B
Python
Executable file

import re
from util import hook, http, misc
from BeautifulSoup import BeautifulSoup
@hook.command("calc")
@hook.command("math")
def calc(inp):
".calc <term> -- Calculate <term> with Google Calc."
white_re = re.compile(r'\s+')
page = http.get('http://www.google.com/search', q=inp)
soup = BeautifulSoup(page)
response = soup.find('h2', {'class' : 'r'})
if response is None:
return "Could not calculate " + inp
output = response.renderContents()
output = ' '.join(output.splitlines())
output = output.replace("\xa0", ",")
output = white_re.sub(' ', output.strip())
output = output.decode('utf-8', 'ignore')
output = misc.strip_html(output)
return output