summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/gcalc.py45
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.'