creative_creation.py 38 KB

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