1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-11-01 21:02:38 +00:00

Add support for SauceNAO as fallback reverse image search engine

This commit is contained in:
Myned 2019-09-17 02:59:35 -04:00
parent e3b73a6237
commit 17d771060e
3 changed files with 80 additions and 46 deletions

View file

@ -106,8 +106,10 @@ class MsG(cmds.Cog):
return args return args
def _get_score(self, score): def _get_icon(self, score):
if score < 0: if score is 'SauceNAO':
return 'https://d2.alternativeto.net/dist/icons/saucenao_23437.png?width=64&height=64&mode=crop&upscale=false'
elif score < 0:
return 'https://emojipedia-us.s3.amazonaws.com/thumbs/320/twitter/103/pouting-face_1f621.png' return 'https://emojipedia-us.s3.amazonaws.com/thumbs/320/twitter/103/pouting-face_1f621.png'
elif score == 0: elif score == 0:
return 'https://emojipedia-us.s3.amazonaws.com/thumbs/320/mozilla/36/pile-of-poo_1f4a9.png' return 'https://emojipedia-us.s3.amazonaws.com/thumbs/320/mozilla/36/pile-of-poo_1f4a9.png'
@ -301,7 +303,7 @@ class MsG(cmds.Cog):
embed.set_author(name=f'{post["width"]} x {post["height"]}', embed.set_author(name=f'{post["width"]} x {post["height"]}',
url=f'https://e621.net/post?tags=ratio:{post["width"]/post["height"]:.2f}', icon_url=ctx.author.avatar_url) url=f'https://e621.net/post?tags=ratio:{post["width"]/post["height"]:.2f}', icon_url=ctx.author.avatar_url)
embed.set_footer(text=post['score'], embed.set_footer(text=post['score'],
icon_url=self._get_score(post['score'])) icon_url=self._get_icon(post['score']))
except exc.MissingArgument: except exc.MissingArgument:
await ctx.send('\N{CROSS MARK} **Invalid url**') await ctx.send('\N{CROSS MARK} **Invalid url**')
@ -399,17 +401,21 @@ class MsG(cmds.Cog):
try: try:
await ctx.trigger_typing() await ctx.trigger_typing()
post = await scraper.get_post(url) post, source, similarity = await scraper.get_post(url)
embed = d.Embed( embed = d.Embed(
title=', '.join(post['artist']), url=f'https://e621.net/post/show/{post["id"]}', color=ctx.me.color if isinstance(ctx.channel, d.TextChannel) else u.color) 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_image(url=post['file_url'])
embed.set_author(name=f'{post["width"]} x {post["height"]}', embed.set_author(
url=f'https://e621.net/post?tags=ratio:{post["width"]/post["height"]:.2f}', icon_url=ctx.author.avatar_url) name=similarity,
embed.set_footer(text=post['score'], icon_url=ctx.author.avatar_url)
icon_url=self._get_score(post['score'])) embed.set_footer(
text=post['score'],
icon_url=self._get_icon(post['score']))
await ctx.send('**Probable match**', embed=embed) await ctx.send(embed=embed)
c += 1 c += 1
@ -470,17 +476,21 @@ class MsG(cmds.Cog):
try: try:
await ctx.trigger_typing() await ctx.trigger_typing()
post = await scraper.get_post(url) post, source, similarity = await scraper.get_post(url)
embed = d.Embed( embed = d.Embed(
title=', '.join(post['artist']), url=f'https://e621.net/post/show/{post["id"]}', color=ctx.me.color if isinstance(ctx.channel, d.TextChannel) else u.color) 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_image(url=post['file_url'])
embed.set_author(name=f'{post["width"]} x {post["height"]}', embed.set_author(
url=f'https://e621.net/post?tags=ratio:{post["width"]/post["height"]:.2f}', icon_url=ctx.author.avatar_url) name=similarity,
icon_url=message.author.avatar_url)
embed.set_footer( embed.set_footer(
text=post['score'], icon_url=self._get_score(post['score'])) text=post['score'],
icon_url=self._get_icon(post['score']))
await dest.send(f'**Probable match from** {message.author.display_name}', embed=embed) await dest.send(embed=embed)
await message.add_reaction('\N{WHITE HEAVY CHECK MARK}') await message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
if remove: if remove:
@ -529,17 +539,21 @@ class MsG(cmds.Cog):
try: try:
await message.channel.trigger_typing() await message.channel.trigger_typing()
post = await scraper.get_post(url) post, source, similarity = await scraper.get_post(url)
embed = d.Embed( embed = d.Embed(
title=', '.join(post['artist']), url=f'https://e621.net/post/show/{post["id"]}', color=message.channel.guild.me.color if isinstance(message.channel, d.TextChannel) else u.color) 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_image(url=post['file_url'])
embed.set_author(name=f'{post["width"]} x {post["height"]}', embed.set_author(
url=f'https://e621.net/post?tags=ratio:{post["width"]/post["height"]:.2f}', icon_url=message.author.avatar_url) name=similarity,
embed.set_footer(text=post['score'], icon_url=message.author.avatar_url)
icon_url=self._get_score(post['score'])) embed.set_footer(
text=post['score'],
icon_url=self._get_icon(post['score']))
await message.channel.send('**Probable match from** {}'.format(message.author.display_name), 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}')
@ -768,7 +782,7 @@ class MsG(cmds.Cog):
embed.set_author(name=pool['name'], embed.set_author(name=pool['name'],
url='https://e621.net/pool/show?id={}'.format(pool['id']), icon_url=ctx.author.avatar_url) url='https://e621.net/pool/show?id={}'.format(pool['id']), icon_url=ctx.author.avatar_url)
embed.set_footer(text='{} / {}'.format(c, len(posts)), embed.set_footer(text='{} / {}'.format(c, len(posts)),
icon_url=self._get_score(values[c - 1]['score'])) icon_url=self._get_icon(values[c - 1]['score']))
paginator = await ctx.send(embed=embed) paginator = await ctx.send(embed=embed)
@ -799,7 +813,7 @@ class MsG(cmds.Cog):
embed.url = 'https://e621.net/post/show/{}'.format( embed.url = 'https://e621.net/post/show/{}'.format(
keys[c - 1]) keys[c - 1])
embed.set_footer(text='{} / {}'.format(c, len(posts)), embed.set_footer(text='{} / {}'.format(c, len(posts)),
icon_url=self._get_score(values[c - 1]['score'])) icon_url=self._get_icon(values[c - 1]['score']))
embed.set_image(url=values[c - 1]['file_url']) embed.set_image(url=values[c - 1]['file_url'])
await paginator.edit(content='\N{HEAVY BLACK HEART}' if keys[c - 1] in hearted.keys() else None, embed=embed) await paginator.edit(content='\N{HEAVY BLACK HEART}' if keys[c - 1] in hearted.keys() else None, embed=embed)
@ -817,7 +831,7 @@ class MsG(cmds.Cog):
embed.url = 'https://e621.net/post/show/{}'.format( embed.url = 'https://e621.net/post/show/{}'.format(
keys[c - 1]) keys[c - 1])
embed.set_footer(text='{} / {}'.format(c, len(posts)), embed.set_footer(text='{} / {}'.format(c, len(posts)),
icon_url=self._get_score(values[c - 1]['score'])) icon_url=self._get_icon(values[c - 1]['score']))
embed.set_image(url=values[c - 1]['file_url']) embed.set_image(url=values[c - 1]['file_url'])
if ctx.channel is d.TextChannel: if ctx.channel is d.TextChannel:
@ -833,7 +847,7 @@ class MsG(cmds.Cog):
embed.url = 'https://e621.net/post/show/{}'.format( embed.url = 'https://e621.net/post/show/{}'.format(
keys[c - 1]) keys[c - 1])
embed.set_footer(text='{} / {}'.format(c, len(posts)), embed.set_footer(text='{} / {}'.format(c, len(posts)),
icon_url=self._get_score(values[c - 1]['score'])) icon_url=self._get_icon(values[c - 1]['score']))
embed.set_image(url=values[c - 1]['file_url']) embed.set_image(url=values[c - 1]['file_url'])
await paginator.edit(content='\N{HEAVY BLACK HEART}' if keys[c - 1] in hearted.keys() else None, embed=embed) await paginator.edit(content='\N{HEAVY BLACK HEART}' if keys[c - 1] in hearted.keys() else None, embed=embed)
@ -904,7 +918,7 @@ class MsG(cmds.Cog):
embed.set_author(name=' '.join(tags) if tags else order, embed.set_author(name=' '.join(tags) if tags else order,
url='https://{}.net/post?tags={}'.format(booru, ','.join(tags)), icon_url=ctx.author.avatar_url) url='https://{}.net/post?tags={}'.format(booru, ','.join(tags)), icon_url=ctx.author.avatar_url)
embed.set_footer(text=values[c - 1]['score'], embed.set_footer(text=values[c - 1]['score'],
icon_url=self._get_score(values[c - 1]['score'])) icon_url=self._get_icon(values[c - 1]['score']))
paginator = await ctx.send(embed=embed) paginator = await ctx.send(embed=embed)
@ -936,7 +950,7 @@ class MsG(cmds.Cog):
booru, booru,
keys[c - 1]) keys[c - 1])
embed.set_footer(text=values[c - 1]['score'], embed.set_footer(text=values[c - 1]['score'],
icon_url=self._get_score(values[c - 1]['score'])) icon_url=self._get_icon(values[c - 1]['score']))
embed.set_image(url=values[c - 1]['file_url']) embed.set_image(url=values[c - 1]['file_url'])
await paginator.edit(content='\N{HEAVY BLACK HEART}' if keys[c - 1] in hearted.keys() else None, embed=embed) await paginator.edit(content='\N{HEAVY BLACK HEART}' if keys[c - 1] in hearted.keys() else None, embed=embed)
@ -960,7 +974,7 @@ class MsG(cmds.Cog):
booru, booru,
keys[c - 1]) keys[c - 1])
embed.set_footer(text=values[c - 1]['score'], embed.set_footer(text=values[c - 1]['score'],
icon_url=self._get_score(values[c - 1]['score'])) icon_url=self._get_icon(values[c - 1]['score']))
embed.set_image(url=values[c - 1]['file_url']) embed.set_image(url=values[c - 1]['file_url'])
await paginator.edit(content='\N{HEAVY BLACK HEART}' if keys[c - 1] in hearted.keys() else None, embed=embed) await paginator.edit(content='\N{HEAVY BLACK HEART}' if keys[c - 1] in hearted.keys() else None, embed=embed)
@ -1035,7 +1049,7 @@ class MsG(cmds.Cog):
embed.set_author(name=' '.join(tags) if tags else order, embed.set_author(name=' '.join(tags) if tags else order,
url='https://{}.net/post?tags={}'.format(booru, ','.join(tags)), icon_url=ctx.author.avatar_url) url='https://{}.net/post?tags={}'.format(booru, ','.join(tags)), icon_url=ctx.author.avatar_url)
embed.set_footer( embed.set_footer(
text=post['score'], icon_url=self._get_score(post['score'])) text=post['score'], icon_url=self._get_icon(post['score']))
message = await ctx.send(embed=embed) message = await ctx.send(embed=embed)

