[Image] [p]imgur search: Return up to 3 results

This commit is contained in:
Twentysix 2017-04-07 23:29:02 +02:00
parent cb4c91d86f
commit 55f6307b7f

View File

@ -1,5 +1,5 @@
from discord.ext import commands from discord.ext import commands
from random import choice from random import choice, shuffle
import aiohttp import aiohttp
import functools import functools
import asyncio import asyncio
@ -54,19 +54,24 @@ class Image:
@_imgur.command(pass_context=True, name="search") @_imgur.command(pass_context=True, name="search")
async def imgur_search(self, ctx, *, term: str): async def imgur_search(self, ctx, *, term: str):
"""Searches Imgur for the specified term""" """Searches Imgur for the specified term and returns up to 3 results"""
task = functools.partial(self.imgur.gallery_search, term, task = functools.partial(self.imgur.gallery_search, term,
advanced=None, sort='time', advanced=None, sort='time',
window='all', page=0) window='all', page=0)
task = self.bot.loop.run_in_executor(None, task) task = self.bot.loop.run_in_executor(None, task)
try: try:
result = await asyncio.wait_for(task, timeout=10) results = await asyncio.wait_for(task, timeout=10)
except asyncio.TimeoutError: except asyncio.TimeoutError:
await self.bot.say("Error: request timed out") await self.bot.say("Error: request timed out")
else: else:
if result: if results:
await self.bot.say(result[0].link) shuffle(results)
msg = "Search results...\n"
for r in results[:3]:
msg += r.gifv if hasattr(r, "gifv") else r.link
msg += "\n"
await self.bot.say(msg)
else: else:
await self.bot.say("Your search terms gave no results.") await self.bot.say("Your search terms gave no results.")