11
This commit is contained in:
+49
-2
@@ -3,6 +3,7 @@ from astrbot.api import logger
|
|||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
from .api_data import api_data_get, api_data_post
|
from .api_data import api_data_get, api_data_post
|
||||||
|
from .sql_data import sql_data_searchdata,sql_data_select
|
||||||
|
|
||||||
|
|
||||||
def fetch_all_pages(base_url, initial_params, max_pages=None):
|
def fetch_all_pages(base_url, initial_params, max_pages=None):
|
||||||
@@ -126,14 +127,14 @@ def jx3_data_jiaoyihang(inserver="眉间雪", inname="武技殊影图"):
|
|||||||
all_items = fetch_all_pages(custom_url, initial_params)
|
all_items = fetch_all_pages(custom_url, initial_params)
|
||||||
|
|
||||||
if not all_items:
|
if not all_items:
|
||||||
logger.error(f"未找到物品: {inname}")
|
#logger.error(f"未找到物品: {inname}")
|
||||||
return "未找到改物品"
|
return "未找到改物品"
|
||||||
|
|
||||||
# 提取所有ID
|
# 提取所有ID
|
||||||
ids = [item.get("id") for item in all_items if item.get("id")]
|
ids = [item.get("id") for item in all_items if item.get("id")]
|
||||||
|
|
||||||
if not ids:
|
if not ids:
|
||||||
logger.error(f"物品 {inname} 没有有效的ID")
|
#logger.error(f"物品 {inname} 没有有效的ID")
|
||||||
return "未找到改物品"
|
return "未找到改物品"
|
||||||
|
|
||||||
# 返回格式化结果
|
# 返回格式化结果
|
||||||
@@ -182,3 +183,49 @@ def jx3_data_jiaoyihang(inserver="眉间雪", inname="武技殊影图"):
|
|||||||
|
|
||||||
return result_items
|
return result_items
|
||||||
|
|
||||||
|
|
||||||
|
#物价查询
|
||||||
|
def jx3_data_wujia(inname="秃盒"):
|
||||||
|
"""
|
||||||
|
获取剑三外观物品价格数据
|
||||||
|
|
||||||
|
Args:
|
||||||
|
inname: 物品名称
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list: 合并后的数据,包含价格和名称信息
|
||||||
|
"""
|
||||||
|
datas = {
|
||||||
|
"code": 0, # 默奇遇
|
||||||
|
"msg": "未获取数据" # 默认服务器
|
||||||
|
}
|
||||||
|
#获取所查询物品的id和官方名称
|
||||||
|
idname = sql_data_select(inname)
|
||||||
|
|
||||||
|
# 查询万宝楼公示数据
|
||||||
|
custom_url = "https://www.aijx3.cn/api2/aijx3-wblwg/record/queryByCondition"
|
||||||
|
initial_params = {
|
||||||
|
"tradeStatus": "3",
|
||||||
|
"accoSeq": "",
|
||||||
|
"orderMode": 1, # 按时间降序
|
||||||
|
"orderBy":"price_num",
|
||||||
|
"searchId":[idname["searchId"]],
|
||||||
|
"current":1,
|
||||||
|
"size":10
|
||||||
|
}
|
||||||
|
|
||||||
|
datawblgs = api_data_post(custom_url, initial_params,"data")
|
||||||
|
|
||||||
|
if not datawblgs:
|
||||||
|
datas["code"] = 0
|
||||||
|
datas["msg"] = "数据为空"
|
||||||
|
return
|
||||||
|
|
||||||
|
goodsId = datawblgs.get("records", "").get("goodsId", "")
|
||||||
|
|
||||||
|
datas["data"] = goodsId
|
||||||
|
datas["code"] = 200
|
||||||
|
datas["msg"] = "处理完成"
|
||||||
|
|
||||||
|
|
||||||
|
return datas
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
import pymysql
|
||||||
|
import time
|
||||||
|
from astrbot.api import logger
|
||||||
|
from .api_data import api_data_get, api_data_post
|
||||||
|
|
||||||
|
|
||||||
|
# 提取所有 dataModels 中的数据
|
||||||
|
def extract_data_models(source_data):
|
||||||
|
extracted_data = []
|
||||||
|
for category in source_data:
|
||||||
|
if "dataModels" in category and category["dataModels"]:
|
||||||
|
extracted_data.extend(category["dataModels"])
|
||||||
|
return extracted_data
|
||||||
|
|
||||||
|
|
||||||
|
def sql_data_searchdata():
|
||||||
|
|
||||||
|
# 接口URL
|
||||||
|
custom_url = "https://www.aijx3.cn/api2/aijx3-wblwg/basedata/getSearchData"
|
||||||
|
# 接口参数
|
||||||
|
params = {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
# 连接数据库
|
||||||
|
connection = pymysql.connect(
|
||||||
|
host='45.205.31.132', # 数据库主机地址
|
||||||
|
port=5211, # 数据库端口
|
||||||
|
user='asrtbot', # 数据库用户名
|
||||||
|
password='qsc123456', # 数据库密码
|
||||||
|
database='asrtbot', # 数据库名
|
||||||
|
charset='utf8mb4' # 字符编码
|
||||||
|
)
|
||||||
|
|
||||||
|
# 获取数据
|
||||||
|
try:
|
||||||
|
source_data = api_data_post(custom_url,params,"data")
|
||||||
|
|
||||||
|
if not source_data:
|
||||||
|
test = "获取数据失败或数据为空"
|
||||||
|
return
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
test = f"获取数据时出错: {e}"
|
||||||
|
|
||||||
|
#处理数据
|
||||||
|
extracted_data = extract_data_models(source_data)
|
||||||
|
if not source_data:
|
||||||
|
test = "未提取到数据"
|
||||||
|
return
|
||||||
|
|
||||||
|
# 插入数据到数据库
|
||||||
|
try:
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
# 清空表数据
|
||||||
|
cursor.execute("TRUNCATE TABLE searchdata")
|
||||||
|
|
||||||
|
# 准备 SQL 插入语句
|
||||||
|
sql = """
|
||||||
|
INSERT INTO searchdata
|
||||||
|
(typeName, name, showName, picUrl, searchId, searchDescType)
|
||||||
|
VALUES (%s, %s, %s, %s, %s, %s)
|
||||||
|
"""
|
||||||
|
|
||||||
|
# 准备批量数据
|
||||||
|
values_list = []
|
||||||
|
for item in extracted_data:
|
||||||
|
values_list.append((
|
||||||
|
item['typeName'],
|
||||||
|
item['name'],
|
||||||
|
item['showName'],
|
||||||
|
item['picUrl'],
|
||||||
|
item['searchId'],
|
||||||
|
item['searchDescType']
|
||||||
|
))
|
||||||
|
|
||||||
|
# 批量插入
|
||||||
|
cursor.executemany(sql, values_list)
|
||||||
|
connection.commit()
|
||||||
|
test = f"成功批量插入 {len(extracted_data)} 条数据!"
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
test = f"插入数据时出错: {e}"
|
||||||
|
connection.rollback()
|
||||||
|
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
return test
|
||||||
|
|
||||||
|
|
||||||
|
def sql_data_select(search_string):
|
||||||
|
|
||||||
|
# 连接数据库
|
||||||
|
connection = pymysql.connect(
|
||||||
|
host='45.205.31.132', # 数据库主机地址
|
||||||
|
port=5211, # 数据库端口
|
||||||
|
user='asrtbot', # 数据库用户名
|
||||||
|
password='qsc123456', # 数据库密码
|
||||||
|
database='asrtbot', # 数据库名
|
||||||
|
charset='utf8mb4' # 字符编码
|
||||||
|
)
|
||||||
|
|
||||||
|
results = []
|
||||||
|
|
||||||
|
# 插入数据到数据库
|
||||||
|
try:
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
# 准备 SQL 插入语句
|
||||||
|
sql = """
|
||||||
|
SELECT searchId, showName
|
||||||
|
FROM searchdata
|
||||||
|
WHERE name = %s OR showName = %s
|
||||||
|
"""
|
||||||
|
|
||||||
|
# 添加通配符 % 到搜索字符串的两端
|
||||||
|
#search_pattern = f"%{search_string}%"
|
||||||
|
|
||||||
|
# 执行查询
|
||||||
|
cursor.execute(sql, (search_string, search_string))
|
||||||
|
|
||||||
|
# 获取所有匹配的结果
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
|
# 将结果转换为字典列表
|
||||||
|
for row in rows:
|
||||||
|
results.append({
|
||||||
|
"searchId": row[0],
|
||||||
|
"showName": row[1]
|
||||||
|
})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"查询数据时出错: {e}")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
return results
|
||||||
@@ -6,8 +6,9 @@ from astrbot.api import logger
|
|||||||
from astrbot.api import AstrBotConfig
|
from astrbot.api import AstrBotConfig
|
||||||
|
|
||||||
from .core.load_template import load_template
|
from .core.load_template import load_template
|
||||||
from .core.jx3_data import jx3_data_jiaoyihang
|
from .core.jx3_data import jx3_data_jiaoyihang,jx3_data_wujia
|
||||||
from .core.api_data import api_data_get, api_data_post
|
from .core.api_data import api_data_get, api_data_post
|
||||||
|
from .core.sql_data import sql_data_searchdata,sql_data_select
|
||||||
|
|
||||||
# 禁用 SSL 警告
|
# 禁用 SSL 警告
|
||||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||||
@@ -342,5 +343,41 @@ class Jx3ApiPlugin(Star):
|
|||||||
logger.error(f"处理数据时出错: {e}")
|
logger.error(f"处理数据时出错: {e}")
|
||||||
yield event.plain_result("处理接口返回信息时出错")
|
yield event.plain_result("处理接口返回信息时出错")
|
||||||
|
|
||||||
|
|
||||||
|
@filter.command("剑三外观数据同步")
|
||||||
|
async def jx3_SearchData(self, event: AstrMessageEvent):
|
||||||
|
"""剑三外观数据同步"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
test = sql_data_searchdata()
|
||||||
|
yield event.plain_result(f"{test}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"处理数据时出错: {e}")
|
||||||
|
yield event.plain_result("处理接口返回信息时出错")
|
||||||
|
|
||||||
|
|
||||||
|
@filter.command("剑三物价")
|
||||||
|
async def jx3_wujia(self, event: AstrMessageEvent):
|
||||||
|
"""剑三物价 外观名称"""
|
||||||
|
|
||||||
|
inname = "秃盒"
|
||||||
|
|
||||||
|
# 获取消息内容
|
||||||
|
message_str = event.message_str.strip()
|
||||||
|
parts = message_str.split()
|
||||||
|
# 解析消息内容
|
||||||
|
if len(parts) > 1:
|
||||||
|
inname = parts[1] # 外观名称
|
||||||
|
|
||||||
|
try:
|
||||||
|
test = jx3_data_wujia(inname)
|
||||||
|
yield event.plain_result(f"{test}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"处理数据时出错: {e}")
|
||||||
|
yield event.plain_result("处理接口返回信息时出错")
|
||||||
|
|
||||||
|
|
||||||
async def terminate(self):
|
async def terminate(self):
|
||||||
"""可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。"""
|
"""可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。"""
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pymysql
|
||||||
Reference in New Issue
Block a user