This commit is contained in:
2025-09-19 22:29:04 +08:00
parent a52fb7c17c
commit 5b15c66d0f
4 changed files with 182 additions and 17 deletions
+24
View File
@@ -0,0 +1,24 @@
import os
from pathlib import Path
def load_template(template_name):
"""
从模板文件加载模板内容
Args:
template_name: 模板文件名(不带路径)
Returns:
str: 模板内容
"""
# 获取模板文件路径
plugin_dir = Path(__file__).parent.parent
template_path = plugin_dir / "templates" / template_name
# 检查文件是否存在
if not template_path.exists():
raise FileNotFoundError(f"模板文件不存在: {template_path}")
# 读取模板内容
with open(template_path, 'r', encoding='utf-8') as f:
return f.read()
+42 -14
View File
@@ -1,6 +1,5 @@
import requests
import json
import urllib3 import urllib3
from datetime import datetime
from astrbot.api.event import filter, AstrMessageEvent, MessageEventResult from astrbot.api.event import filter, AstrMessageEvent, MessageEventResult
from astrbot.api.star import Context, Star, register from astrbot.api.star import Context, Star, register
from astrbot.api import logger from astrbot.api import logger
@@ -8,9 +7,14 @@ from astrbot.api import AstrBotConfig
from .core.request import fetch_jx3_data from .core.request import fetch_jx3_data
from .core.jx3jiaoyihang import fetch_jx3_jiaoyihang from .core.jx3jiaoyihang import fetch_jx3_jiaoyihang
from .core.load_template import load_template
from jinja2 import Template
# 禁用 SSL 警告 # 禁用 SSL 警告
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
@register("jx3api", "fxdyz", "通过接口调用剑网三API接口获取游戏数据", "1.0.0") @register("jx3api", "fxdyz", "通过接口调用剑网三API接口获取游戏数据", "1.0.0")
class Jx3ApiPlugin(Star): class Jx3ApiPlugin(Star):
def __init__(self, context: Context, config: AstrBotConfig): def __init__(self, context: Context, config: AstrBotConfig):
@@ -21,6 +25,7 @@ class Jx3ApiPlugin(Star):
async def initialize(self): async def initialize(self):
"""可选择实现异步的插件初始化方法,当实例化该插件类之后会自动调用该方法。""" """可选择实现异步的插件初始化方法,当实例化该插件类之后会自动调用该方法。"""
@filter.command("剑三日常") @filter.command("剑三日常")
async def jx3_richang(self, event: AstrMessageEvent): async def jx3_richang(self, event: AstrMessageEvent):
"""获取剑网3日常活动信息""" """获取剑网3日常活动信息"""
@@ -172,23 +177,46 @@ class Jx3ApiPlugin(Star):
if len(parts) > 2: if len(parts) > 2:
params["name"] = parts[2] # 第三个参数为日期偏移 params["name"] = parts[2] # 第三个参数为日期偏移
# 获取数据
data = fetch_jx3_jiaoyihang(params["server"], params["name"])
if not data: # 获取交易行数据
yield event.plain_result("获取接口信息失败,请稍后再试")
return
# 格式化返回消息
try: try:
# 构建回复消息 items_data = fetch_jx3_jiaoyihang(params["server"], params["name"])
result_msg = f"{data}\n"
yield event.plain_result(result_msg) if not items_data or items_data == "无交易行数据":
yield f"在服务器【{params['server']}】未找到物品【{params['name']}】的交易行数据"
return
# 加载模板
try:
template_content = load_template("jiaoyihang.html")
except FileNotFoundError as e:
logger.error(f"加载模板失败: {e}")
yield "系统错误:模板文件不存在"
return
# 准备模板渲染数据
render_data = {
"items": items_data,
"server": params["server"],
"update_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
# 渲染为图片 - 可能需要调整选项以适应完整HTML文档
options = {
"clip": {
"x": 0,
"y": 0,
"width": 800,
"height": 600
}
}
url = await self.html_render(template_content, render_data, options)
yield event.image_result(url)
except Exception as e: except Exception as e:
logger.error(f"处理数据时出错: {e}") logger.error(f"交易行查询出错: {e}")
yield event.plain_result("处理接口返回信息时出错") yield "查询交易行数据时出错,请稍后再试"
async def terminate(self): async def terminate(self):
"""可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。""" """可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。"""
+107
View File
@@ -0,0 +1,107 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>剑网3交易行价格查询</title>
<style>
body {
font-family: 'Microsoft YaHei', sans-serif;
width: 700px;
background-color: #f9f9f9;
padding: 15px;
margin: 0;
box-sizing: border-box;
}
.header {
color: #333;
text-align: center;
border-bottom: 2px solid #4a90e2;
padding-bottom: 8px;
margin: 0 0 15px 0;
font-size: 24px;
}
.summary {
background-color: #4a90e2;
color: white;
padding: 8px;
border-radius: 4px;
margin-bottom: 12px;
font-size: 16px;
}
table {
width: 100%;
border-collapse: collapse;
background-color: white;
border-radius: 4px;
overflow: hidden;
font-size: 14px;
}
th {
background-color: #4a90e2;
color: white;
padding: 8px;
text-align: left;
border-bottom: 2px solid #ddd;
}
td {
padding: 8px;
border-bottom: 1px solid #ddd;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
.item-name {
color: #333;
font-weight: 500;
max-width: 350px;
overflow: hidden;
text-overflow: ellipsis;
}
.price {
text-align: right;
color: #e74c3c;
font-weight: bold;
}
.sample {
text-align: center;
color: #7f8c8d;
}
.footer {
margin-top: 12px;
text-align: center;
color: #7f8c8d;
font-size: 12px;
}
</style>
</head>
<body>
<h1 class="header">剑网3交易行价格查询</h1>
<div class="summary">
共找到 <strong>{{ items|length }}</strong> 件物品 | 服务器: {{ server }}
</div>
<table>
<thead>
<tr>
<th>物品名称</th>
<th style="text-align: right;">最低价</th>
<th style="text-align: center;">样本数</th>
</tr>
</thead>
<tbody>
{% for item in items %}
<tr>
<td class="item-name">{{ item.name }}</td>
<td class="price">{{ item.price }}</td>
<td class="sample">{{ item.sample_size }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="footer">
数据更新时间: {{ update_time }}
</div>
</body>
</html>
+6
View File
@@ -0,0 +1,6 @@
<div>
<h1>测试页面</h1>
<p>服务器: {{ server }}</p>
<p>物品数量: {{ items|length }}</p>
<p>时间: {{ update_time }}</p>
</div>