2013-12-26 19:54:25 +00:00
|
|
|
import random
|
|
|
|
|
2013-12-26 19:22:15 +00:00
|
|
|
from util import hook, http
|
|
|
|
|
|
|
|
|
|
|
|
@hook.api_key('giphy')
|
|
|
|
@hook.command('gif')
|
|
|
|
@hook.command
|
|
|
|
def giphy(inp, api_key=None):
|
|
|
|
'''.gif/.giphy <query> -- returns first giphy search result'''
|
|
|
|
url = 'http://api.giphy.com/v1/gifs/search'
|
|
|
|
try:
|
2013-12-26 19:54:25 +00:00
|
|
|
response = http.get_json(url, q=inp, limit=10, api_key=api_key)
|
2013-12-26 19:22:15 +00:00
|
|
|
except http.HTTPError as e:
|
|
|
|
return e.msg
|
|
|
|
|
|
|
|
results = response.get('data')
|
|
|
|
if results:
|
2013-12-26 19:54:25 +00:00
|
|
|
return random.choice(results).get('bitly_gif_url')
|
2013-12-26 19:22:15 +00:00
|
|
|
else:
|
|
|
|
return 'no results found'
|