From 559de2e2f6fc74c95265a5716906950dbf74d097 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 25 Feb 2024 00:29:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0bmcl.py=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E4=B8=AD=E7=9A=84wrap=5Frequest=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=92=8C=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/plugins/bmcl.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ica-rs/plugins/bmcl.py b/ica-rs/plugins/bmcl.py index e061c85..594fc8b 100644 --- a/ica-rs/plugins/bmcl.py +++ b/ica-rs/plugins/bmcl.py @@ -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)) -def wrap_request(url: str, client: IcaClient) -> Optional[dict]: - response = requests.get(url) +def wrap_request(url: str, msg: NewMessage, client: IcaClient) -> Optional[dict]: + 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": client.warn( 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: 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: return ranks = [parse_rank(data) for data in rank_data]