resou 修复 API
All checks were successful
Github commit to telegram / build (push) Successful in 44s

This commit is contained in:
xtaodada 2024-07-08 15:22:43 +08:00
parent 8df760475b
commit a07818e20d
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 96 additions and 51 deletions

View File

@ -1 +1,8 @@
命令wbrszhrsdyrs。 命令resou
- resou zhihu
- resou wb
- resou dy
- resou baidu
- resou b
- resou tt

View File

@ -1,58 +1,96 @@
from pyrogram import Client from enum import Enum
from json.decoder import JSONDecodeError from typing import Dict, Any
from pagermaid.enums import Message
from pagermaid.enums.command import CommandHandler
from pagermaid.listener import listener from pagermaid.listener import listener
from pagermaid.utils import Message, client from pagermaid.services import client
@listener(command="zhrs", description="知乎热搜。") class TypeEnum(str, Enum):
async def zhrs(_: Client, message: Message): ZHIHU = "知乎"
req = await client.get("https://tenapi.cn/zhihuresou/") WB = "微博"
if req.status_code == 200: DY = "抖音"
BAIDU = "百度"
B = "哔哩哔哩"
TT = "今日头条"
RESOU_HELP = """热搜榜单
- 参数 zhihu 查询知乎热搜
- 参数 wb 查询微博热搜
- 参数 dy 查询抖音热搜
- 参数 baidu 查询百度热搜
- 参数 b 查询哔哩哔哩
- 参数 tt 查询今日头条热点
"""
RESOU_MAP = {
TypeEnum.ZHIHU: "https://tenapi.cn/v2/zhihuhot",
TypeEnum.WB: "https://tenapi.cn/v2/weibohot",
TypeEnum.DY: "https://tenapi.cn/v2/douyinhot",
TypeEnum.BAIDU: "https://tenapi.cn/v2/baiduhot",
TypeEnum.B: "https://tenapi.cn/v2/bilihot",
TypeEnum.TT: "https://tenapi.cn/v2/toutiaohot"
}
async def get_data(name_type: TypeEnum) -> Dict[str, Any]:
url = RESOU_MAP.get(name_type)
try: try:
data = req.json() data = (await client.get(url)).json()["data"]
except JSONDecodeError: except Exception as e:
await message.edit("出错了呜呜呜 ~ API 数据解析失败。") return {"error": str(e)}
return res = ""
res = "知乎实时热搜榜:\n" for i, d in enumerate(data):
for i in range(10): if i == 10:
res += f'\n{i + 1}.「<a href={data["list"][i]["url"]}>{data["list"][i]["query"]}</a>」' break
await message.edit(res) res += f"\n{i + 1}. 「<a href={d['url']}>{d['name']}</a>」"
else: return {"error": None, "data": data, "text": res}
@listener(command="resou", description="热搜榜单查询")
async def resou_handler(message: Message):
await message.edit(RESOU_HELP)
resou_handler: "CommandHandler"
async def rs_handler(message: Message, name_type: TypeEnum):
data = await get_data(name_type)
if data.get("error"):
await message.edit("出错了呜呜呜 ~ 无法访问到 API 服务器 。") await message.edit("出错了呜呜呜 ~ 无法访问到 API 服务器 。")
@listener(command="wbrs", description="微博热搜。")
async def wbrs(_: Client, message: Message):
req = await client.get("https://tenapi.cn/resou/")
if req.status_code == 200:
try:
data = req.json()
except JSONDecodeError:
await message.edit("出错了呜呜呜 ~ API 数据解析失败。")
return return
res = "微博实时热搜榜:\n" res = f"{name_type.value}实时热搜榜:\n{data['text']}"
for i in range(10):
res += (
f'\n{i + 1}.「<a href={data["list"][i]["url"]}>{data["list"][i]["name"]}</a>」 '
f'热度:{data["list"][i]["hot"]}'
)
await message.edit(res) await message.edit(res)
else:
await message.edit("出错了呜呜呜 ~ 无法访问到 API 服务器 。")
@listener(command="dyrs", description="抖音热搜。") @resou_handler.sub_command(command="zhihu", description="知乎热搜")
async def dyrs(_: Client, message: Message): async def zhrs(message: Message):
req = await client.get("https://tenapi.cn/douyinresou/") await rs_handler(message, TypeEnum.ZHIHU)
if req.status_code == 200:
try:
data = req.json() @resou_handler.sub_command(command="wb", description="微博热搜。")
except JSONDecodeError: async def wbrs(message: Message):
await message.edit("出错了呜呜呜 ~ API 数据解析失败。") await rs_handler(message, TypeEnum.WB)
return
res = "抖音实时热搜榜:\n"
for i in range(10): @resou_handler.sub_command(command="dy", description="抖音热搜。")
res += f'\n{i + 1}.「{data["list"][i]["name"]}」 热度:{data["list"][i]["hot"]}' async def dyrs(message: Message):
await message.edit(res) await rs_handler(message, TypeEnum.DY)
else:
await message.edit("出错了呜呜呜 ~ 无法访问到 API 服务器 。")
@resou_handler.sub_command(command="baidu", description="百度热搜。")
async def bdrs(message: Message):
await rs_handler(message, TypeEnum.BAIDU)
@resou_handler.sub_command(command="b", description="哔哩哔哩热搜。")
async def brs(message: Message):
await rs_handler(message, TypeEnum.B)
@resou_handler.sub_command(command="tt", description="今日头条热点。")
async def ttrs(message: Message):
await rs_handler(message, TypeEnum.TT)