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

33 lines
714 B
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
import re
from util import hook, http, misc
from BeautifulSoup import BeautifulSoup
2012-03-12 19:13:32 +01:00
2011-11-20 10:23:31 +01:00
@hook.command("math")
2012-03-12 19:13:32 +01:00
@hook.command
2011-11-20 10:23:31 +01:00
def calc(inp):
2012-05-16 05:07:27 +02:00
"calc <term> -- Calculate <term> with Google Calc."
2011-11-20 10:23:31 +01:00
white_re = re.compile(r'\s+')
page = http.get('http://www.google.com/search', q=inp)
soup = BeautifulSoup(page)
2012-03-12 19:13:32 +01:00
response = soup.find('h2', {'class': 'r'})
2011-11-20 10:23:31 +01:00
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