creative_creation.py 30 KB

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