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.'