Skip to content

Commit

Permalink
⚡ Reuse downloaded media for searching
Browse files Browse the repository at this point in the history
  • Loading branch information
wiseCirno committed Jun 19, 2024
1 parent 56bb099 commit 4fdf7e3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions bot/CoreFunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ async def search_and_reply(url):

if filters.Sticker.ALL.filter(update.message.reply_to_message):
sticker_url = (await context.bot.get_file(attachment.file_id)).file_path
sticker_instance = AggregationSearch(proxy = self._proxy)
media = await sticker_instance.get_media(sticker_url)
get_sticker = AggregationSearch(proxy = self._proxy)
await get_sticker.get_media(sticker_url)

if attachment.is_video:
filename = attachment.file_unique_id + '.webm'
await update.message.reply_document(media, filename = filename)
await update.message.reply_document(get_sticker.media, filename = filename)
else:
await update.message.reply_photo(photo = media)
await update.message.reply_photo(photo = get_sticker.media)

return ConversationHandler.END

Expand Down
9 changes: 5 additions & 4 deletions src/service/Search.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def __init__(self, proxy: None | Proxy = None):
self._proxy = proxy
self._ascii2d: List = []
self._ascii2d_bovw: List = []

self.media = b''
self.ascii2d_result: Dict = {}
self.iqdb_result: Dict = {}

Expand All @@ -39,14 +41,13 @@ async def get_media(self, url: str, cookies: Optional[str] = None):
proxies = self._proxy, follow_redirects = True
) as client:
resp = await client.get(_url)

return resp.raise_for_status().content
self.media = resp.raise_for_status().content

async def _search_with_type(self, url: str, function, client_class):
async with Network(proxies = self._proxy) as client:
media = await self.get_media(url)
await self.get_media(url) if not self.media else None
search_instance = client_class(client = client)
results = await function(search_instance, file = media)
results = await function(search_instance, file = self.media)
if not results.raw:
raise Exception("No search results")

Expand Down

0 comments on commit 4fdf7e3

Please sign in to comment.