diff --git a/api_config.json b/api_config.json index f9cdd60..16183d9 100644 --- a/api_config.json +++ b/api_config.json @@ -126,6 +126,15 @@ "goods_type": "3", "sort[price]": "1" } + }, + "wzry_zhanji":{ + "url":"https://api.t1qq.com/api/tool/wzrr/morebattle", + "method":"GET", + "description":"魔盒中模糊查找剑网三的物品数据", + "params":{ + "key": "vBpEzoiC9z5A9c9Nn83IhLn6M9", + "id": "489048724", + "option": "1" + } } - } \ No newline at end of file diff --git a/core/class_reqsest.py b/core/class_reqsest.py index 628fd77..2f1235a 100644 --- a/core/class_reqsest.py +++ b/core/class_reqsest.py @@ -73,38 +73,41 @@ class APIClient: async def _handle_response(self, response): """ - 处理HTTP响应 - - Args: - response: aiohttp响应对象 - - Returns: - 解析后的数据或None + 处理HTTP响应(增强版:兼容 text/json 和非标准 JSON) """ try: - # 添加响应日志 logger.debug(f"响应状态: {response.status}") - response.raise_for_status() - data = await response.json() - + + # ---------------------- + # 强制兼容 text/json 类型 + # ---------------------- + try: + # content_type=None 忽略 MIME 类型检查 + data = await response.json(content_type=None) + except Exception: + # 响应不是 JSON,尝试手动解析 + text = await response.text() + logger.debug(f"原始文本响应: {text}") + + # 尝试解析 JSON 字符串 + try: + data = json.loads(text) + except json.JSONDecodeError: + logger.error("无法解析为 JSON 数据") + return None + logger.debug(f"响应数据: {data}") - - # 检查API响应状态 + return self._check_response_data(data) - + except aiohttp.ClientError as e: logger.error(f"HTTP错误: {e}") return None - except json.JSONDecodeError as e: - logger.error(f"JSON解析错误: {e}") - # 尝试读取原始文本内容 - try: - text_content = await response.text() - logger.error(f"原始响应内容: {text_content}") - except: - pass + except Exception as e: + logger.error(f"未知错误: {e}") return None + def _check_response_data(self, data): """ diff --git a/core/cless_wzry.py b/core/cless_wzry.py new file mode 100644 index 0000000..587e8e3 --- /dev/null +++ b/core/cless_wzry.py @@ -0,0 +1,80 @@ +from datetime import datetime + +from astrbot.api import logger + +from .class_reqsest import APIClient +from .cless_mysql import AsyncMySQL +from .function_basic import load_template,extract_field,flatten_field,extract_fields,gold_to_string,plot_line_chart_base64 + +class WZRYFunction: + def __init__(self, api_config,db: AsyncMySQL ): + self.__api = APIClient() + self.__db = db + self.__api_config = api_config + + async def zhanji(self,id: str ,option: str): + """ + 战绩查询 + """ + return_data = { + "code": 0, + "msg": "功能函数未执行", + "data": {} + } + #在配置文件中获取接口配置 + api_config = self.__api_config["wzry_zhanji"] + #更新参数 + api_config["params"]["id"] = id + api_config["params"]["option"] = option + fields = ["gametime","killcnt","deadcnt","assistcnt","gameresult","mvpcnt","losemvp","mapName", + "oldMasterMatchScore","newMasterMatchScore","usedTime","winNum","failNum","roleJobName","stars","desc", + "gradeGame","heroIcon"] + # 处理返回数据 + try: + # 获取数据 + data = await self.__api.get(api_config["url"],api_config["params"],"data") + if not data: + return_data["msg"] = "获取接口信息失败" + return return_data + result = extract_fields(data["list"], fields) + result = result[:15] + # 数据处理 + for m in result: + minutes = m["usedTime"] // 60 + seconds = m["usedTime"] % 60 + m["time_str"] = f"{minutes}:{seconds:02d}" + # 可选:提前转义 gameresult + if m["gameresult"] == 1: + m["gameresult_label"] = "胜利" + m["gameresult_bg"] = "#e6fbf1" + m["gameresult_color"] = "#056a3a" + elif m["gameresult"] == 2: + m["gameresult_label"] = "失败" + m["gameresult_bg"] = "#fff0f0" + m["gameresult_color"] = "#9c1f1f" + else: + m["gameresult_label"] = "平局" + m["gameresult_bg"] = "#eee" + m["gameresult_color"] = "#555" + m["MVP"] = "混子" + if m["mvpcnt"] ==1: + m["MVP"] = "胜方MVP" + if m["mvpcnt"] ==1: + m["MVP"] = "败方MVP" + + return_data["data"] = result + + except Exception as e: + logger.error(f"处理数据时出错: {e}") + return_data["msg"] = "处理接口返回信息时出错" + # 加载模板 + try: + return_data["temp"] = load_template("temp_test.html") + except FileNotFoundError as e: + logger.error(f"加载模板失败: {e}") + return_data["msg"] = "系统错误:模板文件不存在" + return return_data + return_data["code"] = 200 + return return_data + + diff --git a/core/test.py b/core/test.py index 08b3839..b5c5e9c 100644 --- a/core/test.py +++ b/core/test.py @@ -1,20 +1,14 @@ -import aiohttp -import asyncio +import http.client -async def fetch_data(): - url = "https://trade-api.seasunwbl.com/api/buyer/goods/list?filter%255Bstate%255D=1&page=1&size=10&goods_type=3&sort%255Bprice%255D=1&filter%255Brole_appearance%255D=%25E6%25BB%2587%25E6%259E%2597%25E9%259B%25A8%25C2%25B7%25E7%259F%25A5%25E5%258D%2597%25C2%25B7%25E6%25A0%2587%25E5%2587%2586" - headers = { - 'User-Agent': 'Apifox/1.0.0 (https://apifox.com)', - 'Accept': '*/*', - 'Host': 'trade-api.seasunwbl.com', - 'Connection': 'keep-alive', - 'Cookie': 'ts_session_id=AqvN1gha8NDXTjdauL6ApVT4KgbJLvPv0Dnd9uid; ts_session_id_=AqvN1gha8NDXTjdauL6ApVT4KgbJLvPv0Dnd9uid' - } - - async with aiohttp.ClientSession() as session: - async with session.get(url, headers=headers) as response: - data = await response.text() - print(data) - -# Run the async function -asyncio.run(fetch_data()) +conn = http.client.HTTPSConnection("api.t1qq.com") +payload = '' +headers = { + 'User-Agent': 'Apifox/1.0.0 (https://apifox.com)', + 'Accept': '*/*', + 'Host': 'api.t1qq.com', + 'Connection': 'keep-alive' +} +conn.request("GET", "/api/tool/wzrr/morebattle?key=vBpEzoiC9z5A9c9Nn83IhLn6M9&id=489048724&option=1", payload, headers) +res = conn.getresponse() +data = res.read() +print(data.decode("utf-8")) \ No newline at end of file diff --git a/main.py b/main.py index 84e82e6..41a9951 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,7 @@ from astrbot.api import AstrBotConfig from .core.cless_mysql import AsyncMySQL from .core.cless_jx3 import JX3Function +from .core.cless_wzry import WZRYFunction @@ -52,6 +53,7 @@ class Jx3ApiPlugin(Star): #创建类实例 self.db = AsyncMySQL(db_config) self.jx3fun = JX3Function(self.api_config,self.db) + self.wzry = WZRYFunction(self.api_config,self.db) # 周期函数调用 @@ -285,6 +287,28 @@ class Jx3ApiPlugin(Star): yield event.plain_result(f"测试值:{self.test_server}") + + @filter.command_group("王者") + def wz(self): + pass + + @wz.command("战绩") + async def wz_zhanji(self, event: AstrMessageEvent,ID: str = "489048724",option: str = "1"): + """王者 战绩 服务器 天数""" + try: + data = await self.wzry.zhanji(ID,option) + # logger.info(f"王者荣耀战绩查询结果{data}") + if data["code"] == 200: + url = await self.html_render(data["temp"],{"data": data["data"]}, options={}) + yield event.image_result(url) + else: + yield event.plain_result(data["msg"]) + return + except Exception as e: + logger.error(f"功能函数执行错误: {e}") + yield event.plain_result("猪脑过载,请稍后再试") + + async def terminate(self): """可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。""" await self.db.close_pool() diff --git a/templates/temp_test.html b/templates/temp_test.html index 4c4a416..b048538 100644 --- a/templates/temp_test.html +++ b/templates/temp_test.html @@ -1,30 +1,57 @@ -
服务器: {{ server }}
+|
+
+ {{ m.desc }}
+
+ |
+
+
+
+
+
+
+
+
+ {{ m.MVP }}
+
+
+
+ {{ m.gameresult_label }}
+
+
+ {{ m.gametime }} · {{ m.mapName }}
+
+
+ + {{ m.roleJobName }} · ⭐{{ m.stars }} +
+ 击杀 {{ m.killcnt }} 死亡 {{ m.deadcnt }} 助攻 {{ m.assistcnt }} 时长 {{ m.time_str }}
+
+
+
+
+ 评分 {{ m.gradeGame }}
+
+
+
+
+ 巅峰赛积分 {{ m.oldMasterMatchScore }} → {{ m.newMasterMatchScore }}
+
+ |
+
|
+ |
+
+
+
+ {{ m.gametime }} · {{ m.mapName }}
+
+
+ + {{ m.roleJobName }} · ⭐{{ m.stars }} + + {{ m.gameresult_label }} + + 评分 {{ m.gradeGame }} +
+ 击杀 {{ m.killcnt }} 死亡 {{ m.deadcnt }} 助攻 {{ m.assistcnt }} 时长 {{ m.time_str }}
+
+
+
+
+ {{ m.desc }}
+ 巅峰赛积分 {{ m.oldMasterMatchScore }} → {{ m.newMasterMatchScore }}
+
+ |
+