Skip to content

Instantly share code, notes, and snippets.

@passcod
Created November 23, 2012 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passcod/4137174 to your computer and use it in GitHub Desktop.
Save passcod/4137174 to your computer and use it in GitHub Desktop.
Wolfram|Alpha mod for •AZ•BOT•
"""
Wolfram|Alpha mod
Provides .wa and .time and other
calculation-related commands.
Depends on:
core mods (azbot >= 1.4.0)
wolframalpha (pip install ~)
Config options:
wolfram:
# App ID, obtainable from
# http://products.wolframalpha.com/api/
#
# Required.
id: "XXXXXXX-XXXX"
# Local timezone, useful if the bot isn't
# running in the same timezone as its users.
#
# If not present, the timezone choice is
# left to Wolfram.
timezone: "Auckland, NZ"
"""
import _util
import wolframalpha
config = {}
client = None
def init_wa(bot):
if 'wolfram' in bot.config and 'id' in bot.config['wolfram']:
global config
global client
if not client:
config = bot.config['wolfram']
client = wolframalpha.Client(config['id'])
return True
else:
print "[!] Wolfram: not configured."
def results(query):
res = client.query(query)
if len(res.pods) > 0:
texts = []
for pod in res.pods[1:]:
if pod.text:
texts.append(pod.text)
return texts
else:
return None
def wa(opts, args=[]):
ok = init_wa(opts['bot'])
if not ok: return
if len(args) == 0:
_util.give_help(opts, "<query>")
return
res = results(' '.join(args))
if res:
text = "\n".join(res).encode('utf-8')
_util.answer(opts, text)
else:
_util.answer(opts, "No results.")
def loc_condition(opts, type, args=[]):
ok = init_wa(opts['bot'])
if not ok: return
if len(args) == 0:
args = ["here"]
if args[0] == "?" or args[0] == "help":
_util.give_help(opts, "[in <place>]")
return
if args[0] == "in":
timezone = ' '.join(args)
elif 'timezone' in config:
timezone = "in %s" % config['timezone']
else:
timezone = ""
res = results("%s %s" % (type, timezone))
if res:
text = "\n".join(res[:1]).encode('utf-8')
_util.answer(opts, text)
else:
_util.answer(opts, "Boop.")
def give_time(opts, args=[]):
loc_condition(opts, "time", args)
def give_weather(opts, args=[]):
loc_condition(opts, "weather", args)
_util.register(wa, 'command', 'wa')
_util.register(give_time, 'command', 'time')
_util.register(give_weather, 'command', 'weather')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment