From a36183b1d3e39f9a0847e5d82cbcf8e4481394bf Mon Sep 17 00:00:00 2001 From: Dylan Dizon Date: Sun, 22 Sep 2019 13:39:04 -0400 Subject: [PATCH 1/2] Add limit to similarity of search result --- src/utils/scraper.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/utils/scraper.py b/src/utils/scraper.py index 702bb50..28532e1 100644 --- a/src/utils/scraper.py +++ b/src/utils/scraper.py @@ -27,9 +27,9 @@ async def get_post(url): ident = re.search('#(\\d+)', post['delreason']).group(1) post = await u.fetch('http://e621.net/post/show.json', params={'id': ident}, json=True) source = f'https://e621.net/post/show/{post["id"]}' - similarity = re.search('\\d+', soup.find(string=re.compile('similarity'))).group(0) + '% Match' + similarity = re.search('\\d+', soup.find(string=re.compile('similarity'))).group(0) - return post, source, similarity + return post, source, similarity + '% Match' else: raise IndexError @@ -53,9 +53,10 @@ async def get_post(url): 'artist': [result['data'][artist]], 'score': 'SauceNAO'} source = result['data']['ext_urls'][0] - similarity = re.search('(\\d+)\\.', result['header']['similarity']).group(1) + '% Match' + similarity = re.search('(\\d+)\\.', result['header']['similarity']).group(1) - return post, source, similarity + if int(similarity) >= 55: + return post, source, similarity + '% Match' raise exc.MatchError(re.search('\\/([^\\/]+)$', url).group(1)) From ea2de2c1d7ad8f193ea25fb26d578f1769ae6c5d Mon Sep 17 00:00:00 2001 From: Dylan Dizon Date: Sun, 22 Sep 2019 13:58:56 -0400 Subject: [PATCH 2/2] Change reverse commands to continually type when processing --- src/cogs/booru.py | 159 +++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 81 deletions(-) diff --git a/src/cogs/booru.py b/src/cogs/booru.py index 471932f..bdf50df 100644 --- a/src/cogs/booru.py +++ b/src/cogs/booru.py @@ -397,30 +397,29 @@ class MsG(cmds.Cog): for attachment in ctx.message.attachments: urls.append(attachment.url) - for url in urls: - try: - await ctx.trigger_typing() + async with ctx.channel.typing(): + for url in urls: + try: + post, source, similarity = await scraper.get_post(url) - post, source, similarity = await scraper.get_post(url) + embed = d.Embed( + title=', '.join(post['artist']), + url=source, + color=ctx.me.color if isinstance(ctx.channel, d.TextChannel) else u.color) + embed.set_image(url=post['file_url']) + embed.set_author( + name=similarity, + icon_url=ctx.author.avatar_url) + embed.set_footer( + text=post['score'], + icon_url=self._get_icon(post['score'])) - embed = d.Embed( - title=', '.join(post['artist']), - url=source, - color=ctx.me.color if isinstance(ctx.channel, d.TextChannel) else u.color) - embed.set_image(url=post['file_url']) - embed.set_author( - name=similarity, - icon_url=ctx.author.avatar_url) - embed.set_footer( - text=post['score'], - icon_url=self._get_icon(post['score'])) + await ctx.send(embed=embed) - await ctx.send(embed=embed) + c += 1 - c += 1 - - except exc.MatchError as e: - await ctx.send('**No probable match for:** `{}`'.format(e)) + except exc.MatchError as e: + await ctx.send('**No probable match for:** `{}`'.format(e)) if not c: await u.add_reaction(ctx.message, '\N{CROSS MARK}') @@ -471,43 +470,42 @@ class MsG(cmds.Cog): raise exc.NotFound n = 1 - for message, urls in links.items(): - for url in urls: - try: - await ctx.trigger_typing() + async with ctx.channel.typing(): + for message, urls in links.items(): + for url in urls: + try: + post, source, similarity = await scraper.get_post(url) - post, source, similarity = await scraper.get_post(url) + embed = d.Embed( + title=', '.join(post['artist']), + url=source, + color=ctx.me.color if isinstance(ctx.channel, d.TextChannel) else u.color) + embed.set_image(url=post['file_url']) + embed.set_author( + name=similarity, + icon_url=message.author.avatar_url) + embed.set_footer( + text=post['score'], + icon_url=self._get_icon(post['score'])) - embed = d.Embed( - title=', '.join(post['artist']), - url=source, - color=ctx.me.color if isinstance(ctx.channel, d.TextChannel) else u.color) - embed.set_image(url=post['file_url']) - embed.set_author( - name=similarity, - icon_url=message.author.avatar_url) - embed.set_footer( - text=post['score'], - icon_url=self._get_icon(post['score'])) + await dest.send(embed=embed) + await message.add_reaction('\N{WHITE HEAVY CHECK MARK}') - await dest.send(embed=embed) - await message.add_reaction('\N{WHITE HEAVY CHECK MARK}') + if remove: + with suppress(err.NotFound): + await message.delete() - if remove: - with suppress(err.NotFound): - await message.delete() + except exc.MatchError as e: + await dest.send('`{} / {}` **No probable match for:** `{}`'.format(n, len(links), e)) + await message.add_reaction('\N{CROSS MARK}') + c -= 1 + except exc.SizeError as e: + await dest.send(f'`{e}` **too large.** Maximum is 8 MB') + await message.add_reaction('\N{CROSS MARK}') + c -= 1 - except exc.MatchError as e: - await dest.send('`{} / {}` **No probable match for:** `{}`'.format(n, len(links), e)) - await message.add_reaction('\N{CROSS MARK}') - c -= 1 - except exc.SizeError as e: - await dest.send(f'`{e}` **too large.** Maximum is 8 MB') - await message.add_reaction('\N{CROSS MARK}') - c -= 1 - - finally: - n += 1 + finally: + n += 1 if c <= 0: await u.add_reaction(ctx.message, '\N{CROSS MARK}') @@ -535,40 +533,39 @@ class MsG(cmds.Cog): for attachment in message.attachments: urls.append(attachment.url) - for url in urls: - try: - await message.channel.trigger_typing() + async with message.channel.typing(): + for url in urls: + try: + post, source, similarity = await scraper.get_post(url) - post, source, similarity = await scraper.get_post(url) + embed = d.Embed( + title=', '.join(post['artist']), + url=source, + color=message.channel.guild.me.color if isinstance(message.channel, d.TextChannel) else u.color) + embed.set_image(url=post['file_url']) + embed.set_author( + name=similarity, + icon_url=message.author.avatar_url) + embed.set_footer( + text=post['score'], + icon_url=self._get_icon(post['score'])) - embed = d.Embed( - title=', '.join(post['artist']), - url=source, - color=message.channel.guild.me.color if isinstance(message.channel, d.TextChannel) else u.color) - embed.set_image(url=post['file_url']) - embed.set_author( - name=similarity, - icon_url=message.author.avatar_url) - embed.set_footer( - text=post['score'], - icon_url=self._get_icon(post['score'])) + await message.channel.send(embed=embed) - await message.channel.send(embed=embed) + await message.add_reaction('\N{WHITE HEAVY CHECK MARK}') - await message.add_reaction('\N{WHITE HEAVY CHECK MARK}') + with suppress(err.NotFound): + await message.delete() - with suppress(err.NotFound): - await message.delete() - - except exc.MatchError as e: - await message.channel.send('**No probable match for:** `{}`'.format(e)) - await message.add_reaction('\N{CROSS MARK}') - except exc.SizeError as e: - await message.channel.send(f'`{e}` **too large.** Maximum is 8 MB') - await message.add_reaction('\N{CROSS MARK}') - except Exception: - await message.channel.send(f'**An unknown error occurred.**') - await message.add_reaction('\N{WARNING SIGN}') + except exc.MatchError as e: + await message.channel.send('**No probable match for:** `{}`'.format(e)) + await message.add_reaction('\N{CROSS MARK}') + except exc.SizeError as e: + await message.channel.send(f'`{e}` **too large.** Maximum is 8 MB') + await message.add_reaction('\N{CROSS MARK}') + except Exception: + await message.channel.send(f'**An unknown error occurred.**') + await message.add_reaction('\N{WARNING SIGN}') print('STOPPED : reversifying')