From c3febe07463407b9cdf6828fa940e32c9be1d36a Mon Sep 17 00:00:00 2001 From: Scumm Boy Date: Wed, 10 Feb 2016 13:01:56 -0500 Subject: [PATCH 1/2] Randomize !gif results Let the bot pick a random result out of the returned giphy array. Make sure to not choose a number higher than the length of the array. --- cogs/image.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cogs/image.py b/cogs/image.py index 6ddbb695d..ce91e041c 100644 --- a/cogs/image.py +++ b/cogs/image.py @@ -3,6 +3,7 @@ from discord.ext import commands from random import randint from imgurpython import ImgurClient import aiohttp +import random class Image: """Image related commands.""" @@ -45,6 +46,7 @@ class Image: @commands.command(no_pm=True) async def gif(self, *text): + random.seed() """ gif [keyword] - retrieves first search result from giphy """ if len(text) > 0: if len(text[0]) > 1 and len(text[0]) < 20: @@ -54,7 +56,8 @@ class Image: async with aiohttp.get(search) as r: result = await r.json() if result["data"] != []: - url = result["data"][0]["url"] + maxarray = len(result) + url = result["data"][random.randint(0,maxarray)]["url"] await self.bot.say(url) else: await self.bot.say("Your search terms gave no results.") From 15601fb9a68c889e0b4a97468e9f7f65c3b61932 Mon Sep 17 00:00:00 2001 From: Scumm Boy Date: Wed, 10 Feb 2016 14:04:22 -0500 Subject: [PATCH 2/2] Reverted changes to !gif, created !gifr Instead of modifying !gif, I have moved the changes into !gifr for accessing random images within giphy's search results. --- cogs/image.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/cogs/image.py b/cogs/image.py index ce91e041c..b2664e32f 100644 --- a/cogs/image.py +++ b/cogs/image.py @@ -46,7 +46,6 @@ class Image: @commands.command(no_pm=True) async def gif(self, *text): - random.seed() """ gif [keyword] - retrieves first search result from giphy """ if len(text) > 0: if len(text[0]) > 1 and len(text[0]) < 20: @@ -56,8 +55,7 @@ class Image: async with aiohttp.get(search) as r: result = await r.json() if result["data"] != []: - maxarray = len(result) - url = result["data"][random.randint(0,maxarray)]["url"] + url = result["data"][0]["url"] await self.bot.say(url) else: await self.bot.say("Your search terms gave no results.") @@ -68,5 +66,29 @@ class Image: else: await self.bot.say("gif [text]") + @commands.command(no_pm=True) + async def gifr(self, *text): + """ gifr [keyword] - retrieves a random gif from a giphy search """ + random.seed() + if len(text) > 0: + if len(text[0]) > 1 and len(text[0]) < 20: + try: + msg = "+".join(text) + search = "http://api.giphy.com/v1/gifs/search?q=" + msg + "&api_key=dc6zaTOxFJmzC" + async with aiohttp.get(search) as r: + result = await r.json() + if result["data"] != []: + maxarray = len(result) + url = result["data"][random.randint(0,maxarray)]["url"] + await self.bot.say(url) + else: + await self.bot.say("Your search terms gave no results.") + except: + await self.bot.say("Error.") + else: + await self.bot.say("Invalid search.") + else: + await self.bot.say("gif [text]") + def setup(bot): bot.add_cog(Image(bot))