更新bmcl.py插件中的wrap_request函数,添加了异常处理和错误提示功能

This commit is contained in:
shenjack 2024-02-25 00:29:48 +08:00
parent 85608570bf
commit 559de2e2f6
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -47,8 +47,16 @@ def format_hit_count(count: int) -> str:
return "_".join(count_str[i:i + 4] for i in range(0, count_len, 4)) return "_".join(count_str[i:i + 4] for i in range(0, count_len, 4))
def wrap_request(url: str, client: IcaClient) -> Optional[dict]: def wrap_request(url: str, msg: NewMessage, client: IcaClient) -> Optional[dict]:
response = requests.get(url) try:
response = requests.get(url)
except requests.exceptions.RequestException as e:
client.warn(
f"数据请求失败, 请检查网络\n{e}"
)
reply = msg.reply_with(f"数据请求失败, 请检查网络\n{e}")
client.send_message(reply)
return None
if not response.status_code == 200 or response.reason != "OK": if not response.status_code == 200 or response.reason != "OK":
client.warn( client.warn(
f"数据请求失败, 请检查网络\n{response.status}" f"数据请求失败, 请检查网络\n{response.status}"
@ -104,7 +112,7 @@ def parse_rank(data: dict) -> dict:
def bmcl_rank(msg: NewMessage, client: IcaClient, name: Optional[str]) -> None: def bmcl_rank(msg: NewMessage, client: IcaClient, name: Optional[str]) -> None:
req_time = time.time() req_time = time.time()
# 记录请求时间 # 记录请求时间
rank_data = wrap_request("https://bd.bangbang93.com/openbmclapi/metric/rank", client) rank_data = wrap_request("https://bd.bangbang93.com/openbmclapi/metric/rank", msg, client)
if rank_data is None: if rank_data is None:
return return
ranks = [parse_rank(data) for data in rank_data] ranks = [parse_rank(data) for data in rank_data]