From c638647fde7934f5367ebbfec4778726cf9bc1ab Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 10 Oct 2010 21:34:41 +0200 Subject: Added gcalc module. --- modules/gcalc.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 modules/gcalc.py (limited to 'modules') 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.' -- cgit v1.2.3