From b8d236d5a3dc67ddcc822ca1edc2033c67dd5ac7 Mon Sep 17 00:00:00 2001 From: Emil Hammarstrom Date: Sun, 7 Feb 2016 22:55:43 +0100 Subject: [PATCH 1/7] Added and imgur command, almost a direct port from the not so modular branch. Command is to be expanded, this is a test. --- cogs/general.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/cogs/general.py b/cogs/general.py index 37ca9ddaf..57de95025 100644 --- a/cogs/general.py +++ b/cogs/general.py @@ -6,6 +6,7 @@ import datetime import time import aiohttp import asyncio +from imgurpython import ImgurClient settings = {"POLL_DURATION" : 60} @@ -174,6 +175,24 @@ class General: except: await self.bot.say("Error.") + @commands.command(no_pm=True) + async def imgur(self, *text): + """Retrieves a random imgur picture. + If a link combination e.g. As3DsA4 is provided it will try to retrieve that image.""" + imgurclient = ImgurClient("", "") + if text == (): + rand = randint(0, 59) #60 results per generated page + items = imgurclient.gallery_random(page=0) + await self.bot.say(items[rand].link) + elif text == "viral top": + items = imgurclient.gallery(section='hot', sort='viral', page=0, window='day', show_viral=True) + await self.bot.say(items[0].link) + await self.bot.say(items[1].link) + await self.bot.say(items[2].link) + else: + item = imgurclient.get_image(text) + await self.bot.say(item.link) + @commands.command(pass_context=True, no_pm=True) async def poll(self, ctx, *text): """Starts/stops a poll @@ -188,7 +207,7 @@ class General: return if not self.getPollByChannel(message): p = NewPoll(message, self) - if p.valid: + if p.valid: self.poll_sessions.append(p) await p.start() else: @@ -213,7 +232,7 @@ class General: return False async def check_poll_votes(self, message): - if message.author.id != self.bot.user.id: + if message.author.id != self.bot.user.id: if self.getPollByChannel(message): self.getPollByChannel(message).checkAnswer(message) @@ -228,7 +247,7 @@ class NewPoll(): msg = msg.split(";") if len(msg) < 2: # Needs at least one question and 2 choices self.valid = False - return None + return None else: self.valid = True self.already_voted = [] @@ -274,4 +293,4 @@ class NewPoll(): def setup(bot): n = General(bot) bot.add_listener(n.check_poll_votes, "on_message") - bot.add_cog(n) \ No newline at end of file + bot.add_cog(n) From 65ea2d7d14980ab1b4dee9c17e950dc514df3347 Mon Sep 17 00:00:00 2001 From: Emil Hammarstrom Date: Sun, 7 Feb 2016 23:29:43 +0100 Subject: [PATCH 2/7] Commented work in progress. --- cogs/general.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cogs/general.py b/cogs/general.py index 57de95025..4be5bb4aa 100644 --- a/cogs/general.py +++ b/cogs/general.py @@ -184,14 +184,14 @@ class General: rand = randint(0, 59) #60 results per generated page items = imgurclient.gallery_random(page=0) await self.bot.say(items[rand].link) - elif text == "viral top": - items = imgurclient.gallery(section='hot', sort='viral', page=0, window='day', show_viral=True) - await self.bot.say(items[0].link) - await self.bot.say(items[1].link) - await self.bot.say(items[2].link) - else: - item = imgurclient.get_image(text) - await self.bot.say(item.link) + #elif text == "viral top": + #items = imgurclient.gallery(section='hot', sort='viral', page=0, window='day', show_viral=True) + #await self.bot.say(items[0].link) + #await self.bot.say(items[1].link) + #await self.bot.say(items[2].link) + #else: + #item = imgurclient.get_image(text) + #await self.bot.say(item.link) @commands.command(pass_context=True, no_pm=True) async def poll(self, ctx, *text): From 732fde6ae72c10d01c16399ce6b08dbc35900a5e Mon Sep 17 00:00:00 2001 From: Emil Hammarstrom Date: Mon, 8 Feb 2016 11:13:16 +0100 Subject: [PATCH 3/7] This cog handles image related commands. Currently implemented commands: imgur gif Imgur has various subcommands. Gif is an implementation of the redv1 gif (less code because of new text handling). aiohttp does not collide with any of the imgurpython->ImgurClient dependencies. --- cogs/image.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 cogs/image.py diff --git a/cogs/image.py b/cogs/image.py new file mode 100644 index 000000000..c79e7be69 --- /dev/null +++ b/cogs/image.py @@ -0,0 +1,69 @@ +import discord +from discord.ext import commands +from random import randint +from imgurpython import ImgurClient +import aiohttp + +class Image: + """Image related commands.""" + + def __init__(self, bot): + self.bot = bot + #Reserved for further ... stuff + + """Commands section""" + + @commands.command(no_pm=True) + async def imgur(self, *text): + """Retrieves a random imgur picture. + imgur search [keyword] - retrieves first hit of search query. + imgur [subreddit section] [top or new] - retrieves top 3 hottest or latest pictures of today for given a subreddit section, e.g. 'funny'.""" + imgurclient = ImgurClient("1fd3ef04daf8cab", "f963e574e8e3c17993c933af4f0522e1dc01e230") + if text == (): + rand = randint(0, 59) #60 results per generated page + items = imgurclient.gallery_random(page=0) + await self.bot.say(items[rand].link) + elif text[0] == "search": + items = imgurclient.gallery_search(text[1:len(text)], advanced=None, sort='time', window='all', page=0) + if len(items) < 1: + await self.bot.say("Your search terms gave no results.") + else: + await self.bot.say(items[0].link) + elif text[0] != (): + if text[1] == "top": + imgSort = "top" + elif text[1] == "new": + imgSort = "time" + else: + await self.bot.say("Only top or new is a valid subcommand.") + return + items = imgurclient.subreddit_gallery(text[0], sort=imgSort, window='day', page=0) + if (len(items) < 1): + await self.bot.say("This subreddit section does not exist, try 'funny'") + else: + await self.bot.say("{} {} {}".format(items[0].link, items[1].link, items[2].link)) + + @commands.command(no_pm=True) + async def gif(self, *text): + """ gif [keyword] - retrieves first search result from giphy """ + 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"] != []: + url = result["data"][0]["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)) From 1aa3c6b3b16c3fd094810a2b0c2db562f7f5f7a4 Mon Sep 17 00:00:00 2001 From: Emil Hammarstrom Date: Mon, 8 Feb 2016 11:18:08 +0100 Subject: [PATCH 4/7] Accidently left some imgur code in general.py --- cogs/general.py | 19 ------------------- cogs/image.py | 2 +- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/cogs/general.py b/cogs/general.py index 3a0e4c08d..bbaffb01b 100644 --- a/cogs/general.py +++ b/cogs/general.py @@ -6,7 +6,6 @@ import datetime import time import aiohttp import asyncio -from imgurpython import ImgurClient settings = {"POLL_DURATION" : 60} @@ -175,24 +174,6 @@ class General: except: await self.bot.say("Error.") - @commands.command(no_pm=True) - async def imgur(self, *text): - """Retrieves a random imgur picture. - If a link combination e.g. As3DsA4 is provided it will try to retrieve that image.""" - imgurclient = ImgurClient("", "") - if text == (): - rand = randint(0, 59) #60 results per generated page - items = imgurclient.gallery_random(page=0) - await self.bot.say(items[rand].link) - #elif text == "viral top": - #items = imgurclient.gallery(section='hot', sort='viral', page=0, window='day', show_viral=True) - #await self.bot.say(items[0].link) - #await self.bot.say(items[1].link) - #await self.bot.say(items[2].link) - #else: - #item = imgurclient.get_image(text) - #await self.bot.say(item.link) - @commands.command(pass_context=True, no_pm=True) async def poll(self, ctx, *text): """Starts/stops a poll diff --git a/cogs/image.py b/cogs/image.py index c79e7be69..17821ba22 100644 --- a/cogs/image.py +++ b/cogs/image.py @@ -38,7 +38,7 @@ class Image: await self.bot.say("Only top or new is a valid subcommand.") return items = imgurclient.subreddit_gallery(text[0], sort=imgSort, window='day', page=0) - if (len(items) < 1): + if (len(items) < 3): await self.bot.say("This subreddit section does not exist, try 'funny'") else: await self.bot.say("{} {} {}".format(items[0].link, items[1].link, items[2].link)) From 6ba43bb86a5f873528921e4e5467f16ce8009a72 Mon Sep 17 00:00:00 2001 From: Emil Hammarstrom Date: Mon, 8 Feb 2016 20:49:14 +0100 Subject: [PATCH 5/7] Fixed too many small bugs. --- cogs/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/image.py b/cogs/image.py index 17821ba22..6ddbb695d 100644 --- a/cogs/image.py +++ b/cogs/image.py @@ -24,7 +24,7 @@ class Image: items = imgurclient.gallery_random(page=0) await self.bot.say(items[rand].link) elif text[0] == "search": - items = imgurclient.gallery_search(text[1:len(text)], advanced=None, sort='time', window='all', page=0) + items = imgurclient.gallery_search(" ".join(text[1:len(text)]), advanced=None, sort='time', window='all', page=0) if len(items) < 1: await self.bot.say("Your search terms gave no results.") else: From c3febe07463407b9cdf6828fa940e32c9be1d36a Mon Sep 17 00:00:00 2001 From: Scumm Boy Date: Wed, 10 Feb 2016 13:01:56 -0500 Subject: [PATCH 6/7] 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 7/7] 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))