diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-10-10 21:34:41 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-10-10 21:34:41 +0200 |
commit | c638647fde7934f5367ebbfec4778726cf9bc1ab (patch) | |
tree | 3b5847710ae66a5b88ed0c717f656fdbc7ddc320 | |
parent | 2d552945b9fbfd4333c96e42d5bb7161aebef879 (diff) |
Added gcalc module.
-rw-r--r-- | modules/gcalc.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/modules/gcalc.py b/modules/gcalc.py new file mode 100644 index 0000000..1596f8d --- /dev/null +++ b/modules/gcalc.py @@ -0,0 +1,45 @@ +info = { + 'author': 'Jon Bergli Heier', + 'title': 'Google Calculator', + 'description': 'Google Calculator', +} + +import urllib, urllib2, json + +class Module: + def __init__(self, bot): + self.irc = bot + 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().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.' |