Added google calculator module

This commit is contained in:
liebot@grimace 2010-02-22 17:25:01 +00:00
parent ca8b24518e
commit 5cc1366e0d
1 changed files with 27 additions and 0 deletions

27
plugins/gcalc.py Normal file
View File

@ -0,0 +1,27 @@
from util import hook
import urllib, httplib, sys
def doquery(argv):
query=urllib.urlencode({'q':argv})
start='<h2 class=r style="font-size:138%"><b>'
end='</b>'
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("<font size=-2> </font>",",").replace(" &#215; 10<sup>","E").replace("</sup>","").replace("\xa0",",")
return result
@hook.command
def calc(inp):
'''.calc <term> -- returns Google Calculator result'''
if not inp or not inp.strip():
return calc.__doc__
return doquery(inp)