11
This commit is contained in:
+80
-111
@@ -4,6 +4,17 @@ from astrbot.api import logger
|
||||
from .api_data import api_data_get, api_data_post
|
||||
|
||||
|
||||
#连接数据库配置
|
||||
db_config = {
|
||||
'host': '154.201.70.116',
|
||||
'port': 3306,
|
||||
'user': 'asrtbot',
|
||||
'password': 'qsc123456',
|
||||
'database': 'asrtbot',
|
||||
'charset': 'utf8mb4',
|
||||
'cursorclass': pymysql.cursors.DictCursor #返回字典格式数据
|
||||
}
|
||||
|
||||
# 提取所有 dataModels 中的数据
|
||||
def extract_data_models(source_data):
|
||||
extracted_data = []
|
||||
@@ -12,127 +23,85 @@ def extract_data_models(source_data):
|
||||
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' # 字符编码
|
||||
)
|
||||
# 接口配置
|
||||
custom_url = "https://www.aijx3.cn/api2/aijx3-wblwg/basedata/getSearchData"
|
||||
params = {}
|
||||
|
||||
# 获取数据
|
||||
try:
|
||||
source_data = api_data_post(custom_url,params,"data")
|
||||
|
||||
# 获取数据
|
||||
source_data = api_data_post(custom_url, params, "data")
|
||||
if not source_data:
|
||||
test = "获取数据失败或数据为空"
|
||||
return
|
||||
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']
|
||||
))
|
||||
# 处理数据
|
||||
extracted_data = extract_data_models(source_data)
|
||||
if not extracted_data: # 修正:应该是extracted_data而不是source_data
|
||||
return "未提取到数据"
|
||||
|
||||
# 连接数据库并插入数据
|
||||
with pymysql.connect(**db_config) as connection:
|
||||
with connection.cursor() as cursor:
|
||||
# 清空表数据
|
||||
cursor.execute("TRUNCATE TABLE searchdata")
|
||||
|
||||
# 批量插入
|
||||
cursor.executemany(sql, values_list)
|
||||
connection.commit()
|
||||
test = f"成功批量插入 {len(extracted_data)} 条数据!"
|
||||
|
||||
# 准备SQL和批量数据
|
||||
sql = """
|
||||
INSERT INTO searchdata
|
||||
(typeName, name, showName, picUrl, searchId, searchDescType)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
"""
|
||||
|
||||
# 使用列表推导式简化数据准备
|
||||
values_list = [
|
||||
(
|
||||
item['typeName'],
|
||||
item['name'],
|
||||
item['showName'],
|
||||
item['picUrl'],
|
||||
item['searchId'],
|
||||
item['searchDescType']
|
||||
)
|
||||
for item in extracted_data
|
||||
]
|
||||
|
||||
# 批量插入
|
||||
cursor.executemany(sql, values_list)
|
||||
connection.commit()
|
||||
|
||||
return f"成功批量插入 {len(extracted_data)} 条数据!"
|
||||
|
||||
except pymysql.Error as e:
|
||||
return f"数据库操作失败: {e}"
|
||||
except Exception as e:
|
||||
test = f"插入数据时出错: {e}"
|
||||
connection.rollback()
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
return test
|
||||
|
||||
return f"操作失败: {e}"
|
||||
|
||||
# 根据搜索字符串查询匹配的数据
|
||||
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 = []
|
||||
|
||||
# 插入数据到数据库
|
||||
"""
|
||||
根据搜索字符串查询匹配的数据
|
||||
|
||||
Args:
|
||||
search_string: 搜索字符串
|
||||
|
||||
Returns:
|
||||
匹配的第一条记录,如果没有匹配则返回None
|
||||
"""
|
||||
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]
|
||||
})
|
||||
with pymysql.connect(**db_config) as connection:
|
||||
with connection.cursor() as cursor:
|
||||
sql = """
|
||||
SELECT searchId, showName, name
|
||||
FROM searchdata
|
||||
WHERE name = %s OR showName = %s
|
||||
LIMIT 1
|
||||
"""
|
||||
params = (search_string, search_string)
|
||||
|
||||
cursor.execute(sql, params)
|
||||
return cursor.fetchone()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"查询数据时出错: {e}")
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
return results[0]
|
||||
return None
|
||||
Reference in New Issue
Block a user