11
This commit is contained in:
+25
-22
@@ -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):
|
||||
"""
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+13
-19
@@ -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"))
|
||||
Reference in New Issue
Block a user