mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-07 11:48:55 -05:00
[Image] Complete rewrite: no longer blocking
This commit is contained in:
parent
b5398aeacb
commit
3d9a157516
222
cogs/image.py
222
cogs/image.py
@ -1,111 +1,153 @@
|
|||||||
import discord
|
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from random import randint
|
from random import choice
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import random
|
import functools
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
try:
|
||||||
|
from imgurpython import ImgurClient
|
||||||
|
except:
|
||||||
|
ImgurClient = False
|
||||||
|
|
||||||
|
CLIENT_ID = "1fd3ef04daf8cab"
|
||||||
|
CLIENT_SECRET = "f963e574e8e3c17993c933af4f0522e1dc01e230"
|
||||||
|
GIPHY_API_KEY = "dc6zaTOxFJmzC"
|
||||||
|
|
||||||
|
|
||||||
class Image:
|
class Image:
|
||||||
"""Image related commands."""
|
"""Image related commands."""
|
||||||
|
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
#Reserved for further ... stuff
|
self.imgur = ImgurClient(CLIENT_ID, CLIENT_SECRET)
|
||||||
|
|
||||||
"""Commands section"""
|
@commands.group(name="imgur", no_pm=True, pass_context=True)
|
||||||
|
async def _imgur(self, ctx):
|
||||||
|
"""Retrieves pictures from imgur"""
|
||||||
|
if ctx.invoked_subcommand is None:
|
||||||
|
await self.bot.send_cmd_help(ctx)
|
||||||
|
|
||||||
@commands.command(no_pm=True)
|
@_imgur.command(pass_context=True, name="random")
|
||||||
async def imgur(self, *text):
|
async def imgur_random(self, ctx):
|
||||||
"""Retrieves a picture from imgur
|
"""Retrieves a random image from Imgur"""
|
||||||
|
task = functools.partial(self.imgur.gallery_random, page=0)
|
||||||
|
task = self.bot.loop.run_in_executor(None, task)
|
||||||
|
|
||||||
imgur search [keyword] - Retrieves first hit of search query.
|
try:
|
||||||
imgur [subreddit section] [top or new] - Retrieves top 3 hottest or latest pictures of today for given a subreddit section, e.g. 'funny'."""
|
results = await asyncio.wait_for(task, timeout=10)
|
||||||
imgurclient = ImgurClient("1fd3ef04daf8cab", "f963e574e8e3c17993c933af4f0522e1dc01e230")
|
except asyncio.TimeoutError:
|
||||||
if text == ():
|
await self.bot.say("Error: request timed out")
|
||||||
rand = randint(0, 59) #60 results per generated page
|
else:
|
||||||
items = imgurclient.gallery_random(page=0)
|
item = choice(results)
|
||||||
await self.bot.say(items[rand].link)
|
link = item.gifv if hasattr(item, "gifv") else item.link
|
||||||
elif text[0] == "search":
|
await self.bot.say(link)
|
||||||
items = imgurclient.gallery_search(" ".join(text[1:len(text)]), advanced=None, sort='time', window='all', page=0)
|
|
||||||
if len(items) < 1:
|
@_imgur.command(pass_context=True, name="search")
|
||||||
|
async def imgur_search(self, ctx, *, term: str):
|
||||||
|
"""Searches Imgur for the specified term"""
|
||||||
|
task = functools.partial(self.imgur.gallery_search, term,
|
||||||
|
advanced=None, sort='time',
|
||||||
|
window='all', page=0)
|
||||||
|
task = self.bot.loop.run_in_executor(None, task)
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = await asyncio.wait_for(task, timeout=10)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
await self.bot.say("Error: request timed out")
|
||||||
|
else:
|
||||||
|
if result:
|
||||||
|
await self.bot.say(result[0].link)
|
||||||
|
else:
|
||||||
await self.bot.say("Your search terms gave no results.")
|
await self.bot.say("Your search terms gave no results.")
|
||||||
else:
|
|
||||||
await self.bot.say(items[0].link)
|
|
||||||
elif text[0] != ():
|
|
||||||
try:
|
|
||||||
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) < 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))
|
|
||||||
except:
|
|
||||||
await self.bot.say("Type help imgur for details.")
|
|
||||||
|
|
||||||
@commands.command(no_pm=True)
|
@_imgur.command(pass_context=True, name="subreddit")
|
||||||
async def gif(self, *text):
|
async def imgur_subreddit(self, ctx, subreddit: str, sort_type: str="top", window: str="day"):
|
||||||
"""Retrieves first search result from giphy
|
"""Gets images from the specified subreddit section
|
||||||
|
|
||||||
gif [keyword]"""
|
Sort types: new, top
|
||||||
if len(text) > 0:
|
Time windows: day, week, month, year, all"""
|
||||||
if len(text[0]) > 1 and len(text[0]) < 20:
|
sort_type = sort_type.lower()
|
||||||
try:
|
|
||||||
msg = "+".join(text)
|
if sort_type not in ("new", "top"):
|
||||||
search = "http://api.giphy.com/v1/gifs/search?q=" + msg + "&api_key=dc6zaTOxFJmzC"
|
await self.bot.say("Only 'new' and 'top' are a valid sort type.")
|
||||||
async with aiohttp.get(search) as r:
|
return
|
||||||
result = await r.json()
|
elif window not in ("day", "week", "month", "year", "all"):
|
||||||
if result["data"] != []:
|
await self.bot.send_cmd_help(ctx)
|
||||||
url = result["data"][0]["url"]
|
return
|
||||||
await self.bot.say(url)
|
|
||||||
else:
|
if sort_type == "new":
|
||||||
await self.bot.say("Your search terms gave no results.")
|
sort = "time"
|
||||||
except:
|
elif sort_type == "top":
|
||||||
await self.bot.say("Error.")
|
sort = "top"
|
||||||
else:
|
|
||||||
await self.bot.say("Invalid search.")
|
links = []
|
||||||
|
|
||||||
|
task = functools.partial(self.imgur.subreddit_gallery, subreddit,
|
||||||
|
sort=sort, window=window, page=0)
|
||||||
|
task = self.bot.loop.run_in_executor(None, task)
|
||||||
|
try:
|
||||||
|
items = await asyncio.wait_for(task, timeout=10)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
await self.bot.say("Error: request timed out")
|
||||||
|
return
|
||||||
|
|
||||||
|
for item in items[:3]:
|
||||||
|
link = item.gifv if hasattr(item, "gifv") else item.link
|
||||||
|
links.append("{}\n{}".format(item.title, link))
|
||||||
|
|
||||||
|
if links:
|
||||||
|
await self.bot.say("\n".join(links))
|
||||||
else:
|
else:
|
||||||
await self.bot.say("gif [text]")
|
await self.bot.say("No results found.")
|
||||||
|
|
||||||
@commands.command(no_pm=True)
|
@commands.command(pass_context=True, no_pm=True)
|
||||||
async def gifr(self, *text):
|
async def gif(self, ctx, *keywords):
|
||||||
"""Retrieves a random gif from a giphy search
|
"""Retrieves first search result from giphy"""
|
||||||
|
if keywords:
|
||||||
gifr [keyword]"""
|
keywords = "+".join(keywords)
|
||||||
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/random?&api_key=dc6zaTOxFJmzC&tag=" + msg
|
|
||||||
async with aiohttp.get(search) as r:
|
|
||||||
result = await r.json()
|
|
||||||
if result["data"] != []:
|
|
||||||
url = result["data"]["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:
|
else:
|
||||||
await self.bot.say("gifr [text]")
|
await self.bot.send_cmd_help(ctx)
|
||||||
|
return
|
||||||
|
|
||||||
|
url = ("http://api.giphy.com/v1/gifs/search?&api_key={}&q={}"
|
||||||
|
"".format(GIPHY_API_KEY, keywords))
|
||||||
|
|
||||||
|
async with aiohttp.get(url) as r:
|
||||||
|
result = await r.json()
|
||||||
|
if r.status == 200:
|
||||||
|
if result["data"]:
|
||||||
|
await self.bot.say(result["data"][0]["url"])
|
||||||
|
else:
|
||||||
|
await self.bot.say("No results found.")
|
||||||
|
else:
|
||||||
|
await self.bot.say("Error contacting the API")
|
||||||
|
|
||||||
|
@commands.command(pass_context=True, no_pm=True)
|
||||||
|
async def gifr(self, ctx, *keywords):
|
||||||
|
"""Retrieves a random gif from a giphy search"""
|
||||||
|
if keywords:
|
||||||
|
keywords = "+".join(keywords)
|
||||||
|
else:
|
||||||
|
await self.bot.send_cmd_help(ctx)
|
||||||
|
return
|
||||||
|
|
||||||
|
url = ("http://api.giphy.com/v1/gifs/random?&api_key={}&tag={}"
|
||||||
|
"".format(GIPHY_API_KEY, keywords))
|
||||||
|
|
||||||
|
async with aiohttp.get(url) as r:
|
||||||
|
result = await r.json()
|
||||||
|
if r.status == 200:
|
||||||
|
if result["data"]:
|
||||||
|
await self.bot.say(result["data"]["url"])
|
||||||
|
else:
|
||||||
|
await self.bot.say("No results found.")
|
||||||
|
else:
|
||||||
|
await self.bot.say("Error contacting the API")
|
||||||
|
|
||||||
class ModuleNotFound(Exception):
|
|
||||||
def __init__(self, m):
|
|
||||||
self.message = m
|
|
||||||
def __str__(self):
|
|
||||||
return self.message
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
global ImgurClient
|
if ImgurClient is False:
|
||||||
try:
|
raise RuntimeError("You need the imgurpython module to use this.\n"
|
||||||
from imgurpython import ImgurClient
|
"pip3 install imgurpython")
|
||||||
except:
|
|
||||||
raise ModuleNotFound("imgurpython is not installed. Do 'pip3 install imgurpython' to use this cog.")
|
|
||||||
bot.add_cog(Image(bot))
|
bot.add_cog(Image(bot))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user