From a29dffbfb77efe790900113692bf507a919567df Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Sun, 27 Jan 2013 16:40:31 -0600 Subject: [PATCH] refactor apikey retrieval, fix lastfm with empty input --- core/main.py | 7 +++++++ plugins/lastfm.py | 10 +++------- plugins/rottentomatoes.py | 12 ++++-------- plugins/util/hook.py | 7 +++++++ plugins/weather.py | 4 ++-- plugins/wolframalpha.py | 7 ++----- plugins/yahooanswers.py | 9 +++------ 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/core/main.py b/core/main.py index 3e344b6..cafc3d3 100644 --- a/core/main.py +++ b/core/main.py @@ -125,6 +125,13 @@ def dispatch(input, kind, func, args, autohelp=False): input.reply(func.__doc__) return + if hasattr(func, '_apikey'): + key = bot.config.get('api_keys', {}).get(func._apikey, None) + if key is None: + input.reply('error: missing api key') + return + input.api_key = key + if func._thread: bot.threads[func].put(input) else: diff --git a/plugins/lastfm.py b/plugins/lastfm.py index 4e77f65..b87d02b 100644 --- a/plugins/lastfm.py +++ b/plugins/lastfm.py @@ -7,13 +7,9 @@ from util import hook, http api_url = "http://ws.audioscrobbler.com/2.0/?format=json" - -@hook.command -def lastfm(inp, nick='', say=None, bot=None): - api_key = bot.config.get("api_keys", {}).get("lastfm", None) - if not api_key: - return None - +@hook.api_key('lastfm') +@hook.command(autohelp=False) +def lastfm(inp, nick='', say=None, api_key=None): if inp: user = inp else: diff --git a/plugins/rottentomatoes.py b/plugins/rottentomatoes.py index 295cc10..691dfb5 100644 --- a/plugins/rottentomatoes.py +++ b/plugins/rottentomatoes.py @@ -5,17 +5,13 @@ movie_search_url = api_root + 'movies.json' movie_reviews_url = api_root + 'movies/%s/reviews.json' +@hook.api_key('rottentomatoes') @hook.command('rt') -def rottentomatoes(inp, bot=None): +@hook.command +def rottentomatoes(inp, api_key=None): '.rt -- gets ratings for <title> from Rotten Tomatoes' - api_key = bot.config.get("api_keys", {}).get("rottentomatoes", None) - if not api_key: - return - - title = inp.strip() - - results = http.get_json(movie_search_url, q=title, apikey=api_key) + results = http.get_json(movie_search_url, q=inp, apikey=api_key) if results['total'] == 0: return 'no results' diff --git a/plugins/util/hook.py b/plugins/util/hook.py index ff11e82..a20b265 100644 --- a/plugins/util/hook.py +++ b/plugins/util/hook.py @@ -85,6 +85,13 @@ def singlethread(func): return func +def api_key(key): + def annotate(func): + func._apikey = key + return func + return annotate + + def regex(regex, flags=0, **kwargs): args = kwargs diff --git a/plugins/weather.py b/plugins/weather.py index 9eb5860..85c567e 100644 --- a/plugins/weather.py +++ b/plugins/weather.py @@ -3,12 +3,12 @@ from util import hook, http +@hook.api_key('wunderground') @hook.command(autohelp=False) -def weather(inp, nick='', server='', reply=None, db=None, bot=None): +def weather(inp, nick='', server='', reply=None, db=None, api_key=None): ".weather <location> [dontsave] -- gets weather data from Wunderground "\ "http://wunderground.com/weather/api" - api_key = bot.config.get("api_keys", {}).get("wunderground", None) if not api_key: return None diff --git a/plugins/wolframalpha.py b/plugins/wolframalpha.py index ce2812b..e12cdcc 100644 --- a/plugins/wolframalpha.py +++ b/plugins/wolframalpha.py @@ -3,15 +3,12 @@ import re from util import hook, http +@hook.api_key('wolframalpha') @hook.command('wa') @hook.command -def wolframalpha(inp, bot=None): +def wolframalpha(inp, api_key=None): ".wa/.wolframalpha <query> -- computes <query> using Wolfram Alpha" - api_key = bot.config.get("api_keys", {}).get("wolframalpha", None) - if api_key is None: - return "error: no api key set" - url = 'http://api.wolframalpha.com/v2/query?format=plaintext' result = http.get_xml(url, input=inp, appid=api_key) diff --git a/plugins/yahooanswers.py b/plugins/yahooanswers.py index 3705eaa..f6b23ab 100644 --- a/plugins/yahooanswers.py +++ b/plugins/yahooanswers.py @@ -1,21 +1,18 @@ from util import hook, http from random import choice +@hook.api_key('yahoo') @hook.command -def answer(inp, bot=None): +def answer(inp, api_key=None): ".answer <query> -- 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) + appid=api_key) questions = result.get("all", {}).get("questions", []) answered = filter(lambda x: x.get("ChosenAnswer", ""), questions)