Skip to content

Commit

Permalink
✨ Add google search, but it will not work without proxy (not cf proxy)
Browse files Browse the repository at this point in the history
  • Loading branch information
wiseCirno committed Aug 24, 2024
1 parent a988dd3 commit 460f4a7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 35 deletions.
49 changes: 28 additions & 21 deletions bot/CoreFunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,36 @@ async def search_and_reply(url):
return ConversationHandler.END

if not search_result:
await update.message.reply_text("没有发现准确搜索结果😿")
await update.message.reply_text("No search results.")
return ConversationHandler.END

search_reply_message = f"[🖼️]({search_result['url']}) Gacha (>ワ<) [😼]({search_result['thumbnail']})"

if search_result["class"] == "iqdb":
search_reply_integrated_buttons = [
[InlineKeyboardButton(
f"{search_result['source']}: {search_result['similarity']}% Match",
url = search_result['url'])]
]
await update.message.reply_markdown(
search_reply_message, reply_markup = InlineKeyboardMarkup(search_reply_integrated_buttons)
)

elif search_result["class"] == "ascii2d":
search_reply_integrated_buttons = [
[InlineKeyboardButton("Original", url = search_result['url'])],
[InlineKeyboardButton(f"{search_result['author']}", url = search_result['author_url'])]
]
await update.message.reply_markdown(
search_reply_message, reply_markup = InlineKeyboardMarkup(search_reply_integrated_buttons)
)
for result in search_result:
if result["class"] == "iqdb":
search_reply_message = f"[🖼️]({result['url']}) Iqdb Search [😼]({result['thumbnail']})"
search_reply_integrated_buttons = [
[InlineKeyboardButton(
f"{result['source']}: {result['similarity']}% Match",
url = result['url'])]
]
await update.message.reply_markdown(
search_reply_message, reply_markup = InlineKeyboardMarkup(search_reply_integrated_buttons)
)

elif result["class"] == "ascii2d":
search_reply_message = f"[🖼️]({result['url']}) Ascii2d Search [😼]({result['thumbnail']})"
search_reply_integrated_buttons = [
[InlineKeyboardButton("Original", url = result['url'])],
[InlineKeyboardButton(f"{result['author']}", url = result['author_url'])]
]
await update.message.reply_markdown(
search_reply_message, reply_markup = InlineKeyboardMarkup(search_reply_integrated_buttons)
)
elif result["class"] == "google":
search_reply_message = f"[🖼️]({result['url']}) Google Search [😼]({result['thumbnail']})"
search_reply_integrated_buttons = [[InlineKeyboardButton("Original", url = result['url'])]]
await update.message.reply_markdown(
search_reply_message, reply_markup = InlineKeyboardMarkup(search_reply_integrated_buttons)
)

# start from here
link_preview = update.message.reply_to_message.link_preview_options
Expand Down
37 changes: 23 additions & 14 deletions src/service/Search.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ async def get_media(self, url: str, cookies: Optional[str] = None):

async def _search_with_type(self, url: str, type: str):
async with Network(proxies = self._proxy) as client:
self.media = await self.get_media(url)
await self.get_media(url)

if type == "ascii2d":
base_url = f'{self._cf_proxy}/https://ascii2d.net' if self._cf_proxy else 'https://ascii2d.net'
ascii2d = Ascii2D(base_url = base_url, client = client)
ascii2d_bovw = Ascii2D(base_url = base_url, client = client, bovw = True)

resp = await ascii2d.search(file = self.media)
bovw_resp = await ascii2d_bovw.search(file = self.media)
resp, bovw_resp = await asyncio.gather(ascii2d.search(file = self.media),
ascii2d_bovw.search(file = self.media))
if not resp.raw and not bovw_resp.raw:
raise Exception(f"No ascii2d search results, search url: {resp.url}")

tasks = [self._format_ascii2d_result(bovw_resp, bovw = True), self._format_ascii2d_result(resp)]
await asyncio.gather(*tasks)
await asyncio.gather(self._format_ascii2d_result(bovw_resp, bovw = True),
self._format_ascii2d_result(resp))

if type == "iqdb":
base_url = f'{self._cf_proxy}/https://iqdb.org' if self._cf_proxy else 'https://iqdb.org'
Expand All @@ -76,7 +76,7 @@ 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'
base_url = f'{self._cf_proxy}/https://www.google.com' if self._cf_proxy else 'https://www.google.com'
google = Google(base_url = base_url, client = client)

resp = await google.search(file = self.media)
Expand All @@ -86,9 +86,13 @@ async def _search_with_type(self, url: str, type: str):
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
if len(resp.raw) >= 3:
self.google_result = {
"class": "google",
"thumbnail": resp.raw[2].thumbnail,
"title": resp.raw[2].title,
"url": resp.raw[2].url
}

async def _format_iqdb_result(self, resp: IqdbResponse):
selected_res = resp.raw[0]
Expand All @@ -111,7 +115,7 @@ async def _format_ascii2d_result(self, resp: Ascii2DResponse, bovw: bool = False
target = self._ascii2d_bovw if bovw else self._ascii2d

for i, r in enumerate(resp.raw):
if not r.url_list or i == 0:
if not r.url_list or i == 0 or not r.url:
continue

r.author = r.author or "None"
Expand Down Expand Up @@ -144,10 +148,15 @@ async def ascii2d_search(self, url):

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

async def aggregation_search(self, url: str) -> Optional[Dict]:
await asyncio.gather(self.iqdb_search(url), self.ascii2d_search(url))
return self.iqdb_result or self.ascii2d_result
async def aggregation_search(self, url: str) -> Optional[List[Dict]]:
await asyncio.gather(self.iqdb_search(url), self.ascii2d_search(url), self.google_search(url))
result = []
result.append(self.iqdb_result) if self.iqdb_result else None
result.append(self.ascii2d_result) if self.ascii2d_result else None
result.append(self.google_result) if self.google_result else None

return result

0 comments on commit 460f4a7

Please sign in to comment.