mirror of
https://github.com/myned/modufur.git
synced 2024-11-01 21:02:38 +00:00
Support multiple attachments/urls, fixed missing args, added rate limit
This commit is contained in:
parent
f658918473
commit
2cf9faf08f
2 changed files with 54 additions and 31 deletions
|
@ -21,6 +21,7 @@ class MsG:
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.LIMIT = 100
|
self.LIMIT = 100
|
||||||
|
self.RATE_LIMIT = 2.1
|
||||||
|
|
||||||
self.favorites = u.setdefault('cogs/favorites.pkl', {})
|
self.favorites = u.setdefault('cogs/favorites.pkl', {})
|
||||||
self.blacklists = u.setdefault(
|
self.blacklists = u.setdefault(
|
||||||
|
@ -30,55 +31,77 @@ class MsG:
|
||||||
# Tag search
|
# Tag search
|
||||||
@commands.command(aliases=['tag', 't'], brief='e621 Tag search', description='e621 | NSFW\nReturn a link search for given tags')
|
@commands.command(aliases=['tag', 't'], brief='e621 Tag search', description='e621 | NSFW\nReturn a link search for given tags')
|
||||||
@checks.del_ctx()
|
@checks.del_ctx()
|
||||||
async def tags(self, ctx, tag):
|
async def tags(self, ctx, tag=None):
|
||||||
tags = []
|
tags = []
|
||||||
|
|
||||||
await ctx.trigger_typing()
|
try:
|
||||||
tag_request = await u.fetch('https://e621.net/tag/related.json', params={'tags': tag, 'type': 'general'}, json=True)
|
if tag is None:
|
||||||
for tag in tag_request.get('wolf', []):
|
raise exc.MissingArgument
|
||||||
tags.append(tag[0])
|
|
||||||
|
|
||||||
await ctx.send('✅ `{}` **related tags:**\n```\n{}```'.format(tag, formatter.tostring(tags)))
|
await ctx.trigger_typing()
|
||||||
|
|
||||||
@tags.error
|
tag_request = await u.fetch('https://e621.net/tag/related.json', params={'tags': tag, 'type': 'general'}, json=True)
|
||||||
async def tags_error(self, ctx, error):
|
for tag in tag_request.get('wolf', []):
|
||||||
if isinstance(error, errext.MissingRequiredArgument):
|
tags.append(tag[0])
|
||||||
return await ctx.send('❌ **No tags given.**', delete_after=10)
|
|
||||||
|
await ctx.send('✅ `{}` **related tags:**\n```\n{}```'.format(tag, formatter.tostring(tags)))
|
||||||
|
|
||||||
|
except exc.MissingArgument:
|
||||||
|
await ctx.send('❌ **No tags given.**', delete_after=10)
|
||||||
|
|
||||||
# Tag aliases
|
# Tag aliases
|
||||||
@commands.command(name='aliases', aliases=['alias', 'a'], brief='e621 Tag aliases', description='e621 | NSFW\nSearch aliases for given tag')
|
@commands.command(name='aliases', aliases=['alias', 'a'], brief='e621 Tag aliases', description='e621 | NSFW\nSearch aliases for given tag')
|
||||||
@checks.del_ctx()
|
@checks.del_ctx()
|
||||||
async def tag_aliases(self, ctx, tag):
|
async def tag_aliases(self, ctx, tag=None):
|
||||||
aliases = []
|
aliases = []
|
||||||
|
|
||||||
await ctx.trigger_typing()
|
try:
|
||||||
alias_request = await u.fetch('https://e621.net/tag_alias/index.json', params={'aliased_to': tag, 'approved': 'true'}, json=True)
|
if tag is None:
|
||||||
for dic in alias_request:
|
raise exc.MissingArgument
|
||||||
aliases.append(dic['name'])
|
|
||||||
|
|
||||||
await ctx.send('✅ `{}` **aliases:**\n```\n{}```'.format(tag, formatter.tostring(aliases)))
|
await ctx.trigger_typing()
|
||||||
|
|
||||||
@tag_aliases.error
|
alias_request = await u.fetch('https://e621.net/tag_alias/index.json', params={'aliased_to': tag, 'approved': 'true'}, json=True)
|
||||||
async def tag_aliases_error(self, ctx, error):
|
for dic in alias_request:
|
||||||
if isinstance(error, errext.MissingRequiredArgument):
|
aliases.append(dic['name'])
|
||||||
return await ctx.send('❌ **No tags given.**', delete_after=10)
|
|
||||||
|
await ctx.send('✅ `{}` **aliases:**\n```\n{}```'.format(tag, formatter.tostring(aliases)))
|
||||||
|
|
||||||
|
except exc.MissingArgument:
|
||||||
|
await ctx.send('❌ **No tags given.**', delete_after=10)
|
||||||
|
|
||||||
# Reverse image searches a linked image using the public iqdb
|
# Reverse image searches a linked image using the public iqdb
|
||||||
@commands.command(name='reverse', aliases=['rev', 'ris'], brief='e621 Reverse image search', description='e621 | NSFW\nReverse-search an image with given URL')
|
@commands.command(name='reverse', aliases=['rev', 'ris'], brief='e621 Reverse image search', description='e621 | NSFW\nReverse-search an image with given URL')
|
||||||
@checks.del_ctx()
|
@checks.del_ctx()
|
||||||
async def reverse_image_search(self, ctx, url=None):
|
async def reverse_image_search(self, ctx, *urls):
|
||||||
try:
|
try:
|
||||||
await ctx.trigger_typing()
|
if not urls and not ctx.message.attachments:
|
||||||
|
|
||||||
if url is None and ctx.message.attachments:
|
|
||||||
url = ctx.message.attachments[0].url
|
|
||||||
elif url is None:
|
|
||||||
raise exc.MissingArgument
|
raise exc.MissingArgument
|
||||||
|
|
||||||
await ctx.send('✅ **Probable match:**\n{}'.format(await scraper.check_match(url)))
|
await ctx.trigger_typing()
|
||||||
|
|
||||||
except exc.MatchError:
|
for url in urls:
|
||||||
await ctx.send('❌ **No probable match.** Make sure the URL directs to an image file.', delete_after=10)
|
try:
|
||||||
|
await ctx.trigger_typing()
|
||||||
|
|
||||||
|
await ctx.send('✅ **Probable match:**\n{}'.format(await scraper.check_match(url)))
|
||||||
|
|
||||||
|
except exc.MatchError as e:
|
||||||
|
await ctx.send('❌ **No probable match for** `{}`**.** Make sure URLs direct to an image file.'.format(e), delete_after=10)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
await asyncio.sleep(self.RATE_LIMIT)
|
||||||
|
for attachment in ctx.message.attachments:
|
||||||
|
try:
|
||||||
|
await ctx.trigger_typing()
|
||||||
|
|
||||||
|
await ctx.send('✅ **Probable match:**\n{}'.format(await scraper.check_match(attachment.url)))
|
||||||
|
|
||||||
|
except exc.MatchError as e:
|
||||||
|
await ctx.send('❌ **No probable match for** `{}`**.** Make sure URLs direct to an image file.'.format(e), delete_after=10)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
await asyncio.sleep(self.RATE_LIMIT)
|
||||||
|
|
||||||
except exc.MissingArgument:
|
except exc.MissingArgument:
|
||||||
await ctx.send('❌ **Invalid url or file.**', delete_after=10)
|
await ctx.send('❌ **Invalid url or file.**', delete_after=10)
|
||||||
|
|
|
@ -11,9 +11,9 @@ async def check_match(url):
|
||||||
try:
|
try:
|
||||||
value = BeautifulSoup(content, 'html.parser').find_all('a')[1].get('href')
|
value = BeautifulSoup(content, 'html.parser').find_all('a')[1].get('href')
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise exc.MatchError
|
raise exc.MatchError(url)
|
||||||
|
|
||||||
if value != '#':
|
if value != '#':
|
||||||
return value
|
return value
|
||||||
else:
|
else:
|
||||||
raise exc.MatchError
|
raise exc.MatchError(url)
|
||||||
|
|
Loading…
Reference in a new issue