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
452 B
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
import re
from util import hook, http, misc
from bs4 import BeautifulSoup
2011-11-20 10:23:31 +01:00
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
page = http.get('http://www.google.com/search', q=inp)
soup = BeautifulSoup(page, 'lxml')
2011-11-20 10:23:31 +01:00
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 '%s'" % inp
2011-11-20 10:23:31 +01:00
return http.unescape(response.contents[0])