From dd723eafe06f42cfabe6c8b0ac5c919801ec9569 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Sun, 31 Jan 2010 22:49:52 -0700 Subject: [PATCH] wolframalpha.py: gets results from WA (not using the commercial API)-- works pretty well --- core/main.py | 2 +- plugins/wolframalpha.py | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 plugins/wolframalpha.py diff --git a/core/main.py b/core/main.py index eaf3974..cd3a00f 100644 --- a/core/main.py +++ b/core/main.py @@ -16,7 +16,7 @@ class Input(object): self.host = host self.paraml = paraml # params[-1] without the : self.msg = msg - self.chan = paraml[0] + self.chan = paraml[0].lower() if self.chan == conn.nick: # is a PM self.chan = nick diff --git a/plugins/wolframalpha.py b/plugins/wolframalpha.py new file mode 100644 index 0000000..28c6cbb --- /dev/null +++ b/plugins/wolframalpha.py @@ -0,0 +1,50 @@ +import re +import urllib2 + +from lxml import html + +from util import hook + +@hook.command +@hook.command('wa') +def wolframalpha(inp): + ".wa/.wolframalpha -- scrapes Wolfram Alpha's" \ + "results for " + + if not inp: + return wolframalpha.__query__ + + url = "http://www.wolframalpha.com/input/?i=%s&asynchronous=false" + + h = html.parse(url % urllib2.quote(inp, safe='')) + + pods = h.xpath("//div[@class='pod ']") + + pod_texts = [] + for pod in pods: + heading = pod.find('h1/span').text_content().strip() + results = [] + for image in pod.xpath('div/div[@class="output"]/img'): + alt = image.attrib['alt'].strip() + alt = alt.replace('\\n', '; ') + alt = re.sub(r'\s+', ' ', alt) + if alt: + results.append(alt) + if results: + pod_texts.append(heading + ' ' + '|'.join(results)) + + ret = '. '.join(pod_texts[1:]) # first pod is the input + + if not ret: + if not pod_texts: + return 'no results' + ret = pod_texts[0] # definite integrals have only the result pod first + + if len(ret) > 430: + ret = ret[:ret.rfind(' ', 0, 430)] + ret = re.sub(r'\W+$', '', ret) + '...' + + if not ret: + return 'no result' + + return ret