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
Luke Rogers d3e5099dab silly attempt to make calc plugin last longer before google totally breaks it
cookies are an attempt to make it less likely to realise cloudbot is a bot, and the IE user agent is to force it to use the legacy HTML we can actually parse
2013-09-19 10:58:22 +12:00

21 lines
536 B
Python
Executable file

from util import hook, http
@hook.command("math")
@hook.command
def calc(inp):
"""calc <term> -- Calculate <term> with Google Calc."""
soup = http.get_soup('http://www.google.com/search', q=inp, cookies=True, user_agent=http.ua_internetexplorer)
result = soup.find('h2', {'class': 'r'})
exponent = result.find('sup')
if not result:
return "Could not calculate '{}'".format(inp)
if not exponent:
return result.contents[0]
if exponent:
return "{}^{}".format(result.contents[0]*2)