View file

@ -16,30 +16,50 @@ async def get_post(url):
if filesize > 8192 * 1024: if filesize > 8192 * 1024:
raise exc.SizeError(size(filesize, system=alternative)) raise exc.SizeError(size(filesize, system=alternative))
except (ValueError, KeyError):
raise exc.MissingArgument
content = await u.fetch('http://iqdb.harry.lu', params={'url': url}) content = await u.fetch('http://iqdb.harry.lu', params={'url': url})
soup = BeautifulSoup(content, 'html.parser')
source = soup.find_all('a', limit=2)[1].get('href')
try: if source != '#':
value = BeautifulSoup(content, 'html.parser').find_all('a')[1].get('href') ident = re.search('show/([0-9]+)', source).group(1)
if value != '#':
ident = re.search('show/([0-9]+)', value).group(1)
post = await u.fetch('http://e621.net/post/show.json', params={'id': ident}, json=True) post = await u.fetch('http://e621.net/post/show.json', params={'id': ident}, json=True)
if (post['status'] == 'deleted'): if (post['status'] == 'deleted'):
ident = re.search('#(\\d+)', post['delreason']).group(1) ident = re.search('#(\\d+)', post['delreason']).group(1)
post = await u.fetch('http://e621.net/post/show.json', params={'id': ident}, json=True) 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'
return post return post, source, similarity
else: else:
raise IndexError raise IndexError
except IndexError: except IndexError:
try: content = await u.fetch(
'https://saucenao.com/search.php',
params={
'url': url,
'api_key': u.config['saucenao_api'],
'output_type': 2},
json=True)
result = content['results'][0]
if 'author_name' in result['data']:
artist = 'author_name'
elif 'member_name' in result['data']:
artist = 'member_name'
else:
artist = 'creator'
post = {
'file_url': result['header']['thumbnail'],
'artist': [result['data'][artist]],
'score': 'SauceNAO'}
source = result['data']['ext_urls'][0]
similarity = re.search('(\\d+)\\.', result['header']['similarity']).group(1) + '% Match'
return post, source, similarity
raise exc.MatchError(re.search('\\/([^\\/]+)$', url).group(1)) raise exc.MatchError(re.search('\\/([^\\/]+)$', url).group(1))
except AttributeError: except (AttributeError, ValueError, KeyError):
raise exc.MissingArgument raise exc.MissingArgument

View file

@ -26,7 +26,7 @@ try:
except FileNotFoundError: except FileNotFoundError:
with open('config.json', 'w') as outfile: with open('config.json', 'w') as outfile:
jsn.dump({'client_id': 0, 'owner_id': 0, 'permissions': 126016, jsn.dump({'client_id': 0, 'owner_id': 0, 'permissions': 126016,
'playing': 'a game', 'prefix': [',', 'm,'], 'selfbot': False, 'token': 'str'}, outfile, indent=4, sort_keys=True) 'playing': 'a game', 'prefix': [',', 'm,'], 'selfbot': False, 'token': 'str', 'saucenao_api': 'str'}, outfile, indent=4, sort_keys=True)
print('FILE NOT FOUND : config.json created with abstract values. Restart run.py with correct values') print('FILE NOT FOUND : config.json created with abstract values. Restart run.py with correct values')