diff --git a/plugins/gcalc.py b/plugins/gcalc.py new file mode 100644 index 0000000..521b12e --- /dev/null +++ b/plugins/gcalc.py @@ -0,0 +1,27 @@ +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)