Skip to content

Commit

Permalink
✨ add google lens for backup search plan
Browse files Browse the repository at this point in the history
  • Loading branch information
wiseCirno committed Jul 28, 2024
1 parent d302472 commit 74e23ba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
9 changes: 7 additions & 2 deletions bot/CoreFunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ async def search_and_reply(url):
_search = AggregationSearch(proxy = self._proxy, cf_proxy = self._cf_proxy)
result = await _search.aggregation_search(url)

if len(_search.exception) == 2:
if len(_search.exception) == 3:
err_message = ''.join([f'{e}\n' for e in _search.exception])
await update.message.reply_text(err_message)
return ConversationHandler.END

if not result:
await update.message.reply_text(f"没有发现对{url}的搜索结果 XwX")
_url = _search.google_result['url']
_reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton("URL", url=_url)]])
await update.message.reply_markdown(
f"没有发现准确搜索结果[😿]({_url}), 此结果并不可靠",
reply_markup = _reply_markup
)
return ConversationHandler.END

_message = f"[🖼️]({result['url']}) Gacha (>ワ<) [😼]({result['thumbnail']})"
Expand Down
30 changes: 25 additions & 5 deletions src/service/Search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import asyncio
from typing import Dict, Optional, List

from PicImageSearch import Ascii2D, Iqdb
from PicImageSearch import Ascii2D, Iqdb, Google
from PicImageSearch import Network
from PicImageSearch.model import Ascii2DResponse, IqdbResponse, IqdbItem
from PicImageSearch.model import Ascii2DResponse, IqdbResponse, IqdbItem, GoogleResponse
from fake_useragent import UserAgent
from httpx import Proxy
from httpx import URL, AsyncClient
Expand Down Expand Up @@ -31,6 +31,7 @@ def __init__(self, proxy: Optional[Proxy] = None, cf_proxy: Optional[str] = None
self.media = b''
self.ascii2d_result: Dict = {}
self.iqdb_result: Dict = {}
self.google_result: Dict = {}

async def get_media(self, url: str, cookies: Optional[str] = None):
_url: URL = URL(url)
Expand Down Expand Up @@ -73,6 +74,20 @@ async def _search_with_type(self, url: str, type: str):

await self._format_iqdb_result(resp)

if type == "google":
base_url = f'{self._cf_proxy}/https://google.com' if self._cf_proxy else 'https://google.com'
search = Google(base_url = base_url, client = client)
resp = await Google.search(search, file = self.media)
if not resp.raw:
raise Exception(f"No google search results, search url: {resp.url}")

await self._format_google_result(resp)

async def _format_google_result(self, resp: GoogleResponse):
self.google_result["thumbnail"] = resp.raw[2].thumbnail
self.google_result["title"] = resp.raw[2].title
self.google_result["url"] = resp.raw[2].url

async def _format_iqdb_result(self, resp: IqdbResponse):
selected_res: IqdbItem = resp.raw[0]
danbooru_res: List[IqdbItem] = [i for i in resp.raw if i.source == "Danbooru"]
Expand All @@ -94,7 +109,6 @@ async def _format_iqdb_result(self, resp: IqdbResponse):
self.iqdb_result["source"] = selected_res.source

async def _format_ascii2d_result(self, resp: Ascii2DResponse, bovw: bool = False):

target: List = self._ascii2d_bovw if bovw else self._ascii2d

for i, r in enumerate(resp.raw):
Expand Down Expand Up @@ -126,8 +140,14 @@ async def ascii2d_search(self, url):
except Exception as e:
self.exception.append(e)

async def google_search(self, url):
try:
await self._search_with_type(url, 'google')
except Exception as e:
self.exception.append(e)

async def aggregation_search(self, url: str) -> Optional[Dict]:
tasks = [self.iqdb_search(url), self.ascii2d_search(url)]
tasks = [self.iqdb_search(url), self.ascii2d_search(url), self.google_search(url)]
await asyncio.gather(*tasks)

return self.iqdb_result if self.iqdb_result else self.ascii2d_result
return self.ascii2d_result if self.ascii2d_result else self.iqdb_result

0 comments on commit 74e23ba

Please sign in to comment.