PagerMaid_Plugins_Pyro/pypi/main.py

52 lines
1.7 KiB
Python
Raw Permalink Normal View History

2022-07-25 08:37:44 +00:00
import html
import re
from pyrogram.enums import ParseMode
from pagermaid.enums import Message, AsyncClient
from pagermaid.listener import listener
def clean_html(raw_html):
return re.sub(re.compile(r"<.*?>"), "", raw_html)
def escape_definition(definition):
for key, value in definition.items():
if isinstance(value, str):
definition[key] = html.escape(clean_html(value))
return definition
2023-07-01 12:18:58 +00:00
@listener(
command="pypi", description="Search PyPI packages", parameters="The query string"
)
2022-07-25 08:37:44 +00:00
async def pypi(message: Message, httpx: AsyncClient):
if not message.arguments:
return await message.edit("Please provide a query string")
2023-07-01 12:18:58 +00:00
r = await httpx.get(
f"https://pypi.org/pypi/{message.arguments}/json", follow_redirects=True
)
2022-07-25 08:37:44 +00:00
if r.status_code != 200:
return await message.edit("Could not find the package")
json = r.json()
pypi_info = escape_definition(json["info"])
text = """
<b><a href="{package_link}">{package_name}</a></b> by <i>{author_name} {author_email}</i>
平台<b>{platform}</b>
版本<b>{version}</b>
许可协议<b>{license}</b>
摘要<b>{summary}</b>""".format(
2023-07-01 12:18:58 +00:00
package_link=f"https://pypi.org/pypi/{message.arguments}",
package_name=pypi_info["name"],
author_name=pypi_info["author"],
author_email=f"&lt;{pypi_info['author_email']}&gt;"
if pypi_info["author_email"]
else "",
platform=pypi_info["platform"] or "未指定",
version=pypi_info["version"],
license=pypi_info["license"] or "未指定",
summary=pypi_info["summary"],
)
2022-07-25 08:37:44 +00:00
await message.edit(text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)