sycgram/plugins/google.py

49 lines
1.5 KiB
Python
Raw Normal View History

2022-04-07 02:55:52 +00:00
import asyncio
2022-04-09 05:35:49 +00:00
import re
2022-04-07 02:55:52 +00:00
from core import command
from loguru import logger
from pyrogram import Client
from pyrogram.errors import FloodWait
from pyrogram.types import Message
from tools.googles import google_search
2022-04-09 05:35:49 +00:00
from tools.helpers import Parameters, show_cmd_tip, show_exception
2022-04-07 02:55:52 +00:00
@Client.on_message(command("google"))
async def google(_: Client, msg: Message):
2022-04-07 05:03:37 +00:00
"""谷歌搜索并展示第一页结果和链接"""
2022-04-09 05:35:49 +00:00
cmd, args = Parameters.get(msg)
2022-04-07 02:55:52 +00:00
replied_msg = msg.reply_to_message
2022-04-09 05:35:49 +00:00
if not args and replied_msg and (replied_msg.text or replied_msg.caption):
pattern = replied_msg.text or replied_msg.caption
elif args:
pattern = args
2022-04-07 02:55:52 +00:00
else:
2022-04-09 05:35:49 +00:00
return await show_cmd_tip(msg, cmd)
2022-04-07 02:55:52 +00:00
try:
2022-04-09 05:35:49 +00:00
res = await google_search(pattern)
links = '\n\n'.join(
2022-04-07 05:03:37 +00:00
f"[{title[0:30]}]({url})" for title, url in res.items()
2022-04-07 02:55:52 +00:00
)
2022-04-09 05:35:49 +00:00
pattern = re.sub(r'[_*`[]', '', pattern[0:30])
text = f"🔎 | **Google** | `{pattern}`\n{links}"
2022-04-07 02:55:52 +00:00
await msg.edit_text(
text=text,
parse_mode='md',
disable_web_page_preview=True
)
except FloodWait as e:
await asyncio.sleep(e.x)
await msg.edit_text(
text=text,
parse_mode='md',
disable_web_page_preview=True
)
except Exception as e:
logger.error(e)
2022-04-09 05:35:49 +00:00
await show_exception(msg, "Unable to connect to google")
2022-04-07 02:55:52 +00:00
finally:
await logger.complete()