2013-01-20 04:12:11 +00:00
|
|
|
from util import hook, http
|
|
|
|
from random import choice
|
|
|
|
|
2014-01-14 21:12:37 +00:00
|
|
|
|
2013-01-27 22:40:31 +00:00
|
|
|
@hook.api_key('yahoo')
|
2013-01-20 04:12:11 +00:00
|
|
|
@hook.command
|
2013-01-27 22:40:31 +00:00
|
|
|
def answer(inp, api_key=None):
|
2013-01-20 04:12:11 +00:00
|
|
|
".answer <query> -- find the answer to a question on Yahoo! Answers"
|
|
|
|
|
|
|
|
url = "http://answers.yahooapis.com/AnswersService/V1/questionSearch"
|
|
|
|
|
|
|
|
result = http.get_json(url,
|
2014-01-14 21:12:37 +00:00
|
|
|
query=inp,
|
|
|
|
search_in="question",
|
|
|
|
output="json",
|
|
|
|
appid=api_key)
|
2013-01-20 04:12:11 +00:00
|
|
|
|
|
|
|
questions = result.get("all", {}).get("questions", [])
|
|
|
|
answered = filter(lambda x: x.get("ChosenAnswer", ""), questions)
|
|
|
|
|
|
|
|
if not answered:
|
|
|
|
return "no results"
|
|
|
|
|
|
|
|
chosen = choice(answered)
|
|
|
|
answer, link = chosen["ChosenAnswer"], chosen["Link"]
|
|
|
|
response = "%s -- %s" % (link, answer)
|
|
|
|
|
|
|
|
return " ".join(response.split())
|