From 42d78d06ae6b1b0787673ff1218a21eeb1f822c9 Mon Sep 17 00:00:00 2001 From: Tom Jakubowski Date: Sat, 19 Jan 2013 20:12:11 -0800 Subject: [PATCH] Add Yahoo! Answers plugin The .answer command will search Yahoo! Answers with the given query and respond with one of the 'chosen' answers returned by Yahoo's API and a link to the question. --- plugins/yahooanswers.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 plugins/yahooanswers.py diff --git a/plugins/yahooanswers.py b/plugins/yahooanswers.py new file mode 100644 index 0000000..3705eaa --- /dev/null +++ b/plugins/yahooanswers.py @@ -0,0 +1,31 @@ +from util import hook, http +from random import choice + +@hook.command +def answer(inp, bot=None): + ".answer -- find the answer to a question on Yahoo! Answers" + + url = "http://answers.yahooapis.com/AnswersService/V1/questionSearch" + app_id = bot.config.get("api_keys", {}).get("yahoo", None) + + if app_id is None: + return "error: yahoo appid not set" + + result = http.get_json(url, + query=inp, + search_in="question", + output="json", + appid=app_id) + + 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()) +