from util import hook
import urllib, httplib, sys
def doquery(argv):
query=urllib.urlencode({'q':argv})
start='
'
end=''
google=httplib.HTTPConnection("www.google.com")
google.request("GET","/search?"+query)
search=google.getresponse()
data=search.read()
if data.find(start)==-1: return "Could not calculate " + argv
else:
begin=data.index(start)
result=data[begin+len(start):begin+data[begin:].index(end)]
result = result.replace(" ",",").replace(" × 10","E").replace("","").replace("\xa0",",")
return result
@hook.command
def calc(inp):
'''.calc -- returns Google Calculator result'''
if not inp or not inp.strip():
return calc.__doc__
return doquery(inp)