creative_creation.py 34 KB

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