代码优化

This commit is contained in:
liujianjiang 2026-01-09 15:46:23 +08:00
parent 3330f60ffc
commit 5a5ba0da43
2 changed files with 39 additions and 28 deletions

View File

@ -70,12 +70,9 @@ class DealAccount:
async def deal_backup_task(self, data: Dict[str, Any]):
# 先查询该账户是否备份过,备份过就修改数据库状态,
await self.db_pool.initialize()
set_param = {"status": 1}
params = (data["account_id"],)
where_conditions = "account_id = %s "
result = await self.update_device_status(set_param, where_conditions, params)
print(result)
if not result:
sql_str = f"""select account_id from shoppe_device_record where account_id='{data['account_id']}'"""
result = await self.db_pool.fetch_all(sql_str, )
if result:
print(f"该账号:{data['account_id']} 已经备份过,不需要在备份")
return True
else:
@ -118,7 +115,7 @@ class DealAccount:
# 设置代理
print(data)
time.sleep(10)
self.task_all.set_network_proxy(pad_code=[data["pad_code"]],country=data["country"])
self.task_all.set_network_proxy(pad_code=[data["pad_code"]], country=data["country"])
# 启动对应脚本
time.sleep(10)
self.task_all.async_execute_adb_command(pad_code=data["pad_code"], file_name=res_dict["script_name"])
@ -126,7 +123,8 @@ class DealAccount:
return True
else:
print(f"云机:{data['pad_code']} 300环境还原失败需要认为干预")
else:
print("未查询到可用还原数据")
return False

53
main.py
View File

@ -7,7 +7,8 @@ import asyncio
import uvicorn
from pathlib import Path
from typing import Dict, Any, Optional, List
from fastapi import FastAPI, HTTPException, Depends, Header
from fastapi import FastAPI, HTTPException, Depends, Header, BackgroundTasks
from public_function.auth import verify_tk_token
from task_management.all_task_management import AllTask
from account_management.deal_account import DealAccount
@ -213,32 +214,44 @@ async def reset_task(task_data: ResetTask, task_manager: Any = Depends(get_task_
raise HTTPException(status_code=404, detail=f"任务:{params.get("task_id")} 重启失败")
async def deal_shopee_task(param: Dict[str, Any], account_manager):
print(param)
result = await account_manager.deal_backup_task(param)
print(f"pad_code{param.get('pad_code')} 备份结果为:{result}")
if param.get("is_restore", False):
time.sleep(random.randint(1, 10))
print(param)
restore_result = await account_manager.deal_restore_system(param)
print(f"pad_code{param.get('pad_code')} 还原结果为:{restore_result}")
# if restore_result and result:
# return {"code": 200, "message": f"任务:{param.get("pad_code")} 备份还原成功"}
# raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份还原失败")
# else:
# if result:
# return {"code": 200, "message": f"任务:{param.get("pad_code")} 备份成功", }
# else:
# raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份失败")
# # try:
# # pass
# # except Exception as e:
# # raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份失败,失败原因:{e}")
@app.post("/shop_backup", summary="云机备份与还原")
async def shop_backup(backup_data: BackupItem, account_manager: Any = Depends(get_account_manager)):
async def shop_backup(background_tasks: BackgroundTasks, backup_data: BackupItem, account_manager: Any = Depends(get_account_manager)):
"""
获取指定应用的可用账号
- **app_name**: 应用名称
pad_code
"""
param = backup_data.model_dump()
print(param)
result = await account_manager.deal_backup_task(param)
if param.get("is_restore", False):
time.sleep(random.randint(1, 10))
print(param)
restore_result = await account_manager.deal_restore_system(param)
if restore_result and result:
return {"code": 200, "message": f"任务:{param.get("pad_code")} 备份还原成功"}
raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份还原失败")
else:
if result:
return {"code": 200, "message": f"任务:{param.get("pad_code")} 备份成功", }
else:
raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份失败")
# try:
# pass
# except Exception as e:
# raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份失败,失败原因:{e}")
task_id = f"task_{int(asyncio.get_event_loop().time())}"
param["task_id"] = task_id # 后续需要对任务状态进行记录使用
background_tasks.add_task(deal_shopee_task, param, account_manager)
return {
"message": "请求已接收,任务在后台执行",
"task_id": task_id
}
if __name__ == '__main__':