From df0866b424e76ad1377c8364ef61588a9e489f4d Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 21 Feb 2022 13:43:53 -0600 Subject: [PATCH] Add kheina reverse image search --- commands/booru.py | 5 +++-- tools/scraper.py | 48 ++++++++++++++++++++++------------------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/commands/booru.py b/commands/booru.py index da2d990..c220707 100644 --- a/commands/booru.py +++ b/commands/booru.py @@ -93,10 +93,11 @@ async def _reverse(context, urls, *, selector=None): return pages = [(hikari.Embed( - title=match['artist'], url=match['source'], color=context.get_guild().get_my_member().get_top_role().color) + title=match['artist'], url=match['url'], color=context.get_guild().get_my_member().get_top_role().color) .set_author(name=f'{match["similarity"]}% Match') .set_image(match['thumbnail']) - .set_footer(match['index'])) if match else f'**No match found for:**\n{urls[index]}' for index, match in enumerate(matches)] + .set_footer(match['source'])) + if match else f'**No match found.**\n{urls[index]}' for index, match in enumerate(matches)] if len(pages) > 1: selector = components.Selector( diff --git a/tools/scraper.py b/tools/scraper.py index be7c752..cc71820 100644 --- a/tools/scraper.py +++ b/tools/scraper.py @@ -11,21 +11,7 @@ sauce = pysaucenao.SauceNao(api_key=c.config['saucenao'], priority=(29, 40, 41)) async def reverse(urls): - matches = [] - - for url in urls: - saucenao = await _saucenao(url) - kheina = None - - if saucenao: - matches.append(saucenao) - else: - pass - - if not saucenao and not kheina: - matches.append(None) - - return matches + return [await _saucenao(url) or await _kheina(url) for url in urls] async def _saucenao(url): try: @@ -37,21 +23,31 @@ async def _saucenao(url): except pysaucenao.InvalidImageException: raise pysaucenao.InvalidImageException(url) - if results: - return { - 'source': results[0].url, - 'artist': results[0].author_name or 'unknown', - 'thumbnail': results[0].thumbnail, - 'similarity': int(results[0].similarity), - 'index': tldextract.extract(results[0].index).domain} - return + return { + 'url': results[0].url, + 'artist': ', '.join(results[0].authors) or 'Unknown', + 'thumbnail': results[0].thumbnail, + 'similarity': round(results[0].similarity), + 'source': tldextract.extract(results[0].index).domain + } if results else None async def _kheina(url): - pass + content = await _post('https://api.kheina.com/v1/search', {'url': url}) -async def _fetch(url): + if content['results'][0]['similarity'] < 50: + return None + + return { + 'url': content['results'][0]['sources'][0]['source'], + 'artist': content['results'][0]['sources'][0]['artist'] or 'Unknown', + 'thumbnail': f'https://cdn.kheina.com/file/kheinacom/{content["results"][0]["sources"][0]["sha1"]}.jpg', + 'similarity': round(content['results'][0]['similarity']), + 'source': tldextract.extract(content['results'][0]['sources'][0]['source']).domain + } + +async def _post(url, data): async with aiohttp.ClientSession() as session: - async with session.get(url, headers={'User-Agent': 'Myned/Modufur (https://github.com/Myned/Modufur)'}) as response: + async with session.post(url, data=data, headers={'User-Agent': 'Myned/Modufur (https://github.com/Myned/Modufur)'}) as response: return await response.json() if response.status == 200 else None