2012-04-19 12:37:36 +02:00
|
|
|
# Plugin by Lukeroge
|
2012-03-06 21:16:22 +01:00
|
|
|
|
2012-03-11 20:45:49 +01:00
|
|
|
from util import hook
|
|
|
|
from util import http
|
2012-04-20 16:35:35 +02:00
|
|
|
from util.formatting import capitalize_first
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2012-04-19 12:37:36 +02:00
|
|
|
api_url = 'http://api.wolframalpha.com/v2/query?format=plaintext'
|
2012-03-12 12:21:05 +01:00
|
|
|
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
@hook.command("time")
|
2012-04-19 12:37:36 +02:00
|
|
|
def time_command(inp, bot=None):
|
2012-02-28 03:03:43 +01:00
|
|
|
".time <area> -- Gets the time in <area>"
|
2012-03-12 12:21:05 +01:00
|
|
|
|
2012-04-19 12:37:36 +02:00
|
|
|
query = "current time in %s" % inp
|
2012-04-20 16:35:35 +02:00
|
|
|
|
2012-04-19 12:37:36 +02:00
|
|
|
api_key = bot.config.get("api_keys", {}).get("wolframalpha", None)
|
|
|
|
if not api_key:
|
|
|
|
return "error: no wolfram alpha api key set"
|
|
|
|
|
|
|
|
request = http.get_xml(api_url, input=query, appid=api_key)
|
|
|
|
time = " ".join(request.xpath("//pod[@title='Result']/subpod/plain" \
|
|
|
|
"text/text()"))
|
|
|
|
time = time.replace(" | ", ", ")
|
2012-04-20 16:35:35 +02:00
|
|
|
|
2012-04-20 15:45:02 +02:00
|
|
|
# nice place name for UNIX time
|
|
|
|
if inp.lower() == "unix":
|
|
|
|
place = "Unix Epoch"
|
|
|
|
else:
|
2012-04-20 16:35:35 +02:00
|
|
|
place = capitalize_first(" ".join(request.xpath("//pod[@title='Input" \
|
|
|
|
" interpretation']/subpod/plaintext/text()"))[16:])
|
2012-04-19 12:37:36 +02:00
|
|
|
|
|
|
|
if time:
|
2012-04-20 16:35:35 +02:00
|
|
|
# if wolfram alpha had to guess a place, then show the place it chose
|
2012-04-19 12:37:36 +02:00
|
|
|
#if request.xpath("//assumptions"):
|
|
|
|
# return "%s - \x02%s\x02" % (time, place)
|
|
|
|
#else:
|
|
|
|
# return time
|
2012-04-20 16:35:35 +02:00
|
|
|
return "%s - \x02%s\x02" % (time, place)
|
2012-03-06 21:16:22 +01:00
|
|
|
else:
|
2012-04-20 15:45:02 +02:00
|
|
|
return "Could not get the time for '%s'" % inp
|