First :D
This commit is contained in:
commit
37588421f3
100 changed files with 22673 additions and 0 deletions
32
plugins/gcalc.py
Normal file
32
plugins/gcalc.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
import re
|
||||
|
||||
from util import hook, http, misc
|
||||
from BeautifulSoup import BeautifulSoup
|
||||
|
||||
@hook.command("calc")
|
||||
@hook.command("math")
|
||||
def calc(inp):
|
||||
'''.calc <term> -- returns Google Calculator result'''
|
||||
|
||||
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
|
||||
|
Reference in a new issue