refactor apikey retrieval, fix lastfm with empty input

This commit is contained in:
Ryan Hitchman 2013-01-27 16:40:31 -06:00
parent 92c6e991e7
commit a29dffbfb7
7 changed files with 28 additions and 28 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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 <title> -- 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'

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)