From db05fadc298875dda0bbfce1f06760adf8442c49 Mon Sep 17 00:00:00 2001 From: bwreilly Date: Thu, 26 Dec 2013 11:22:15 -0800 Subject: [PATCH 1/2] add in giphy (.gif query) --- plugins/gif.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 plugins/gif.py diff --git a/plugins/gif.py b/plugins/gif.py new file mode 100644 index 0000000..22dca89 --- /dev/null +++ b/plugins/gif.py @@ -0,0 +1,19 @@ +from util import hook, http + + +@hook.api_key('giphy') +@hook.command('gif') +@hook.command +def giphy(inp, api_key=None): + '''.gif/.giphy -- returns first giphy search result''' + url = 'http://api.giphy.com/v1/gifs/search' + try: + response = http.get_json(url, q=inp, limit=1, api_key=api_key) + except http.HTTPError as e: + return e.msg + + results = response.get('data') + if results: + return results[0].get('bitly_gif_url') + else: + return 'no results found' From 0ee7ea1ac46571b3db2c17b926dc72ab71c7ef38 Mon Sep 17 00:00:00 2001 From: bwreilly Date: Thu, 26 Dec 2013 11:54:25 -0800 Subject: [PATCH 2/2] random choice of first ten results, consistency with gis --- plugins/gif.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/gif.py b/plugins/gif.py index 22dca89..d30537e 100644 --- a/plugins/gif.py +++ b/plugins/gif.py @@ -1,3 +1,5 @@ +import random + from util import hook, http @@ -8,12 +10,12 @@ def giphy(inp, api_key=None): '''.gif/.giphy -- returns first giphy search result''' url = 'http://api.giphy.com/v1/gifs/search' try: - response = http.get_json(url, q=inp, limit=1, api_key=api_key) + response = http.get_json(url, q=inp, limit=10, api_key=api_key) except http.HTTPError as e: return e.msg results = response.get('data') if results: - return results[0].get('bitly_gif_url') + return random.choice(results).get('bitly_gif_url') else: return 'no results found'