增加前测任务获取前测结果
This commit is contained in:
@@ -4,6 +4,7 @@ from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.orm import selectinload
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.base import async_session
|
||||
from app.models.upload_task import UploadTask
|
||||
@@ -11,6 +12,7 @@ from app.models.generated_resource import GeneratedResource
|
||||
from app.models.user_oauth import UserOAuth
|
||||
from app.models.resources_material import ResourcesMaterial
|
||||
from app.models.user_oauth_account import UserOAuthAccount
|
||||
from app.models.pre_test_template import PreTestTemplate
|
||||
from app.utils.id_gen import generate_id
|
||||
from app.utils.douyinApi import DouyinApi
|
||||
|
||||
@@ -51,7 +53,7 @@ if not logger.handlers:
|
||||
|
||||
douyin_api = DouyinApi()
|
||||
|
||||
|
||||
#上传素材队列,处理上传素材的任务
|
||||
class UploadQueue:
|
||||
def __init__(self):
|
||||
self.queue: asyncio.Queue[str] = asyncio.Queue()
|
||||
@@ -124,7 +126,8 @@ class UploadQueue:
|
||||
task.oauth_id,
|
||||
task.advertiser_id,
|
||||
task.resource_id,
|
||||
db=None
|
||||
db=None,
|
||||
other_info=json.loads(task.other_info) if task.other_info else None,
|
||||
)
|
||||
|
||||
async with async_session() as db:
|
||||
@@ -215,7 +218,8 @@ async def _upload_single_material(
|
||||
oauth_id: str,
|
||||
advertiser_id: str,
|
||||
resource_id: str,
|
||||
db=None
|
||||
db=None,
|
||||
other_info=None,
|
||||
) -> dict:
|
||||
own_db = False
|
||||
if db is None:
|
||||
@@ -269,9 +273,20 @@ async def _upload_single_material(
|
||||
"error": "资源本地存储路径为空",
|
||||
}
|
||||
|
||||
return await _upload_to_juliang(
|
||||
result = await _upload_to_juliang(
|
||||
oauth_id, storage_path, resource_type, advertiser_id, resource, db, user_id, file_name
|
||||
)
|
||||
|
||||
if result.get("success") and resource_type == "video" and other_info and "is_pre_test" in other_info and other_info["is_pre_test"] == "1" and "pre_test_template" in other_info:
|
||||
await _pre_test_material(
|
||||
oauth_id,
|
||||
advertiser_id,
|
||||
[result.get("upload_id")],
|
||||
other_info["pre_test_template"],
|
||||
db,
|
||||
)
|
||||
|
||||
return result
|
||||
finally:
|
||||
if own_db and db is not None:
|
||||
await db.close()
|
||||
@@ -470,4 +485,172 @@ async def _upload_to_juliang(
|
||||
}
|
||||
|
||||
|
||||
async def _pre_test_material(
|
||||
oauth_id: str,
|
||||
advertiser_id: str,
|
||||
video_ids: list[str],
|
||||
pre_test_template_id: str,
|
||||
db: AsyncSession,
|
||||
) -> any:
|
||||
oauth = await db.execute(
|
||||
select(UserOAuth).where(
|
||||
UserOAuth.id == oauth_id,
|
||||
UserOAuth.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
oauth = oauth.scalar_one_or_none()
|
||||
if not oauth:
|
||||
note = "授权记录不存在"
|
||||
for video_id in video_ids:
|
||||
await _update_material_pre_test_status(
|
||||
db, oauth_id, advertiser_id, video_id,
|
||||
task_id=None,
|
||||
status="FAILED",
|
||||
note=note,
|
||||
pre_result=None,
|
||||
pre_test_template_id=pre_test_template_id,
|
||||
)
|
||||
await db.commit()
|
||||
return {"code": -1, "message": note, "data": {}}
|
||||
|
||||
pre_test_template = await db.execute(
|
||||
select(PreTestTemplate).where(
|
||||
PreTestTemplate.id == pre_test_template_id,
|
||||
PreTestTemplate.deleted_at.is_(None),
|
||||
PreTestTemplate.user_id == oauth.user_id,
|
||||
)
|
||||
)
|
||||
pre_test_template = pre_test_template.scalar_one_or_none()
|
||||
if not pre_test_template:
|
||||
note = f"前测模板 {pre_test_template_id} 不存在"
|
||||
for video_id in video_ids:
|
||||
await _update_material_pre_test_status(
|
||||
db, oauth_id, advertiser_id, video_id,
|
||||
task_id=None,
|
||||
status="FAILED",
|
||||
note=note,
|
||||
pre_result=None,
|
||||
pre_test_template_id=pre_test_template_id,
|
||||
)
|
||||
await db.commit()
|
||||
return {"code": -1, "message": note, "data": {}}
|
||||
|
||||
diagnose_config = {}
|
||||
if pre_test_template.platform:
|
||||
diagnose_config["platform"] = pre_test_template.platform
|
||||
if pre_test_template.external_action:
|
||||
diagnose_config["external_action"] = pre_test_template.external_action
|
||||
if pre_test_template.cpa_bid:
|
||||
diagnose_config["cpa_bid"] = pre_test_template.cpa_bid
|
||||
if pre_test_template.audience_gender:
|
||||
diagnose_config["audience_gender"] = pre_test_template.audience_gender
|
||||
if pre_test_template.audience_age:
|
||||
diagnose_config["audience_age"] = json.loads(pre_test_template.audience_age)
|
||||
if pre_test_template.audience_region:
|
||||
diagnose_config["audience_region"] = json.loads(pre_test_template.audience_region)
|
||||
if pre_test_template.audience_network:
|
||||
diagnose_config["audience_network"] = json.loads(pre_test_template.audience_network)
|
||||
if pre_test_template.cus_name:
|
||||
diagnose_config["cus_name"] = pre_test_template.cus_name
|
||||
if pre_test_template.pricing_type:
|
||||
diagnose_config["pricing_type"] = pre_test_template.pricing_type
|
||||
if pre_test_template.cost_cap:
|
||||
diagnose_config["cost_cap"] = pre_test_template.cost_cap
|
||||
if pre_test_template.target_cost:
|
||||
diagnose_config["target_cost"] = pre_test_template.target_cost
|
||||
if pre_test_template.nobid:
|
||||
diagnose_config["nobid"] = pre_test_template.nobid
|
||||
if pre_test_template.cpc_bid:
|
||||
diagnose_config["cpc_bid"] = pre_test_template.cpc_bid
|
||||
if pre_test_template.budget:
|
||||
diagnose_config["budget"] = pre_test_template.budget
|
||||
|
||||
params = {
|
||||
"advertiser_id": int(advertiser_id),
|
||||
"video_ids": video_ids,
|
||||
"diagnose_config": diagnose_config,
|
||||
}
|
||||
response = await douyin_api.pre_test_material(oauth_id, params)
|
||||
|
||||
code = response.get("code", -1)
|
||||
|
||||
if code != 0:
|
||||
note = response.get("message", "未知错误")
|
||||
for video_id in video_ids:
|
||||
await _update_material_pre_test_status(
|
||||
db, oauth_id, advertiser_id, video_id,
|
||||
task_id=None,
|
||||
status="FAILED",
|
||||
note=note,
|
||||
pre_result=None,
|
||||
pre_test_template_id=pre_test_template_id,
|
||||
)
|
||||
await db.commit()
|
||||
return response
|
||||
|
||||
data = response.get("data", {})
|
||||
task_ids = data.get("task_ids", [])
|
||||
fail_video_ids = data.get("fail_video_ids", {})
|
||||
|
||||
success_count = 0
|
||||
for i, video_id in enumerate(video_ids):
|
||||
if video_id in fail_video_ids:
|
||||
fail_info = fail_video_ids[video_id]
|
||||
err_code = fail_info.get("err_code", "")
|
||||
err_message = fail_info.get("err_message", "未知错误")
|
||||
note = f"失败[{err_code}]: {err_message}"
|
||||
|
||||
await _update_material_pre_test_status(
|
||||
db, oauth_id, advertiser_id, video_id,
|
||||
task_id=None,
|
||||
status="FAILED",
|
||||
note=note,
|
||||
pre_result=None,
|
||||
pre_test_template_id=pre_test_template_id,
|
||||
)
|
||||
else:
|
||||
task_id = str(task_ids[success_count]) if success_count < len(task_ids) else None
|
||||
|
||||
await _update_material_pre_test_status(
|
||||
db, oauth_id, advertiser_id, video_id,
|
||||
task_id=task_id,
|
||||
status="PENDING",
|
||||
note="",
|
||||
pre_result=None,
|
||||
pre_test_template_id=pre_test_template_id,
|
||||
)
|
||||
success_count += 1
|
||||
|
||||
await db.commit()
|
||||
|
||||
return response
|
||||
|
||||
|
||||
async def _update_material_pre_test_status(
|
||||
db: AsyncSession,
|
||||
oauth_id: str,
|
||||
advertiser_id: str,
|
||||
upload_id: str,
|
||||
task_id: str | None,
|
||||
status: str,
|
||||
note: str,
|
||||
pre_result: str | None,
|
||||
pre_test_template_id: str | None,
|
||||
):
|
||||
await db.execute(
|
||||
update(ResourcesMaterial).where(
|
||||
ResourcesMaterial.oauth_id == oauth_id,
|
||||
ResourcesMaterial.advertiser_id == advertiser_id,
|
||||
ResourcesMaterial.upload_id == upload_id,
|
||||
ResourcesMaterial.deleted_at.is_(None),
|
||||
).values(
|
||||
task_id=task_id,
|
||||
status=status,
|
||||
note=note,
|
||||
pre_result=pre_result,
|
||||
pre_test_template_id=pre_test_template_id,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
upload_queue = UploadQueue()
|
||||
Reference in New Issue
Block a user