Skip to content

Commit

Permalink
WA: Add number of output option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Terje Hoås committed Oct 5, 2012
1 parent fdf0320 commit 578add0
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Wolfram/plugin.py
Expand Up @@ -6,16 +6,22 @@

class Wolfram(callbacks.Privmsg):

def alpha(self, irc, msg, args, question):
"""Ask Mr. Wolfram a question, get an "answer"...maybe? It uses the
Wolfram Alpha API.
def alpha(self, irc, msg, args, options, question):
"""[--lines num] <query>
Ask Mr. Wolfram a question, get an "answer"...maybe? It uses the Wolfram Alpha API.
<http://products.wolframalpha.com/docs/WolframAlpha-API-Reference.pdf>
"""
apikey = self.registryValue('apikey')
if not apikey or apikey == "Not set":
irc.reply("API key not set. see 'config help supybot.plugins.Wolfram.apikey'.")
return

maxoutput = 2
for (key, value) in options:
if key == 'lines':
maxoutput = value

u = "http://api.wolframalpha.com/v2/query?"
q = urllib.urlencode({'input': question, 'appid': apikey})
xml = urllib.urlopen(u + q).read()
Expand All @@ -30,7 +36,6 @@ def alpha(self, irc, msg, args, question):
return

found = False
maxoutput = 2
outputcount = 0
for pod in tree.findall('.//pod'):
title = pod.attrib['title']
Expand All @@ -47,12 +52,17 @@ def alpha(self, irc, msg, args, question):
output = plaintext.text
output = output.replace(' | ', ': ')
output = output.replace('\n', ', ')
# Skip the input interpretation if only one line out.
if maxoutput == 1 and outputcount == 0:
maxoutput = 2 # hack :D
outputcount += 1
continue
irc.reply(("%s: %s" % (title, output.encode('utf-8'))))
outputcount += 1
if not found:
irc.reply("huh, I dunno, I'm still a baby AI. Wait till the singularity I guess?")

alpha = wrap(alpha, ['text'])
alpha = wrap(alpha, [getopts({'lines':'int'}), 'text'])


Class = Wolfram
Expand Down

0 comments on commit 578add0

Please sign in to comment.