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

20 lines
484 B
Python
Raw Normal View History

2012-09-04 21:52:03 +02:00
from util import hook, http
2011-11-20 10:23:31 +01:00
2013-09-04 12:30:04 +02: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):
2013-09-04 12:30:04 +02:00
"""calc <term> -- Calculate <term> with Google Calc."""
2011-11-20 10:23:31 +01:00
2012-09-05 01:41:32 +02:00
soup = http.get_soup('http://www.google.com/search', q=inp)
2011-11-20 10:23:31 +01:00
2012-12-09 13:59:33 +01:00
result = soup.find('h2', {'class': 'r'})
exponent = result.find('sup')
2012-12-09 13:59:33 +01:00
if not result:
2013-09-05 03:46:49 +02:00
return "Could not calculate '{}'".format(inp)
2011-11-20 10:23:31 +01:00
if not exponent:
return result.contents[0]
if exponent:
2013-09-05 03:46:49 +02:00
return "{}^{}".format(result.contents[0]*2)