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 json.decoder import JSONDecodeError
from enum import Enum
from typing import Dict, Any
from pagermaid.enums import Message
from pagermaid.enums.command import CommandHandler
from pagermaid.listener import listener
from pagermaid.utils import Message, client
from pagermaid.services import client
@listener(command="zhrs", description="知乎热搜。")
async def zhrs(_: Client, message: Message):
req = await client.get("https://tenapi.cn/zhihuresou/")
if req.status_code == 200:
try:
data = req.json()
except JSONDecodeError:
await message.edit("出错了呜呜呜 ~ API 数据解析失败。")
return
res = "知乎实时热搜榜:\n"
for i in range(10):
res += f'\n{i + 1}.「<a href={data["list"][i]["url"]}>{data["list"][i]["query"]}</a>」'
await message.edit(res)
else:
class TypeEnum(str, Enum):
ZHIHU = "知乎"
WB = "微博"
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:
data = (await client.get(url)).json()["data"]
except Exception as e:
return {"error": str(e)}
res = ""
for i, d in enumerate(data):
if i == 10:
break
res += f"\n{i + 1}. 「<a href={d['url']}>{d['name']}</a>」"
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 服务器 。")
return
res = f"{name_type.value}实时热搜榜:\n{data['text']}"
await message.edit(res)
@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
res = "微博实时热搜榜:\n"
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)
else:
await message.edit("出错了呜呜呜 ~ 无法访问到 API 服务器 。")
@resou_handler.sub_command(command="zhihu", description="知乎热搜")
async def zhrs(message: Message):
await rs_handler(message, TypeEnum.ZHIHU)
@listener(command="dyrs", description="抖音热搜。")
async def dyrs(_: Client, message: Message):
req = await client.get("https://tenapi.cn/douyinresou/")
if req.status_code == 200:
try:
data = req.json()
except JSONDecodeError:
await message.edit("出错了呜呜呜 ~ API 数据解析失败。")
return
res = "抖音实时热搜榜:\n"
for i in range(10):
res += f'\n{i + 1}.「{data["list"][i]["name"]}」 热度:{data["list"][i]["hot"]}'
await message.edit(res)
else:
await message.edit("出错了呜呜呜 ~ 无法访问到 API 服务器 。")
@resou_handler.sub_command(command="wb", description="微博热搜。")
async def wbrs(message: Message):
await rs_handler(message, TypeEnum.WB)
@resou_handler.sub_command(command="dy", description="抖音热搜。")
async def dyrs(message: Message):
await rs_handler(message, TypeEnum.DY)
@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)