From 4d272e82a4005505398f94e956755f903d67bfb9 Mon Sep 17 00:00:00 2001 From: qsc20001102 Date: Mon, 22 Sep 2025 14:21:56 +0800 Subject: [PATCH] 11 --- core/api_data.py | 70 ++++++++++++++++ core/request.py | 24 ------ main.py | 121 +++++++++++++++++++-------- templates/jinjia.html | 186 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 343 insertions(+), 58 deletions(-) create mode 100644 core/api_data.py delete mode 100644 core/request.py create mode 100644 templates/jinjia.html diff --git a/core/api_data.py b/core/api_data.py new file mode 100644 index 0000000..c61a479 --- /dev/null +++ b/core/api_data.py @@ -0,0 +1,70 @@ +# core/request.py +import requests +import json +from astrbot.api import logger + +def api_data(api_url, json_data=None, outdata="data"): + """ + 获取数据的POST请求函数 + + Args: + api_url: API地址 + json_data: POST请求的JSON数据 + outdata: 返回数据中要提取的字段,默认为"data" + + Returns: + 成功时返回outdata字段的数据,失败返回None + """ + try: + response = requests.post( + api_url, + json=json_data, # JSON body数据 + timeout=10, + verify=False + ) + + response.raise_for_status() + data = response.json() + + if data.get('code') not in [200, "0"]: + logger.error(f"API返回错误: {data.get('msg', '未知错误')}") + return None + + return data.get(outdata, {}) + + except requests.exceptions.RequestException as e: + logger.error(f"请求出错: {e}") + return None + except json.JSONDecodeError as e: + logger.error(f"JSON解析错误: {e}") + return None + +def fetch_jx3_data(api_url=None,outdata="data", **params): + """ + 获取数据的GET请求函数 + + Args: + api_url: API地址 + outdata: 返回数据中要提取的字段,默认为"data" + **params: 其他查询参数 + + Returns: + 成功时返回outdata字段的数据,失败返回None + """ + try: + response = requests.get(api_url, params=params, timeout=10, verify=False) + response.raise_for_status() + data = response.json() + + if data.get('code') != 200: + logger.error(f"API返回错误: {data.get('msg', '未知错误')}") + return None + + return data.get(outdata, {}) + + except requests.exceptions.RequestException as e: + logger.error(f"请求出错: {e}") + return None + except json.JSONDecodeError as e: + logger.error(f"JSON解析错误: {e}") + return None \ No newline at end of file diff --git a/core/request.py b/core/request.py deleted file mode 100644 index b3f8c26..0000000 --- a/core/request.py +++ /dev/null @@ -1,24 +0,0 @@ -# core/request.py -import requests -import json -from astrbot.api import logger - -def fetch_jx3_data(api_url=None,outdata="data", **params): - # 函数实现 - try: - response = requests.get(api_url, params=params, timeout=10, verify=False) - response.raise_for_status() - data = response.json() - - if data.get('code') != 200: - logger.error(f"API返回错误: {data.get('msg', '未知错误')}") - return None - - return data.get(outdata, {}) - - except requests.exceptions.RequestException as e: - logger.error(f"请求出错: {e}") - return None - except json.JSONDecodeError as e: - logger.error(f"JSON解析错误: {e}") - return None diff --git a/main.py b/main.py index 70fe7c9..7562213 100644 --- a/main.py +++ b/main.py @@ -5,11 +5,9 @@ from astrbot.api.star import Context, Star, register from astrbot.api import logger from astrbot.api import AstrBotConfig -from .core.request import fetch_jx3_data from .core.jx3jiaoyihang import fetch_jx3_jiaoyihang from .core.load_template import load_template - -from jinja2 import Template +from .core.api_data import api_data, fetch_jx3_data # 禁用 SSL 警告 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) @@ -28,7 +26,7 @@ class Jx3ApiPlugin(Star): @filter.command("剑三日常") async def jx3_richang(self, event: AstrMessageEvent): - """获取剑网3日常活动信息""" + """剑三日常""" # 接口URL custom_url = "https://www.jx3api.com/data/active/calendar" # 接口参数 @@ -77,13 +75,10 @@ class Jx3ApiPlugin(Star): logger.error(f"处理数据时出错: {e}") yield event.plain_result("处理接口返回信息时出错") - async def terminate(self): - """可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。""" - @filter.command("剑三骚话") async def jx3_shaohua(self, event: AstrMessageEvent): - """随机获取一条与万花门派相关的骚话""" + """剑三骚话""" # 接口URL custom_url = "https://www.jx3api.com/data/saohua/random" # 接口参数 @@ -113,13 +108,10 @@ class Jx3ApiPlugin(Star): logger.error(f"处理数据时出错: {e}") yield event.plain_result("处理接口返回信息时出错") - async def terminate(self): - """可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。""" - @filter.command("剑三技改") async def jx3_jigai(self, event: AstrMessageEvent): - """查询技能的历史修改记录,包括资料片更新、技能调整等信息""" + """剑三技改""" # 接口URL custom_url = "https://www.jx3api.com/data/skills/records" # 接口参数 @@ -153,13 +145,10 @@ class Jx3ApiPlugin(Star): logger.error(f"处理数据时出错: {e}") yield event.plain_result("处理接口返回信息时出错") - async def terminate(self): - """可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。""" - @filter.command("剑三交易行") async def jx3_jiaoyihang(self, event: AstrMessageEvent): - """获取剑网3交易行信息 区服+物品名称""" + """剑三交易行 服务器 物品名称""" # 接口参数 params = { @@ -207,21 +196,6 @@ class Jx3ApiPlugin(Star): # 调整渲染图片的尺寸 options = { - "viewport": { - "width": 800, - "height": 600, - "device_scale_factor": 1 # 明确设置设备缩放因子 - }, - "clip": { - "x": 0, - "y": 0, - "width": 800, - "height": 600 - }, - "full_page": False, - "type": "jpeg", - "quality": 85, - "scale": "device" # 尝试使用设备缩放 } url = await self.html_render(template_content, render_data, options) @@ -231,7 +205,86 @@ class Jx3ApiPlugin(Star): logger.error(f"交易行查询出错: {e}") yield event.plain_result("查询交易行数据时出错,请稍后再试") - async def terminate(self): - """可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。""" - \ No newline at end of file + @filter.command("剑三沙盘") + async def jx3_shapan(self, event: AstrMessageEvent): + """剑三沙盘 服务器""" + # 接口URL + custom_url = "https://www.jianxiachaguan.cn/api2/aijx3-jxcg/game/get-sand-table-img" + # 接口参数 + params = { + "serverName": "眉间雪" # 默认服务器 + } + # 获取消息内容 + message_str = event.message_str.strip() + parts = message_str.split() + # 解析消息内容 + if len(parts) > 1: + params["serverName"] = parts[1] # 第二个参数为服务器 + # 获取数据 + data = api_data(custom_url,params) + + if not data: + yield event.plain_result("获取获取接口信息失败,请稍后再试") + return + + # 格式化返回消息 + try: + # 构建回复消息 + yield event.image_result(data.get("picUrl")) + + except Exception as e: + logger.error(f"处理数据时出错: {e}") + yield event.plain_result("处理接口返回信息时出错") + + @filter.command("剑三金价") + async def jx3_jinjia(self, event: AstrMessageEvent): + """剑三金价 服务器""" + # 接口URL + custom_url = "https://www.jianxiachaguan.cn/api2/aijx3-jxcg/game/get-gold" + # 接口参数 + params = { + "serverName": "眉间雪" # 默认服务器 + } + # 获取消息内容 + message_str = event.message_str.strip() + parts = message_str.split() + # 解析消息内容 + if len(parts) > 1: + params["serverName"] = parts[1] # 第二个参数为服务器 + # 获取数据 + data = api_data(custom_url,params) + + if not data: + yield event.plain_result("获取获取接口信息失败,请稍后再试") + return + + # 格式化返回消息 + try: + # 加载模板 + try: + template_content = load_template("jinjia.html") + except FileNotFoundError as e: + logger.error(f"加载模板失败: {e}") + yield event.plain_result("系统错误:模板文件不存在") + return + # 准备模板渲染数据 + render_data = { + "items": data, + "server": params["serverName"], + "update_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S") + } + # 调整渲染图片的尺寸 + options = { + } + + url = await self.html_render(template_content, render_data, options) + yield event.image_result(url) + + except Exception as e: + logger.error(f"处理数据时出错: {e}") + yield event.plain_result("处理接口返回信息时出错") + + + async def terminate(self): + """可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。""" \ No newline at end of file diff --git a/templates/jinjia.html b/templates/jinjia.html new file mode 100644 index 0000000..56288a4 --- /dev/null +++ b/templates/jinjia.html @@ -0,0 +1,186 @@ + + + + + 剑网3各平台金价查询 + + + +
+
+

剑网3各平台金价查询

+
服务器: {{ server }}
+
+ +
+
共找到 {{ items|length }} 条数据
+
数据更新时间: {{ update_time }}
+
+ +
+ + + + + + + + + + + + + + {% for item in items %} + + + + + + + + + + {% endfor %} + +
时间贴吧万宝楼DD373UU89851737881
{{ item.date }}{{ item.priceTieba }}{{ item.priceWanbaolou }}{{ item.priceDd373 }}{{ item.priceUu898 }}{{ item.price5173 }}{{ item.price7881 }}
+
+ + +
+ + \ No newline at end of file