Skip to content

Commit

Permalink
🐛 fix link preview url search error
Browse files Browse the repository at this point in the history
  • Loading branch information
wiseCirno committed Jun 7, 2024
1 parent 248b134 commit 62667d0
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions bot/CoreFunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ def __init__(self, proxy: None | Proxy = None) -> None:
self._proxy = proxy
self._headers = {'User-Agent': UserAgent().random}

async def _get_url(self, query) -> Optional[List[str]]:
try:
async with AsyncClient(proxy = self._proxy) as client:
resp = await client.get(url = query, headers = self._headers)
if resp.status_code == 200:
return re.findall(r'img src="(.*?)"', resp.text)
except Exception:
raise Exception(f"[Multi Query]: Failed to get image {query}")

async def auto_parse_reply(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
async def send_epub(url):
instance = Telegraph(url, self._proxy)
Expand Down Expand Up @@ -116,16 +107,15 @@ async def search(url) -> tuple[str, Optional[InlineKeyboardMarkup]]:
await send_epub(url = link_preview.url)
return ConversationHandler.END
else:
link_url = await self._get_url(link_preview.url)
msg, mark = await search(link_url)
await update.message.reply_markdown(text = msg)
msg, mark = await search(link_preview.url)
await update.message.reply_markdown(msg, reply_markup = mark)
return ConversationHandler.END

if filters.PHOTO.filter(update.message.reply_to_message):
photo_file = update.message.reply_to_message.photo[2]
file_link = (await context.bot.get_file(photo_file.file_id)).file_path
msg, mark = await search(file_link)
await update.message.reply_markdown(text = msg, reply_markup = mark)
await update.message.reply_markdown(msg, reply_markup = mark)
return ConversationHandler.END

if filters.Sticker.ALL.filter(update.message.reply_to_message):
Expand All @@ -135,7 +125,7 @@ async def search(url) -> tuple[str, Optional[InlineKeyboardMarkup]]:

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

Expand All @@ -144,7 +134,7 @@ async def search(url) -> tuple[str, Optional[InlineKeyboardMarkup]]:
if filters.Document.IMAGE.filter(update.message.reply_to_message):
file_link = (await context.bot.get_file(attachment.thumbnail.file_id)).file_path
msg, mark = await search(file_link)
await update.message.reply_markdown(text = msg, reply_markup = mark)
await update.message.reply_markdown(msg, reply_markup = mark)
return ConversationHandler.END
else:
await update.message.reply_text("这是什么 OwO")
Expand Down

0 comments on commit 62667d0

Please sign in to comment.