refactor apikey retrieval, fix lastfm with empty input
This commit is contained in:
parent
92c6e991e7
commit
a29dffbfb7
|
@ -125,6 +125,13 @@ def dispatch(input, kind, func, args, autohelp=False):
|
||||||
input.reply(func.__doc__)
|
input.reply(func.__doc__)
|
||||||
return
|
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:
|
if func._thread:
|
||||||
bot.threads[func].put(input)
|
bot.threads[func].put(input)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -7,13 +7,9 @@ from util import hook, http
|
||||||
|
|
||||||
api_url = "http://ws.audioscrobbler.com/2.0/?format=json"
|
api_url = "http://ws.audioscrobbler.com/2.0/?format=json"
|
||||||
|
|
||||||
|
@hook.api_key('lastfm')
|
||||||
@hook.command
|
@hook.command(autohelp=False)
|
||||||
def lastfm(inp, nick='', say=None, bot=None):
|
def lastfm(inp, nick='', say=None, api_key=None):
|
||||||
api_key = bot.config.get("api_keys", {}).get("lastfm", None)
|
|
||||||
if not api_key:
|
|
||||||
return None
|
|
||||||
|
|
||||||
if inp:
|
if inp:
|
||||||
user = inp
|
user = inp
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -5,17 +5,13 @@ movie_search_url = api_root + 'movies.json'
|
||||||
movie_reviews_url = api_root + 'movies/%s/reviews.json'
|
movie_reviews_url = api_root + 'movies/%s/reviews.json'
|
||||||
|
|
||||||
|
|
||||||
|
@hook.api_key('rottentomatoes')
|
||||||
@hook.command('rt')
|
@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'
|
'.rt <title> -- gets ratings for <title> from Rotten Tomatoes'
|
||||||
|
|
||||||
api_key = bot.config.get("api_keys", {}).get("rottentomatoes", None)
|
results = http.get_json(movie_search_url, q=inp, apikey=api_key)
|
||||||
if not api_key:
|
|
||||||
return
|
|
||||||
|
|
||||||
title = inp.strip()
|
|
||||||
|
|
||||||
results = http.get_json(movie_search_url, q=title, apikey=api_key)
|
|
||||||
if results['total'] == 0:
|
if results['total'] == 0:
|
||||||
return 'no results'
|
return 'no results'
|
||||||
|
|
||||||
|
|
|
@ -85,6 +85,13 @@ def singlethread(func):
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
|
def api_key(key):
|
||||||
|
def annotate(func):
|
||||||
|
func._apikey = key
|
||||||
|
return func
|
||||||
|
return annotate
|
||||||
|
|
||||||
|
|
||||||
def regex(regex, flags=0, **kwargs):
|
def regex(regex, flags=0, **kwargs):
|
||||||
args = kwargs
|
args = kwargs
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,12 @@
|
||||||
from util import hook, http
|
from util import hook, http
|
||||||
|
|
||||||
|
|
||||||
|
@hook.api_key('wunderground')
|
||||||
@hook.command(autohelp=False)
|
@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 "\
|
".weather <location> [dontsave] -- gets weather data from Wunderground "\
|
||||||
"http://wunderground.com/weather/api"
|
"http://wunderground.com/weather/api"
|
||||||
|
|
||||||
api_key = bot.config.get("api_keys", {}).get("wunderground", None)
|
|
||||||
if not api_key:
|
if not api_key:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
@ -3,15 +3,12 @@ import re
|
||||||
from util import hook, http
|
from util import hook, http
|
||||||
|
|
||||||
|
|
||||||
|
@hook.api_key('wolframalpha')
|
||||||
@hook.command('wa')
|
@hook.command('wa')
|
||||||
@hook.command
|
@hook.command
|
||||||
def wolframalpha(inp, bot=None):
|
def wolframalpha(inp, api_key=None):
|
||||||
".wa/.wolframalpha <query> -- computes <query> using Wolfram Alpha"
|
".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'
|
url = 'http://api.wolframalpha.com/v2/query?format=plaintext'
|
||||||
|
|
||||||
result = http.get_xml(url, input=inp, appid=api_key)
|
result = http.get_xml(url, input=inp, appid=api_key)
|
||||||
|
|
|
@ -1,21 +1,18 @@
|
||||||
from util import hook, http
|
from util import hook, http
|
||||||
from random import choice
|
from random import choice
|
||||||
|
|
||||||
|
@hook.api_key('yahoo')
|
||||||
@hook.command
|
@hook.command
|
||||||
def answer(inp, bot=None):
|
def answer(inp, api_key=None):
|
||||||
".answer <query> -- find the answer to a question on Yahoo! Answers"
|
".answer <query> -- find the answer to a question on Yahoo! Answers"
|
||||||
|
|
||||||
url = "http://answers.yahooapis.com/AnswersService/V1/questionSearch"
|
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,
|
result = http.get_json(url,
|
||||||
query=inp,
|
query=inp,
|
||||||
search_in="question",
|
search_in="question",
|
||||||
output="json",
|
output="json",
|
||||||
appid=app_id)
|
appid=api_key)
|
||||||
|
|
||||||
questions = result.get("all", {}).get("questions", [])
|
questions = result.get("all", {}).get("questions", [])
|
||||||
answered = filter(lambda x: x.get("ChosenAnswer", ""), questions)
|
answered = filter(lambda x: x.get("ChosenAnswer", ""), questions)
|
||||||
|
|
Loading…
Reference in New Issue