1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
info = {
'author': 'Jon Bergli Heier',
'title': 'Google Calculator',
'description': 'Google Calculator',
}
import urllib, urllib2, json
class Module:
def __init__(self, bot):
self.irc = bot
if self.irc:
self.irc.register_keyword('!gcalc', self)
def calc(self, s):
url = 'http://www.google.com/ig/calculator?%s' % urllib.urlencode({'q': s})
u = urllib2.urlopen(url)
# fix object keys
data = u.read().decode('iso-8859-1').replace('lhs', '"lhs"').replace('rhs', '"rhs"').replace('error', '"error"').replace('icc', '"icc"')
data = json.loads(data)
if len(data['error']):
return
return '%s = %s' % (data['lhs'], data['rhs'])
def keyword(self, nick, channel, kw, msg):
target = channel if not channel == self.irc.nickname else nick.split('!')[0]
args = msg.split()
if len(args) == 0:
self.irc.msg(target, 'Usage: !gcalc expression')
return
results = self.calc(' '.join(args))
if results:
self.irc.msg(target, results.encode('utf-8'))
else:
self.irc.msg(target, 'Something failed.')
if __name__ == '__main__':
import sys
m = Module(None)
search = ' '.join(sys.argv[1:])
results = m.calc(search)
if results:
print results
else:
print 'No results.'
|