creative_creation.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. """创意搭建主入口(模块 B)。
  2. 数据流(2026-06-08 用户确认 + 端到端打通):
  3. 承接视频(piaoquantv) → 多路召回素材(vector) → top 1 → 上传素材图(MD5 幂等)
  4. → 调 xcx/save 注册落地计划(拿 page_url + creative_name)→ 读账户 brand
  5. → 构造 body → POST /dynamic_creatives/add → 绑到广告
  6. 决策落地(已验证):
  7. - image 组件:**必须用 image_id**(image_url 会 reject code=18001)
  8. → 实现:下载 cover URL → MD5 → POST /v3.0/images/add multipart → 拿 image_id
  9. → MD5 幂等:同账户重复上传返回同 ID(无需本地缓存)
  10. - brand 组件:**必填**(漏传会 reject code=1800269)
  11. → brand_image_id 跨账户不可复用(reject code=1530003)
  12. → 优先从 DB account_whitelist 读账户级 brand_name/brand_image_id
  13. → 若缺失,按 ad_creation_account_config.brand_key 读取 brand_asset_template,上传并写回账户
  14. - dynamic_creative_type: DYNAMIC_CREATIVE_TYPE_PROGRAM(参考样本 9744753978)
  15. - description: 素材 title 一条(MVP)
  16. - jump_info / 命名:**piaoquantv xcx/save 接口统一管**
  17. → page_url(mini_program_path)和 root_source_id(creative_name)都来自服务侧
  18. → 客户端不本地拼接、不本地命名,保证归因锚点跨系统一致
  19. - 配置 NORMAL 状态,挂上后腾讯进入 PENDING(普通素材审核 2-4 小时)
  20. """
  21. import logging
  22. import random
  23. from dataclasses import asdict
  24. from typing import Optional
  25. from config import (
  26. CREATIVE_DESCRIPTION_COUNT_PER_AD,
  27. CREATIVE_DESCRIPTION_POOL,
  28. LANDING_EXCLUDED_CATEGORIES,
  29. MARKETING_CARRIER_GH_ID,
  30. MAX_LANDING_ATTEMPTS_PER_AD,
  31. MAX_MATERIAL_PER_LANDING,
  32. TARGET_CREATIVES_PER_AD,
  33. )
  34. from tools.ad_api import _check, _get, _post, images_add
  35. from tools.landing_plan import LandingPlanResult, create_landing_plan
  36. from tools.material_recall import Material, recall_materials_for_video
  37. from tools.creative_material_usage import (
  38. load_recent_used_material_ids,
  39. merge_used_material_ids,
  40. )
  41. from tools.video_recall import (
  42. LandingVideo,
  43. PIAOQUANTV_HOT_FALLBACK_SOURCE,
  44. PIAOQUANTV_VIDEO_SOURCE,
  45. fetch_landing_videos_for_account,
  46. get_account_crowd_package,
  47. map_crowd_package_for_video_recall,
  48. )
  49. from tools.video_feature_query import fetch_video_element_features
  50. from tools.video_risk import VideoRiskResult, check_video_risk
  51. logger = logging.getLogger(__name__)
  52. def _landing_category_values(v: LandingVideo) -> set[str]:
  53. """解析承接视频的内容品类,返回去重后的品类标签集合。"""
  54. raw = v.category or ""
  55. return {part.strip() for part in raw.replace(",", ",").split(",") if part.strip()}
  56. def _is_landing_candidate(v: LandingVideo) -> bool:
  57. """承接视频基础预筛:内容品类不在黑名单。
  58. 素材召回特征统一通过 video_id 查 ODPS,不再依赖 videoContentList 返回的
  59. pointType / standardElement。
  60. """
  61. excluded = _landing_category_values(v) & LANDING_EXCLUDED_CATEGORIES
  62. if excluded:
  63. logger.info(
  64. "[creative_creation] landing=%d category=%r 命中排除品类,跳过",
  65. v.video_id, v.category,
  66. )
  67. return False
  68. return True
  69. def _pick_image_url(material: Material) -> str:
  70. """从素材里取 image URL — material.cover 优先,fallback 到 raw.imageList[0]"""
  71. if material.cover:
  72. return material.cover
  73. images = (material.raw or {}).get("imageList") or []
  74. return images[0] if images else ""
  75. def find_ads_needing_creatives(
  76. account_id: int,
  77. min_creatives: int = TARGET_CREATIVES_PER_AD,
  78. ) -> list[dict]:
  79. """扫描账户下"需要补创意"的广告(P0-B,2026-06-09)。
  80. 口径(2026-06-08 用户确认 = C):
  81. configured_status = AD_STATUS_NORMAL AND creative_count < min_creatives
  82. 实测发现:腾讯 /adgroups/get 不返回 creative_count 字段,
  83. 所以分两条路径取创意数:
  84. - 快路径:system_status = ADGROUP_STATUS_CREATIVE_EMPTY → creative_count = 0
  85. - 慢路径:其他状态 → 单独调 /dynamic_creatives/get 数总数(N+1,目前广告 ≤ 10 可接受)
  86. Args:
  87. account_id: 腾讯广告主账号 ID
  88. min_creatives: 默认读 config.TARGET_CREATIVES_PER_AD(测试=1,生产应回 15)
  89. — find 阈值 + 补量目标的同一变量,无矛盾
  90. Returns:
  91. [{adgroup_id, adgroup_name, system_status, creative_count}, ...]
  92. """
  93. # 注意:腾讯 filtering 对 configured_status IN 静默拒绝(实测 2026-06-09 加了 filtering 返回 0)
  94. # 改为不加 filtering 拉全部 + Python 内存过滤(单账户 ≤ 10 条广告,代价≈0)
  95. resp = _get("/adgroups/get", {
  96. "account_id": account_id, "page": 1, "page_size": 100,
  97. "fields": ["adgroup_id", "adgroup_name", "system_status", "configured_status"],
  98. })
  99. all_ads_raw = (resp.get("data") or {}).get("list") or []
  100. all_ads = [a for a in all_ads_raw if a.get("configured_status") == "AD_STATUS_NORMAL"]
  101. logger.info(
  102. "[find_ads_needing_creatives] account=%d 共 %d 条广告,%d 条 NORMAL",
  103. account_id, len(all_ads_raw), len(all_ads),
  104. )
  105. out = []
  106. for ad in all_ads:
  107. adgroup_id = ad["adgroup_id"]
  108. system_status = ad.get("system_status", "")
  109. adgroup_name = ad.get("adgroup_name", "")
  110. if system_status == "ADGROUP_STATUS_CREATIVE_EMPTY":
  111. creative_count = 0
  112. else:
  113. cresp = _get("/dynamic_creatives/get", {
  114. "account_id": account_id, "page": 1, "page_size": 100,
  115. "filtering": [{
  116. "field": "adgroup_id", "operator": "IN",
  117. "values": [str(adgroup_id)],
  118. }],
  119. "fields": ["dynamic_creative_id", "system_status"],
  120. })
  121. all_creatives = (cresp.get("data") or {}).get("list") or []
  122. # 2026-06-09:忽略 DENIED 状态(审核拒绝),让 find_ads 能重补
  123. # 其他状态(PENDING/ACTIVE/SUSPEND 等)都算"已挂"
  124. denied_count = sum(
  125. 1 for c in all_creatives
  126. if c.get("system_status") == "DYNAMIC_CREATIVE_STATUS_DENIED"
  127. )
  128. creative_count = len(all_creatives) - denied_count
  129. if denied_count:
  130. logger.info(
  131. "[find_ads_needing_creatives] adgroup=%d 总创意 %d 减去 %d 条 DENIED → 有效 %d",
  132. adgroup_id, len(all_creatives), denied_count, creative_count,
  133. )
  134. if creative_count < min_creatives:
  135. out.append({
  136. "adgroup_id": adgroup_id,
  137. "adgroup_name": adgroup_name,
  138. "system_status": system_status,
  139. "creative_count": creative_count,
  140. })
  141. logger.info(
  142. "[find_ads_needing_creatives] ✓ adgroup=%d name=%r creative_count=%d < %d",
  143. adgroup_id, adgroup_name, creative_count, min_creatives,
  144. )
  145. logger.info(
  146. "[find_ads_needing_creatives] account=%d 命中 %d 条需补创意",
  147. account_id, len(out),
  148. )
  149. return out
  150. def load_excluded_ad_ids_from_adjustment(
  151. date_str: Optional[str] = None,
  152. output_dir: str = "outputs/reports",
  153. ) -> set[int]:
  154. """读调控当日决策 CSV,提取被 pause 的 adgroup_id(P0-E 关联点过滤,2026-06-09)。
  155. 新建子系统启动时调用,排除调控当日已决策 pause 的广告,避免"调控刚关、新建又补创意"的冲突。
  156. Args:
  157. date_str: 形如 '20260609';None → 用昨天日期(调控 cron 02:00 UTC 跑完后)
  158. output_dir: 调控产物目录,相对当前工作目录
  159. Returns:
  160. 被 pause 的 adgroup_id 集合;文件不存在 / CSV 字段缺失时返回空集(降级,不阻塞)
  161. """
  162. import csv
  163. import glob
  164. import os
  165. from datetime import datetime, timedelta
  166. if date_str is None:
  167. date_str = (datetime.utcnow() - timedelta(days=1)).strftime("%Y%m%d")
  168. pattern = os.path.join(output_dir, f"llm_decisions_{date_str}*.csv")
  169. matches = sorted(glob.glob(pattern))
  170. if not matches:
  171. logger.warning(
  172. "[load_excluded_ad_ids] 未找到调控决策 CSV: %s,返回空排除集(不阻塞主循环)",
  173. pattern,
  174. )
  175. return set()
  176. latest = matches[-1]
  177. excluded: set[int] = set()
  178. with open(latest, newline="", encoding="utf-8") as f:
  179. reader = csv.DictReader(f)
  180. for row in reader:
  181. action = (row.get("action") or "").strip().lower()
  182. ad_id_raw = (row.get("ad_id") or row.get("adgroup_id") or "").strip()
  183. if action == "pause" and ad_id_raw.isdigit():
  184. excluded.add(int(ad_id_raw))
  185. logger.info(
  186. "[load_excluded_ad_ids] 从 %s 读出 %d 个 pause 广告(排除)",
  187. os.path.basename(latest), len(excluded),
  188. )
  189. return excluded
  190. def find_existing_creative_by_image(
  191. account_id: int, adgroup_id: int, material_image_id: str,
  192. ) -> Optional[int]:
  193. """查 adgroup 下是否已有创意挂了这个素材 image_id(幂等检查)。
  194. 判等键(2026-06-08 决策):**同 adgroup + 同 material_image_id**。
  195. - 不按 creative_name 判等:name = root_source_id,每次 xcx/save 都新,等于没做
  196. - 按 image_id 判等:同 image 在同 adgroup 下挂多条会被腾讯模型降权曝光
  197. Returns:
  198. 已有创意的 dynamic_creative_id;无则 None。
  199. """
  200. resp = _get("/dynamic_creatives/get", {
  201. "account_id": account_id, "page": 1, "page_size": 100,
  202. "filtering": [{
  203. "field": "adgroup_id", "operator": "IN", "values": [str(adgroup_id)],
  204. }],
  205. "fields": ["dynamic_creative_id", "creative_components"],
  206. })
  207. items = (resp.get("data") or {}).get("list") or []
  208. for c in items:
  209. images = (c.get("creative_components") or {}).get("image") or []
  210. for img in images:
  211. existing_id = ((img.get("value") or {}).get("image_id")) or ""
  212. if str(existing_id) == str(material_image_id):
  213. cid = int(c.get("dynamic_creative_id"))
  214. logger.info(
  215. "[idempotency] adgroup=%d image_id=%s 已挂创意 creative_id=%d,skip 新建",
  216. adgroup_id, material_image_id, cid,
  217. )
  218. return cid
  219. return None
  220. def get_account_brand(account_id: int) -> dict:
  221. """读取或初始化账户级 brand 资产。
  222. 返回 {"brand_name": str, "brand_image_id": str}。
  223. 跨账户 brand_image_id 不可复用,所以一定按 account_id 取。
  224. 如果 account_whitelist 还没有 brand_image_id,则按待投放配置的 brand_key
  225. 找统一品牌模板,上传模板 URL 到当前账户,并把新 image_id 写回 DB。
  226. """
  227. from db.connection import get_connection
  228. conn = get_connection()
  229. try:
  230. with conn.cursor() as cur:
  231. cur.execute(
  232. "SELECT brand_name, brand_image_id FROM account_whitelist WHERE account_id=%s",
  233. (account_id,),
  234. )
  235. row = cur.fetchone()
  236. if row and row.get("brand_name") and row.get("brand_image_id"):
  237. return {
  238. "brand_name": row["brand_name"],
  239. "brand_image_id": str(row["brand_image_id"]),
  240. }
  241. cur.execute(
  242. """
  243. SELECT b.brand_key, b.brand_name, b.brand_image_url
  244. FROM ad_creation_account_config c
  245. JOIN brand_asset_template b
  246. ON b.brand_key = c.brand_key
  247. AND b.enabled = TRUE
  248. WHERE c.account_id=%s
  249. AND c.enabled = TRUE
  250. """,
  251. (account_id,),
  252. )
  253. tpl = cur.fetchone()
  254. finally:
  255. conn.close()
  256. if not tpl or not tpl.get("brand_name") or not tpl.get("brand_image_url"):
  257. raise RuntimeError(
  258. f"account {account_id} 未配置可用品牌资产模板。"
  259. "请在 ad_creation_account_config.brand_key 关联 brand_asset_template"
  260. )
  261. brand_name = tpl["brand_name"]
  262. brand_image_url = tpl["brand_image_url"]
  263. logger.info(
  264. "[brand] account=%d 缺 brand_image_id,按模板 %s 上传品牌图",
  265. account_id, tpl.get("brand_key"),
  266. )
  267. brand_image_id = images_add(account_id, brand_image_url)
  268. conn = get_connection()
  269. try:
  270. with conn.cursor() as cur:
  271. cur.execute(
  272. """
  273. UPDATE account_whitelist
  274. SET brand_name=%s,
  275. brand_image_id=%s,
  276. updated_by=%s,
  277. updated_at=CURRENT_TIMESTAMP
  278. WHERE account_id=%s
  279. """,
  280. (brand_name, brand_image_id, "auto-brand-init", account_id),
  281. )
  282. conn.commit()
  283. finally:
  284. conn.close()
  285. logger.info(
  286. "[brand] account=%d 已初始化 brand_name=%s brand_image_id=%s",
  287. account_id, brand_name, brand_image_id,
  288. )
  289. return {
  290. "brand_name": brand_name,
  291. "brand_image_id": str(brand_image_id),
  292. }
  293. def build_creative_request_body(
  294. account_id: int,
  295. adgroup_id: int,
  296. landing: LandingVideo,
  297. material: Material,
  298. material_image_id: str,
  299. brand_name: str,
  300. brand_image_id: str,
  301. creative_name: str,
  302. jump_path: str,
  303. description_contents: Optional[list] = None,
  304. ) -> dict:
  305. """生成 /v3.0/dynamic_creatives/add 的请求 body(纯函数,无 I/O)。
  306. Args:
  307. material_image_id: 已上传到当前账户的素材图 image_id
  308. brand_name / brand_image_id: 账户级 brand 资产(已上传)
  309. creative_name: 由 xcx/save 返回的 root_source_id(归因锚点)
  310. jump_path: 由 xcx/save 返回的 page_url(小程序跳转路径)
  311. description_contents: 文案列表(可选);None 时自动 random.sample(用于审批表回显)
  312. """
  313. # 默认文案由配置控制;生产当前统一使用"打开看看"。
  314. if description_contents is None:
  315. description_count = min(CREATIVE_DESCRIPTION_COUNT_PER_AD, len(CREATIVE_DESCRIPTION_POOL))
  316. description_contents = CREATIVE_DESCRIPTION_POOL[:description_count]
  317. jump_spec = {
  318. "page_type": "PAGE_TYPE_WECHAT_MINI_PROGRAM",
  319. "page_spec": {
  320. "wechat_mini_program_spec": {
  321. "mini_program_id": MARKETING_CARRIER_GH_ID,
  322. "mini_program_path": jump_path,
  323. }
  324. },
  325. }
  326. return {
  327. "account_id": account_id,
  328. "adgroup_id": adgroup_id,
  329. "dynamic_creative_name": creative_name,
  330. "delivery_mode": "DELIVERY_MODE_COMPONENT",
  331. "dynamic_creative_type": "DYNAMIC_CREATIVE_TYPE_PROGRAM",
  332. "configured_status": "AD_STATUS_NORMAL",
  333. "creative_components": {
  334. "description": [{"value": {"content": d}} for d in description_contents],
  335. "image": [{"value": {"image_id": material_image_id}}],
  336. "brand": [{"value": {
  337. "brand_name": brand_name,
  338. "brand_image_id": brand_image_id,
  339. }}],
  340. "action_button": [{"value": {"button_text": "查看详情"}}],
  341. "jump_info": [{"value": jump_spec}],
  342. "main_jump_info": [{"value": jump_spec}],
  343. },
  344. }
  345. def _pick_landing_and_materials(account_id: int) -> tuple[LandingVideo, list[Material]]:
  346. """召回链:承接视频 → 多路素材召回 → 取第一个有素材命中的 landing 及其 top 素材。
  347. 出口:确保 landing.point_type+standard_element 都有值(否则多路召回会全 skip)。
  348. """
  349. videos = fetch_landing_videos_for_account(account_id, page_size=50)
  350. valid = [v for v in videos if _is_landing_candidate(v)]
  351. if not valid:
  352. raise RuntimeError(
  353. f"account={account_id} 无 pointType+standardElement 都有值的承接视频"
  354. )
  355. for v in valid:
  356. risk = check_video_risk(v.video_id)
  357. if not risk.passed:
  358. logger.warning(
  359. "[creative_creation] landing=%d 风险拦截:%s",
  360. v.video_id, risk.reason,
  361. )
  362. continue
  363. materials = recall_materials_for_video(v, final_top_n=5)
  364. if materials:
  365. return v, materials
  366. raise RuntimeError(
  367. f"account={account_id} 前 {len(valid)} 条承接视频都召回 0 素材"
  368. )
  369. def preview_for_account(account_id: int, adgroup_id: int) -> dict:
  370. """承接视频 → 召回素材 → top 1 → 上传素材图 → 注册落地计划 → 读 brand → 构造 body。
  371. **会真实调:**
  372. - 腾讯 /images/add(MD5 幂等)
  373. - piaoquantv xcx/save(每次新建一条计划)
  374. body 是真实可投放的(只差 POST /dynamic_creatives/add)。
  375. """
  376. landing, materials = _pick_landing_and_materials(account_id)
  377. top_material = materials[0]
  378. image_url = _pick_image_url(top_material)
  379. if not image_url:
  380. raise ValueError(
  381. f"素材 {top_material.material_id} 既无 cover 也无 imageList,无法构造 image 组件"
  382. )
  383. material_image_id = images_add(account_id, image_url)
  384. crowd_package = get_account_crowd_package(account_id)
  385. plan = create_landing_plan(crowd_package, landing)
  386. brand = get_account_brand(account_id)
  387. body = build_creative_request_body(
  388. account_id=account_id, adgroup_id=adgroup_id,
  389. landing=landing, material=top_material,
  390. material_image_id=material_image_id,
  391. brand_name=brand["brand_name"],
  392. brand_image_id=brand["brand_image_id"],
  393. creative_name=plan.root_source_id,
  394. jump_path=plan.page_url,
  395. )
  396. return {
  397. "landing": asdict(landing),
  398. "material": asdict(top_material),
  399. "material_image_id": material_image_id,
  400. "brand": brand,
  401. "landing_plan": asdict(plan),
  402. "body": body,
  403. }
  404. def create_creative_for_ad(
  405. account_id: int, adgroup_id: int,
  406. landing: LandingVideo, material: Material,
  407. skip_if_exists: bool = True,
  408. ) -> int:
  409. """编排:上传素材图 → 幂等检查 → 注册落地计划 → 读 brand → build body → POST。
  410. Args:
  411. skip_if_exists: True(默认) — 同 adgroup + 同 material_image_id 已有创意时直接返回已有 ID,
  412. 跳过 xcx/save 注册和腾讯 POST,节省审核额度 + 避免模型降权。
  413. False — 强制新建(用于 A/B 测试不同归因锚点的场景)。
  414. Returns:
  415. dynamic_creative_id(新建或已有)
  416. """
  417. image_url = _pick_image_url(material)
  418. if not image_url:
  419. raise ValueError(
  420. f"素材 {material.material_id} 既无 cover 也无 imageList,无法构造 image 组件"
  421. )
  422. material_image_id = images_add(account_id, image_url)
  423. if skip_if_exists:
  424. existing_cid = find_existing_creative_by_image(account_id, adgroup_id, material_image_id)
  425. if existing_cid:
  426. return existing_cid
  427. crowd_package = get_account_crowd_package(account_id)
  428. plan = create_landing_plan(crowd_package, landing)
  429. brand = get_account_brand(account_id)
  430. body = build_creative_request_body(
  431. account_id, adgroup_id, landing, material,
  432. material_image_id=material_image_id,
  433. brand_name=brand["brand_name"],
  434. brand_image_id=brand["brand_image_id"],
  435. creative_name=plan.root_source_id,
  436. jump_path=plan.page_url,
  437. )
  438. logger.info(
  439. "[creative_creation] POST /dynamic_creatives/add adgroup=%d name=%s image_id=%s plan_id=%d",
  440. adgroup_id, body["dynamic_creative_name"], material_image_id, plan.plan_id,
  441. )
  442. resp = _post("/dynamic_creatives/add", body)
  443. data = _check(resp, "creative_create")
  444. return data.get("dynamic_creative_id")
  445. def prepare_one_creative_for_ad(
  446. account_id: int,
  447. adgroup_id: int,
  448. excluded_material_ids: Optional[set] = None,
  449. excluded_landing_ids: Optional[set] = None,
  450. max_landings: int = MAX_LANDING_ATTEMPTS_PER_AD,
  451. max_materials_per_landing: int = MAX_MATERIAL_PER_LANDING,
  452. ) -> Optional[dict]:
  453. """Phase 1 准备:召回 + 上传图 + xcx/save + build body → 返回 pending record。
  454. **不 POST 腾讯**。POST 行为留给 Phase 3(供"先审后挂"流程审批后才挂)。
  455. 跟 try_create_one_creative_with_fallback 的差异:
  456. - 不做 POST-based fallback(因为不 POST)
  457. - 信任黑名单 + 选 top 1 material(`EXCLUDED_COVER_URL_PATTERNS` 已经截掉尺寸不符的)
  458. - 如果 Phase 3 POST 失败(腾讯尺寸 reject 等),由 Phase 3 记 error,下轮主循环重试
  459. Args:
  460. excluded_material_ids: 同广告内已挂的 material_id 集合(2026-06-09 N=3 时去重必需)。
  461. 召回后过滤掉这些,避免同广告挂重复素材被腾讯模型降权曝光。
  462. excluded_landing_ids: 兼容旧调用保留,当前不再使用。落地页视频允许重复。
  463. Returns:
  464. pending record dict(飞书表格字段 + Phase 3 POST 用的完整 body + 元数据);
  465. 所有 landing 都召回 0 素材时返回 None。
  466. """
  467. from datetime import datetime, timezone
  468. excluded_material_ids = excluded_material_ids or set()
  469. excluded_landing_ids = excluded_landing_ids or set()
  470. crowd_package = get_account_crowd_package(account_id)
  471. video_crowd_package = map_crowd_package_for_video_recall(crowd_package)
  472. try:
  473. recent_material_ids = load_recent_used_material_ids(crowd_package)
  474. except Exception as e:
  475. logger.warning(
  476. "[prepare_one_creative] account=%d crowd=%r 读取素材使用历史失败,仅使用本轮排重:%s",
  477. account_id, crowd_package, e,
  478. )
  479. recent_material_ids = set()
  480. effective_excluded_material_ids = merge_used_material_ids(
  481. excluded_material_ids,
  482. recent_material_ids,
  483. )
  484. logger.info(
  485. "[prepare_one_creative] account=%d adgroup=%d crowd=%r video_crowd=%r material_dedupe run=%d recent=%d effective=%d",
  486. account_id, adgroup_id, crowd_package, video_crowd_package,
  487. len(excluded_material_ids), len(recent_material_ids),
  488. len(effective_excluded_material_ids),
  489. )
  490. chosen_landing = None
  491. chosen_material = None
  492. chosen_risk: Optional[VideoRiskResult] = None
  493. chosen_landing_source = ""
  494. # 每个来源最多拉 100 条视频。先主池,主池产不出可用创意时再用同人群包 hot 兜底。
  495. source_plan = [("primary", PIAOQUANTV_VIDEO_SOURCE)]
  496. if PIAOQUANTV_HOT_FALLBACK_SOURCE != PIAOQUANTV_VIDEO_SOURCE:
  497. source_plan.append(("hot", PIAOQUANTV_HOT_FALLBACK_SOURCE))
  498. for source_label, source in source_plan:
  499. videos = fetch_landing_videos_for_account(
  500. account_id,
  501. page_size=100,
  502. source=source,
  503. enable_hot_fallback=False,
  504. )
  505. valid = [v for v in videos if _is_landing_candidate(v)]
  506. features_by_vid = fetch_video_element_features(v.video_id for v in valid)
  507. logger.info(
  508. "[prepare_one_creative] account=%d adgroup=%d source=%s valid landing=%d/100 feature_videos=%d feature_rows=%d excl_mat=%d",
  509. account_id, adgroup_id, source_label, len(valid),
  510. len(features_by_vid), sum(len(v) for v in features_by_vid.values()),
  511. len(effective_excluded_material_ids),
  512. )
  513. attempts = 0
  514. for v in valid:
  515. if v.video_id in excluded_landing_ids:
  516. logger.info(
  517. "[prepare_one_creative] landing=%d 本轮同广告 landing 限频,跳过",
  518. v.video_id,
  519. )
  520. continue
  521. attempts += 1
  522. if attempts > max_landings:
  523. break
  524. # 2026-06-29:承接视频风险审核。必须在素材召回 / xcx-save 前完成,
  525. # 避免高风险 landing 继续产生 plan/rootSourceId 等外部副作用。
  526. risk = check_video_risk(v.video_id)
  527. if not risk.passed:
  528. logger.warning(
  529. "[prepare_one_creative] landing=%d 风险拦截:%s",
  530. v.video_id, risk.reason,
  531. )
  532. continue
  533. element_features = features_by_vid.get(v.video_id) or []
  534. if not element_features:
  535. logger.info(
  536. "[prepare_one_creative] landing=%d 无 ODPS 召回特征,跳过",
  537. v.video_id,
  538. )
  539. continue
  540. materials = recall_materials_for_video(
  541. v,
  542. final_top_n=max_materials_per_landing,
  543. element_features=element_features,
  544. )
  545. # material_id 去重(2026-06-09):跳过已用素材(账户层 set,跨广告也共享)
  546. fresh = [
  547. m for m in materials
  548. if m.material_id not in effective_excluded_material_ids
  549. ]
  550. if fresh:
  551. chosen_landing = v
  552. chosen_material = fresh[0]
  553. chosen_risk = risk
  554. chosen_landing_source = source_label
  555. logger.info(
  556. "[prepare_one_creative] 选中 landing=%d source=%s category=%r material=%s recall=%s/%s/%s cost=%s roi=%s ctr=%s imp=%s score=%s policy=score>=0.8,cost_desc",
  557. v.video_id, source_label, v.category,
  558. chosen_material.material_id,
  559. chosen_material.recall_element_dimension,
  560. chosen_material.recall_point_type,
  561. chosen_material.recall_standard_element,
  562. chosen_material.cost,
  563. chosen_material.roi,
  564. chosen_material.ctr,
  565. chosen_material.impressions,
  566. chosen_material.score,
  567. )
  568. break
  569. if materials:
  570. logger.info(
  571. "[prepare_one_creative] landing=%d 召回 %d 全在 excluded,试下一条",
  572. v.video_id, len(materials),
  573. )
  574. if chosen_landing and chosen_material:
  575. break
  576. logger.info(
  577. "[prepare_one_creative] account=%d adgroup=%d source=%s 未产出可用创意",
  578. account_id, adgroup_id, source_label,
  579. )
  580. if not chosen_landing or not chosen_material:
  581. logger.error(
  582. "[prepare_one_creative] account=%d adgroup=%d 穷尽 landing 后无可用素材(excluded=%d)",
  583. account_id, adgroup_id, len(effective_excluded_material_ids),
  584. )
  585. return None
  586. image_url = _pick_image_url(chosen_material)
  587. if not image_url:
  588. logger.error(
  589. "[prepare_one_creative] material=%s 既无 cover 也无 imageList,放弃",
  590. chosen_material.material_id,
  591. )
  592. return None
  593. # 上传素材图(MD5 幂等)
  594. material_image_id = images_add(account_id, image_url)
  595. # 注册落地计划(xcx/save)
  596. plan = create_landing_plan(crowd_package, chosen_landing)
  597. # 读账户 brand
  598. brand = get_account_brand(account_id)
  599. # 先确定文案,再传给 build_body — 这样 record 跟 body 文案一致。
  600. desc_count = min(CREATIVE_DESCRIPTION_COUNT_PER_AD, len(CREATIVE_DESCRIPTION_POOL))
  601. description_contents = CREATIVE_DESCRIPTION_POOL[:desc_count]
  602. # 构造完整 POST body(Phase 3 直接用)
  603. body = build_creative_request_body(
  604. account_id, adgroup_id, chosen_landing, chosen_material,
  605. material_image_id=material_image_id,
  606. brand_name=brand["brand_name"],
  607. brand_image_id=brand["brand_image_id"],
  608. creative_name=plan.root_source_id,
  609. jump_path=plan.page_url,
  610. description_contents=description_contents,
  611. )
  612. # 反查广告 metadata(飞书表格 B 组用)
  613. ad_info = _fetch_ad_metadata_for_approval(account_id, adgroup_id)
  614. today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
  615. rec = {
  616. # === 飞书表格字段(im_approval_creation 用)===
  617. "approval_date": today,
  618. "account_id": account_id,
  619. "audience_tier": crowd_package,
  620. "video_crowd_package": video_crowd_package,
  621. "adgroup_id": adgroup_id,
  622. "adgroup_name": ad_info.get("adgroup_name", ""),
  623. "bid_amount_yuan": ad_info.get("bid_amount_yuan", ""),
  624. "site_set": ad_info.get("site_set", ""),
  625. "age_range": ad_info.get("age_range", ""),
  626. "landing_video_id": chosen_landing.video_id,
  627. "landing_video_url": chosen_landing.video_url,
  628. "landing_title": chosen_landing.title,
  629. "landing_source": chosen_landing_source,
  630. "landing_category": chosen_landing.category,
  631. "material_cover_url": chosen_material.cover,
  632. # 素材质量字段(2026-06-10 batchByText 升级 — 给 Task 25 飞书表展示用)
  633. "material_ctr": chosen_material.ctr,
  634. "material_cost": chosen_material.cost,
  635. "material_roi": chosen_material.roi,
  636. "material_impressions": chosen_material.impressions,
  637. "material_quality_score": chosen_material.quality_score,
  638. "material_score": chosen_material.score,
  639. "material_selection_policy": "score>=0.8,cost_desc",
  640. "material_recall_strategy": chosen_material.recall_strategy,
  641. "material_recall_query_text": chosen_material.recall_query_text,
  642. "material_recall_config_code": chosen_material.recall_config_code,
  643. "material_recall_element_dimension": chosen_material.recall_element_dimension,
  644. "material_recall_point_type": chosen_material.recall_point_type,
  645. "material_recall_standard_element": chosen_material.recall_standard_element,
  646. "material_recall_hit_queries": chosen_material.recall_hit_queries,
  647. "material_dedupe_recent_count": len(recent_material_ids),
  648. "material_dedupe_run_count": len(excluded_material_ids),
  649. "material_dedupe_effective_count": len(effective_excluded_material_ids),
  650. "landing_dedupe_run_count": len(excluded_landing_ids),
  651. "creative_name": plan.root_source_id,
  652. # === Phase 3 POST 用 ===
  653. "_request_body": body,
  654. # === 追溯元数据(写 summary JSON)===
  655. "_material_id": chosen_material.material_id,
  656. "_material_image_id": material_image_id,
  657. "_plan_id": plan.plan_id,
  658. "_experiment_id": chosen_landing.experiment_id,
  659. "_brand_image_id": brand["brand_image_id"],
  660. # 文案选择回显(飞书表格"创意文案"列要展示)
  661. "_description_contents": description_contents,
  662. }
  663. if chosen_risk is not None:
  664. rec.update(chosen_risk.to_record_fields())
  665. return rec
  666. def _fetch_ad_metadata_for_approval(account_id: int, adgroup_id: int) -> dict:
  667. """反查广告,为飞书审批表组装 B 组(广告维度)字段。"""
  668. resp = _get("/adgroups/get", {
  669. "account_id": account_id, "page": 1, "page_size": 1,
  670. "filtering": [{
  671. "field": "adgroup_id", "operator": "IN", "values": [str(adgroup_id)],
  672. }],
  673. "fields": [
  674. "adgroup_id", "adgroup_name",
  675. "bid_amount", "site_set", "targeting",
  676. ],
  677. })
  678. items = (resp.get("data") or {}).get("list") or []
  679. if not items:
  680. return {}
  681. ad = items[0]
  682. bid_fen = ad.get("bid_amount")
  683. bid_yuan = f"{int(bid_fen) / 100:.2f}" if bid_fen is not None else ""
  684. site_set_cn_map = {
  685. "SITE_SET_WECHAT": "微信公众号",
  686. "SITE_SET_WECHAT_PLUGIN": "微信插件",
  687. "SITE_SET_SEARCH_SCENE": "搜索场景",
  688. "SITE_SET_MOMENTS": "朋友圈",
  689. "SITE_SET_MINI_GAME_WECHAT": "小游戏",
  690. "SITE_SET_MINI_PROGRAM_WECHAT": "小程序",
  691. }
  692. site_raw = ad.get("site_set") or []
  693. if isinstance(site_raw, str):
  694. site_str = site_set_cn_map.get(site_raw, site_raw)
  695. else:
  696. site_str = ",".join(site_set_cn_map.get(s, s) for s in site_raw)
  697. targeting = ad.get("targeting") or {}
  698. age_list = targeting.get("age") or []
  699. if age_list:
  700. age_str = ",".join(f"{a.get('min', '?')}-{a.get('max', '?')}" for a in age_list)
  701. else:
  702. age_str = "不限"
  703. return {
  704. "adgroup_name": ad.get("adgroup_name", ""),
  705. "bid_amount_yuan": bid_yuan,
  706. "site_set": site_str,
  707. "age_range": age_str,
  708. }
  709. # Task 28:永久业务错误码(重试无意义,同一 body 永远报同样错)
  710. # 实测/文档来源:
  711. # 1801159 — 图片尺寸/格式不符
  712. # 1801143 — 创意素材审核拒绝
  713. # 1800269 — 品牌字段缺失/不合规
  714. # 1801118 — 视频 / 图片资产 ID 无效
  715. # 1901634 — 唯一性 reject(同营销内容广告组已存在相同创意)
  716. # 1901589 — 相似性 reject
  717. # 33001 — 参数校验失败
  718. _PERMANENT_ERROR_CODES = ("1801159", "1801143", "1800269", "1801118", "1901634", "1901589", "33001")
  719. def post_creative_with_prepared_body(
  720. account_id: int,
  721. body: dict,
  722. skip_if_exists: bool = True,
  723. max_retries: int = 2,
  724. ) -> Optional[int]:
  725. """Phase 3 POST:用 Phase 1 准备好的 body 调腾讯 /dynamic_creatives/add。
  726. Task 28(2026-06-11):加重试机制,区分瞬时 / 永久错误。
  727. - 永久业务错误(图尺寸 / 品牌缺失 / 唯一性 / 33001 等)→ 直接返回 None,不重试
  728. - 瞬时错误(网络超时 / 5xx / QPS limit)→ 最多重试 max_retries 次,指数退避
  729. Args:
  730. account_id: 腾讯账户 ID(用于幂等检查)
  731. body: Phase 1 prepare_one_creative_for_ad 返回的 _request_body
  732. skip_if_exists: True(默认)POST 前做幂等检查,命中返回已有 cid
  733. max_retries: 瞬时错误的最大重试次数(默认 2;首次 + 2 次重试 = 最坏 3 次总尝试)
  734. Returns:
  735. 成功 dynamic_creative_id;失败 None(记 error log)
  736. """
  737. import time
  738. adgroup_id = body.get("adgroup_id")
  739. image_comp = body.get("creative_components", {}).get("image") or []
  740. material_image_id = ""
  741. if image_comp:
  742. material_image_id = (image_comp[0].get("value") or {}).get("image_id", "")
  743. if skip_if_exists and material_image_id and adgroup_id:
  744. existing_cid = find_existing_creative_by_image(
  745. account_id, int(adgroup_id), str(material_image_id),
  746. )
  747. if existing_cid:
  748. return existing_cid
  749. for attempt in range(max_retries + 1): # 首次 + 重试
  750. try:
  751. logger.info(
  752. "[post_creative] account=%d adgroup=%s name=%s image_id=%s attempt=%d/%d",
  753. account_id, adgroup_id,
  754. body.get("dynamic_creative_name", ""), material_image_id,
  755. attempt + 1, max_retries + 1,
  756. )
  757. resp = _post("/dynamic_creatives/add", body)
  758. data = _check(resp, "creative_create")
  759. cid = data.get("dynamic_creative_id")
  760. if cid:
  761. logger.info("[post_creative] 成功 cid=%s", cid)
  762. return cid
  763. logger.error("[post_creative] 返回 data 缺 dynamic_creative_id: %s", data)
  764. return None
  765. except RuntimeError as e:
  766. err = str(e)
  767. # 永久错误 → 不重试,直接放弃(节省 quota,避免无意义打)
  768. is_permanent = any(code in err for code in _PERMANENT_ERROR_CODES)
  769. if is_permanent:
  770. logger.error(
  771. "[post_creative] 永久错误,不重试 account=%d adgroup=%s: %s",
  772. account_id, adgroup_id, err[:200],
  773. )
  774. return None
  775. # 瞬时错误:重试 quota 没用完 → backoff 后重试
  776. if attempt < max_retries:
  777. backoff_s = 5 * (2 ** attempt) # 5s, 10s
  778. logger.warning(
  779. "[post_creative] 瞬时错误 attempt=%d/%d,sleep %ds 后重试 account=%d adgroup=%s: %s",
  780. attempt + 1, max_retries + 1, backoff_s,
  781. account_id, adgroup_id, err[:150],
  782. )
  783. time.sleep(backoff_s)
  784. continue
  785. # 重试用完 → 放弃
  786. logger.error(
  787. "[post_creative] 重试 %d 次后仍失败 account=%d adgroup=%s: %s",
  788. max_retries, account_id, adgroup_id, err[:200],
  789. )
  790. return None
  791. return None
  792. def try_create_one_creative_with_fallback(
  793. account_id: int,
  794. adgroup_id: int,
  795. max_landings: int = MAX_LANDING_ATTEMPTS_PER_AD,
  796. max_materials_per_landing: int = MAX_MATERIAL_PER_LANDING,
  797. ) -> Optional[int]:
  798. """对一条广告挂 1 条创意,带 try-fallback 鲁棒性(P0-A 核心 helper,2026-06-09)。
  799. 召回的素材不一定都能挂(实测 code=1801159 尺寸不符,1530003 等),
  800. 所以遍历 landing × material 尝试 POST,失败就试下一条,直到成功或穷尽。
  801. Args:
  802. account_id: 腾讯广告主账号 ID
  803. adgroup_id: 目标广告 ID
  804. max_landings: 最多尝试的 landing 数
  805. max_materials_per_landing: 每条 landing 召回 top N 素材尝试
  806. Returns:
  807. 成功:dynamic_creative_id;穷尽:None(记 error log,主循环继续下一广告)
  808. """
  809. videos = fetch_landing_videos_for_account(account_id, page_size=max_landings * 2)
  810. valid = [v for v in videos if _is_landing_candidate(v)][:max_landings]
  811. logger.info(
  812. "[try_create_one_creative] account=%d adgroup=%d 候选 landing=%d",
  813. account_id, adgroup_id, len(valid),
  814. )
  815. attempts = []
  816. for v in valid:
  817. materials = recall_materials_for_video(v, final_top_n=max_materials_per_landing)
  818. if not materials:
  819. attempts.append(f" · landing={v.video_id} 召回 0 素材,跳过")
  820. continue
  821. for m in materials:
  822. try:
  823. cid = create_creative_for_ad(account_id, adgroup_id, v, m)
  824. attempts.append(
  825. f" · landing={v.video_id} material={m.material_id[:12]} → 成功 cid={cid}"
  826. )
  827. logger.info("[try_create_one_creative] 成功 cid=%s\n%s", cid, "\n".join(attempts))
  828. return cid
  829. except RuntimeError as e:
  830. err = str(e)[:100]
  831. attempts.append(f" · landing={v.video_id} material={m.material_id[:12]} → 失败 {err}")
  832. continue
  833. logger.error(
  834. "[try_create_one_creative] account=%d adgroup=%d 穷尽所有 landing×material 仍失败:\n%s",
  835. account_id, adgroup_id, "\n".join(attempts),
  836. )
  837. return None