PagerMaid_Plugins_Pyro/google/main.py

37 lines
1.3 KiB
Python
Raw Normal View History

2024-09-28 14:35:08 +00:00
from pagermaid.config import Config
2022-08-02 05:28:30 +00:00
from pagermaid.listener import listener
from pagermaid.enums import Message
from pagermaid.utils import lang, pip_install
2024-09-28 14:35:08 +00:00
from pagermaid.utils.bot_utils import log
2022-08-02 05:28:30 +00:00
pip_install("magic-google", alias="magic_google")
from magic_google import MagicGoogle
2023-07-01 12:18:58 +00:00
@listener(command="google", description=lang("google_des"), parameters="[query]")
2022-08-02 05:28:30 +00:00
async def google(message: Message):
2023-07-01 12:18:58 +00:00
"""Searches Google for a string."""
2022-08-02 05:28:30 +00:00
query = message.arguments
if not query:
if not message.reply_to_message:
2023-07-01 12:18:58 +00:00
return await message.edit(lang("arg_error"))
query = message.reply_to_message.text
2022-08-02 05:28:30 +00:00
mg = MagicGoogle()
2023-07-01 12:18:58 +00:00
query = query.replace(" ", "+")
2022-08-02 05:28:30 +00:00
if not Config.SILENT:
2023-07-01 12:18:58 +00:00
message = await message.edit(lang("google_processing"))
2022-08-02 05:28:30 +00:00
results = ""
for i in mg.search(query=query, num=5):
try:
2023-07-01 12:18:58 +00:00
title = i["text"][:30] + "..."
link = i["url"]
results += f'\n<a href="{link}">{title}</a> \n'
2022-08-02 05:28:30 +00:00
except Exception:
2023-07-01 12:18:58 +00:00
return await message.edit(lang("google_connection_error"))
await message.edit(
f"<b>Google</b> |<code>{query}</code>| 🎙 🔍 \n{results}",
disable_web_page_preview=True,
)
2022-08-02 05:28:30 +00:00
await log(f"{lang('google_success')} `{query}`")