summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2012-08-19 19:58:36 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2012-08-19 19:58:36 +0200
commit1d6d92d2dd1bf9d87d7065b46e8a60086cd05c02 (patch)
treeb4436e11da96d7deae8878abd2c15e3c21aef3bc
parentdd95fca7fbae1211a7236b4418d0178b64f46d42 (diff)
Added WolframAlpha module.
-rw-r--r--modules/wolframalpha.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/modules/wolframalpha.py b/modules/wolframalpha.py
new file mode 100644
index 0000000..0750578
--- /dev/null
+++ b/modules/wolframalpha.py
@@ -0,0 +1,43 @@
+info = {
+ 'author': 'Jon Bergli Heier',
+ 'title': 'WolframAlpha',
+ 'description': 'WolframAlpha',
+}
+
+import urllib, urllib2
+from lxml.etree import ElementTree
+
+class Module(object):
+ config_section = 'module/wolframalpha'
+
+ def __init__(self, bot):
+ self.irc = bot
+ if self.irc:
+ self.irc.register_keyword('!wa', self)
+
+ def keyword(self, nick, channel, kw, msg):
+ target = channel if not channel == self.irc.nickname else nick.split('!')[0]
+ query = {
+ 'appid': config.get(self.config_section, 'api_key'),
+ 'format': 'plaintext',
+ 'input': msg,
+ }
+ if config.has_option(self.config_section, 'location'):
+ query['location'] = config.get(self.config_section, 'location')
+ u = urllib2.urlopen('http://api.wolframalpha.com/v2/query?%s' % urllib.urlencode(query))
+ xmldoc = ElementTree()
+ xmldoc.parse(u)
+ xml = xmldoc.getroot()
+ if xml.attrib['success'] != 'true':
+ # TODO: check didyoumean tags
+ self.irc.msg(target, 'No result.')
+ return
+
+ result = None
+ for pod in xml.findall('pod'):
+ if pod.attrib['id'] == 'Result':
+ result = pod.find('subpod/plaintext').text
+ if result:
+ self.irc.msg(target, 'Result: %s' % result)
+ else:
+ self.irc.msg(target, 'Something failed.')