🐛 修复迁移旧版数据库失败的BUG

This commit is contained in:
2025-05-26 15:10:12 +08:00
parent cca3dc5d4d
commit c131692b19
15 changed files with 345 additions and 119 deletions
+61 -15
View File
@@ -1,28 +1,50 @@
import json
import os
from datetime import datetime
import httpx
from zhenxun.configs.config import Config
from zhenxun.services.log import logger
from .config import g_sSignInPath
from .tool import g_pToolManager
class CRequestManager:
m_sTokens = "xZ%?z5LtWV7H:0-Xnwp+bNRNQ-jbfrxG"
@classmethod
async def download(cls, url: str, savePath: str, fileName: str) -> bool:
async def download(cls,
url: str,
savePath: str,
fileName: str,
params: dict | None = None,
jsonData: dict | None = None) -> bool:
"""下载文件到指定路径并覆盖已存在的文件
Args:
url (str): 文件的下载链接
savePath (str): 保存文件夹路径
fileName (str): 保存后的文件名
params (dict | None): 可选的 URL 查询参数
jsonData (dict | None): 可选的 JSON 请求体
Returns:
bool: 是否下载成功
"""
headers = {"token": cls.m_sTokens}
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(url)
# 动态组装请求参数
requestArgs: dict = {"headers": headers}
if params:
requestArgs["params"] = params
if jsonData:
requestArgs["json"] = jsonData
response = await client.request("GET", url, **requestArgs)
if response.status_code == 200:
fullPath = os.path.join(savePath, fileName)
os.makedirs(os.path.dirname(fullPath), exist_ok=True)
@@ -32,6 +54,7 @@ class CRequestManager:
else:
logger.warning(f"文件下载失败: HTTP {response.status_code} {response.text}")
return False
except Exception as e:
logger.warning(f"下载文件异常: {e}")
return False
@@ -56,7 +79,7 @@ class CRequestManager:
baseUrl = Config.get_config("zhenxun_plugin_farm", "服务地址")
url = f"{baseUrl.rstrip('/')}/{endpoint.lstrip('/')}"
headers = {"token": "xZ%?z5LtWV7H:0-Xnwp+bNRNQ-jbfrxG"}
headers = {"token": cls.m_sTokens}
try:
async with httpx.AsyncClient(timeout=5.0) as client:
@@ -87,7 +110,7 @@ class CRequestManager:
"""
baseUrl = Config.get_config("zhenxun_plugin_farm", "服务地址")
url = f"{baseUrl.rstrip('/')}/{endpoint.lstrip('/')}"
headers = {"token": "xZ%?z5LtWV7H:0-Xnwp+bNRNQ-jbfrxG"}
headers = {"token": cls.m_sTokens}
try:
async with httpx.AsyncClient(timeout=5.0) as client:
@@ -106,20 +129,43 @@ class CRequestManager:
return {}
@classmethod
async def sign(cls, uid: str) -> str:
a = await cls.post("http://diuse.work:9099/testPost", jsonData={"level":3})
async def initSignInFile(cls):
if os.path.exists(g_sSignInPath):
result = ""
try:
with open(g_sSignInPath, "r", encoding="utf-8") as f:
content = f.read()
sign = json.loads(content)
type = int(a["type"])
if type == 1:
result = f"签到成功 type = 1"
elif type == 2:
result = f"签到成功 type = 2"
date = sign.get("date", "")
yearMonth = datetime.now().strftime("%Y%m")
if date == yearMonth:
logger.debug("真寻农场签到文件检查完毕")
else:
logger.warning("真寻农场签到文件检查失败, 即将下载")
await cls.downloadSignInFile()
except json.JSONDecodeError as e:
logger.warning(f"真寻农场签到文件格式错误, 即将下载")
await cls.downloadSignInFile()
else:
result = f"签到成功 type = {type}"
await cls.downloadSignInFile()
return result
@classmethod
async def downloadSignInFile(cls):
baseUrl = Config.get_config("zhenxun_plugin_farm", "服务地址")
url = f"{baseUrl.rstrip('/')}/sign_in"
url = f"http://127.0.0.1:8998/sign_in"
path = str(g_sSignInPath.parent.resolve(strict=False))
yearMonth = datetime.now().strftime("%Y%m")
logger.info(f"{yearMonth}")
yearMonth = "202506"
await cls.download(url, path, "signTemp.json", jsonData={'date':yearMonth})
g_pToolManager.renameFile(f"{path}/signTemp.json", "sign_in.json")
g_pRequestManager = CRequestManager()