execute_creation_once.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. """模块 B 创意搭建子系统 — 主循环入口(P0-A / P0-C / P0-E,2026-06-09)。
  2. 数据流(三阶段,先审后挂):
  3. Phase 1 — 准备:
  4. 扫账户 → 关联点过滤(读调控当日 latest_decisions 排除 pause)
  5. → find_ads_needing_creatives
  6. → 对每条广告 prepare_one_creative_for_ad(召回 + 上传图 + xcx/save + build body)
  7. → pending_records (含完整 Phase 3 用的 _request_body)
  8. → 写 outputs/data/creation_pending_{date}.json(追溯 + 独立 apply 用)
  9. Phase 2 — 审批(若 CREATION_APPROVAL_REQUIRED=True):
  10. run_approval_workflow 生成 20 列 xlsx → 上传飞书 sheet → 发链接 → 轮询读决策列
  11. actions {row_idx: approve/reject/hold}
  12. → 写回 records["action"]
  13. Phase 3 — 执行:
  14. 对 action=approve 的 → POST /dynamic_creatives/add
  15. → 写 outputs/data/creation_run_{date}.json
  16. → 发飞书"执行汇报"消息
  17. 开关 CREATION_APPROVAL_REQUIRED=False 时跳过 Phase 2,全 records 直接 approve。
  18. """
  19. import json
  20. import logging
  21. import os
  22. import sys
  23. import time
  24. from pathlib import Path
  25. _HERE = Path(__file__).parent
  26. sys.path.insert(0, str(_HERE.parent.parent))
  27. sys.path.insert(0, str(_HERE))
  28. from dotenv import load_dotenv # noqa: E402
  29. load_dotenv(_HERE / ".env")
  30. from config import ( # noqa: E402
  31. ADS_PER_ACCOUNT,
  32. CREATION_APPROVAL_REQUIRED,
  33. CREATION_APPROVAL_TIMEOUT_MINUTES,
  34. MAX_SAME_LANDING_PER_AD_IN_RUN,
  35. TARGET_CREATIVES_PER_AD,
  36. WHITELIST_ACCOUNTS,
  37. get_creation_account_ids,
  38. now_in_timezone,
  39. )
  40. # config import 副作用会改 sys.path(把 im-client / agent/tools/builtin 顶到前面),
  41. # 强占 sys.path[0],只影响本入口自己的 import resolution。
  42. while str(_HERE) in sys.path:
  43. sys.path.remove(str(_HERE))
  44. sys.path.insert(0, str(_HERE))
  45. from tools.ad_creation import ( # noqa: E402
  46. build_ad_request_body,
  47. compute_fingerprint,
  48. enumerate_new_ad_candidates,
  49. post_ad_with_prepared_body,
  50. )
  51. from tools.audience_grant import ensure_account_audience_grant # noqa: E402
  52. from tools.creative_creation import ( # noqa: E402
  53. find_ads_needing_creatives,
  54. load_excluded_ad_ids_from_adjustment,
  55. prepare_one_creative_for_ad,
  56. )
  57. from tools.creative_material_usage import ( # noqa: E402
  58. load_recent_landing_usage_counts,
  59. load_recoverable_prepared_records,
  60. record_prepared_material_usage,
  61. )
  62. from tools.account_material_strategy import ( # noqa: E402
  63. MATERIAL_SOURCE_AI_GENERATED,
  64. load_account_material_strategy,
  65. )
  66. from tools.video_recall import get_account_crowd_package # noqa: E402
  67. from execute_creation_apply import ( # noqa: E402
  68. apply_pending_records,
  69. write_summary as write_apply_summary,
  70. _send_apply_summary_to_feishu,
  71. )
  72. logger = logging.getLogger("execute_creation_once")
  73. MATERIAL_SOURCE_HISTORY = "history"
  74. def _env_flag(name: str, default: bool = False) -> bool:
  75. raw = os.getenv(name)
  76. if raw is None:
  77. return default
  78. return raw.strip().lower() in {"1", "true", "yes", "y", "on"}
  79. def _env_csv_set(name: str) -> set[str]:
  80. return {
  81. item.strip()
  82. for item in os.getenv(name, "").split(",")
  83. if item.strip()
  84. }
  85. def _filter_creation_accounts(accounts: list[int], phase: str) -> list[int]:
  86. """Apply one-off run filters. Empty env means production default: no filter."""
  87. account_filter = {
  88. int(v)
  89. for v in _env_csv_set("CREATION_ONLY_ACCOUNT_IDS")
  90. if v.isdigit()
  91. }
  92. crowd_filter = _env_csv_set("CREATION_ONLY_CROWD_PACKAGES")
  93. if not account_filter and not crowd_filter:
  94. return accounts
  95. selected: list[int] = []
  96. for account_id in accounts:
  97. if account_filter and account_id not in account_filter:
  98. continue
  99. if crowd_filter:
  100. try:
  101. crowd_package = get_account_crowd_package(account_id)
  102. except Exception as e:
  103. logger.warning(
  104. "[%s] account=%d 临时过滤读取 crowd_package 失败,跳过:%s",
  105. phase, account_id, e,
  106. )
  107. continue
  108. if crowd_package not in crowd_filter:
  109. continue
  110. selected.append(account_id)
  111. logger.info(
  112. "[%s] 临时运行过滤 account_ids=%s crowd_packages=%s: %d -> %d",
  113. phase,
  114. sorted(account_filter) if account_filter else "ALL",
  115. sorted(crowd_filter) if crowd_filter else "ALL",
  116. len(accounts),
  117. len(selected),
  118. )
  119. return selected
  120. def _setup_logging() -> None:
  121. logging.basicConfig(
  122. level=logging.INFO,
  123. format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
  124. datefmt="%H:%M:%S",
  125. )
  126. for noisy in ("httpx", "httpcore", "config", "db.config"):
  127. logging.getLogger(noisy).setLevel(logging.WARNING)
  128. # SLS 上报(2026-06-11 接入,K8s pod 直发)— 配置缺失自动降级,不阻塞主链路
  129. try:
  130. from tools.sls_setup import attach_sls_handler
  131. attach_sls_handler()
  132. except Exception as e:
  133. logger.warning("[sls] 挂载异常(降级为本地 only):%s", e)
  134. def _fetch_existing_fingerprints_for_account(account_id: int) -> set[str]:
  135. """Task 27:拉账户下所有非 DELETED 广告 → 反推 fingerprint 集合(预校验唯一性)。
  136. 腾讯文档:NORMAL + SUSPEND 状态广告均占用唯一性槽位,DENIED 也算占用。
  137. list 接口默认返回所有非 DELETED 状态 ad,故无需显式 filter status。
  138. 若腾讯 list 接口某些字段不返回(field 实测差异),降级:跳过该条 + log 警告。
  139. 返回空集 → 上层退化为"不预校验",enumerate 仍能跑(原有行为)。
  140. """
  141. from tools.ad_api import _get
  142. fingerprints: set[str] = set()
  143. skipped = 0
  144. page = 1
  145. try:
  146. while True:
  147. r = _get("/adgroups/get", {
  148. "account_id": account_id, "page": page, "page_size": 100,
  149. "fields": [
  150. "adgroup_id", "configured_status", "site_set",
  151. "targeting", "scene_spec",
  152. ],
  153. })
  154. ads = (r.get("data") or {}).get("list") or []
  155. if not ads:
  156. break
  157. for ad in ads:
  158. tgt = ad.get("targeting") or {}
  159. sc = ad.get("scene_spec") or {}
  160. wp = sc.get("wechat_position") if sc else None
  161. site_set = ad.get("site_set") or []
  162. age = tgt.get("age") or []
  163. geo_regions = (tgt.get("geo_location") or {}).get("regions") or []
  164. custom_audience = tgt.get("custom_audience")
  165. if not site_set or not age:
  166. skipped += 1
  167. continue
  168. try:
  169. fp = compute_fingerprint(
  170. account_id=account_id,
  171. site_set=site_set,
  172. custom_audience=custom_audience,
  173. age=age,
  174. geo_regions=geo_regions,
  175. wechat_position=wp,
  176. )
  177. fingerprints.add(fp)
  178. except Exception as e:
  179. skipped += 1
  180. logger.debug(
  181. "[phase0] fingerprint 算失败 ad=%s: %s",
  182. ad.get("adgroup_id"), e,
  183. )
  184. if len(ads) < 100:
  185. break
  186. page += 1
  187. except Exception as e:
  188. logger.warning(
  189. "[phase0] account=%d 拉 fingerprint 集失败(降级为不预校验): %s",
  190. account_id, e,
  191. )
  192. return set()
  193. logger.info(
  194. "[phase0] 已存在 fingerprint=%d 个(skip 缺字段广告 %d 条)",
  195. len(fingerprints), skipped,
  196. )
  197. return fingerprints
  198. def phase0_create_ads(target_ads: int = ADS_PER_ACCOUNT) -> list[dict]:
  199. """Phase 0:对每账户检查广告数,不足则建到 target_ads 条(模块 A,2026-06-09 P1-G)。
  200. 流程:
  201. account → 查当前广告数(NORMAL+SUSPEND 算占用)→ 不足 target_ads →
  202. → 反查 fingerprint 集 → enumerate candidates(排除已有 fp)→
  203. → 飞书审批 → approve 项 → 真 POST /adgroups/add → 返回新建的 adgroup_id 列表
  204. Returns: 本次新建成功的广告 list[dict],每项含 {account_id, adgroup_id, adgroup_name, wechat_position}
  205. """
  206. from tools.ad_api import _get
  207. from tools.im_approval_ad_creation import run_ad_approval_workflow
  208. creation_accounts = get_creation_account_ids()
  209. if not creation_accounts:
  210. logger.error("[phase0] 待投放账户配置为空,退出")
  211. return []
  212. creation_accounts = _filter_creation_accounts(creation_accounts, "phase0")
  213. if not creation_accounts:
  214. logger.info("[phase0] 临时过滤后无待处理账户")
  215. return []
  216. # Task 26:NORMAL + SUSPEND 都算占用唯一性槽位(腾讯文档:删除前历史广告占槽位)
  217. OCCUPIED_STATUSES = {"AD_STATUS_NORMAL", "AD_STATUS_SUSPEND"}
  218. pending_ad_records: list[dict] = []
  219. for account_id in creation_accounts:
  220. logger.info("=" * 60)
  221. logger.info("[phase0] 账户 %d 处理开始", account_id)
  222. try:
  223. grant_result = ensure_account_audience_grant(account_id)
  224. logger.info(
  225. "[phase0] 人群包校验完成: status=%s audience_id=%s audience=%r",
  226. grant_result.get("status"),
  227. grant_result.get("audience_id"),
  228. grant_result.get("audience_name"),
  229. )
  230. except Exception as e:
  231. logger.exception("[phase0] account=%d 人群包授权/验证失败,跳过:%s", account_id, e)
  232. continue
  233. # Task 26:查所有非 DELETED 广告,NORMAL+SUSPEND 都算占用槽位
  234. try:
  235. r = _get("/adgroups/get", {
  236. "account_id": account_id, "page": 1, "page_size": 100,
  237. "fields": ["adgroup_id", "configured_status"],
  238. })
  239. all_ads = (r.get("data") or {}).get("list") or []
  240. normal_n = sum(1 for a in all_ads if a.get("configured_status") == "AD_STATUS_NORMAL")
  241. suspend_n = sum(1 for a in all_ads if a.get("configured_status") == "AD_STATUS_SUSPEND")
  242. occupied = [a for a in all_ads if a.get("configured_status") in OCCUPIED_STATUSES]
  243. logger.info(
  244. "[phase0] 广告数: 总 %d,NORMAL %d + SUSPEND %d = 占用 %d / 目标 %d",
  245. len(all_ads), normal_n, suspend_n, len(occupied), target_ads,
  246. )
  247. except Exception as e:
  248. logger.exception("[phase0] account=%d 查广告数失败: %s", account_id, e)
  249. continue
  250. to_create = max(0, target_ads - len(occupied))
  251. if to_create == 0:
  252. logger.info("[phase0] 占用槽位已满,跳过")
  253. continue
  254. # Task 27:fingerprint 预校验 — 反查现存广告的 fp 集合,enumerate 时排除
  255. existing_fps = _fetch_existing_fingerprints_for_account(account_id)
  256. # enumerate 候选(模块 A,差异化 wechat_position 有/无)
  257. try:
  258. candidates = enumerate_new_ad_candidates(
  259. account_id, count=to_create,
  260. existing_fingerprints=existing_fps,
  261. )
  262. except Exception as e:
  263. logger.exception("[phase0] account=%d enumerate 失败: %s", account_id, e)
  264. continue
  265. if not candidates:
  266. logger.warning("[phase0] enumerate 返回 0 条候选,跳过")
  267. continue
  268. today = now_in_timezone().strftime("%Y-%m-%d")
  269. for c in candidates:
  270. age_range = ",".join(
  271. f"{a.get('min')}-{a.get('max')}" for a in c.age
  272. )
  273. # 新建广告默认开启;实际投放仍受创意审核/账户/预算控制。
  274. # 这里显式传 NORMAL,避免未来默认值变化影响端到端创建语义。
  275. body = build_ad_request_body(c, configured_status="AD_STATUS_NORMAL")
  276. rec = {
  277. "approval_date": today,
  278. "account_id": c.account_id,
  279. "audience_tier_label": c.audience_tier_label,
  280. "adgroup_name": c.adgroup_name,
  281. "site_set": c.site_set,
  282. "delivery_version": c.delivery_version,
  283. "wechat_position": c.wechat_position,
  284. "bid_amount_fen": c.bid_amount_fen,
  285. "bid_scene": c.bid_scene,
  286. "custom_cost_cap_fen": c.custom_cost_cap_fen,
  287. "automatic_site_enabled": c.automatic_site_enabled,
  288. "age_range": age_range,
  289. "fingerprint": c.fingerprint,
  290. "_request_body": body,
  291. }
  292. pending_ad_records.append(rec)
  293. if not pending_ad_records:
  294. logger.info("[phase0] 无广告需新建,Phase 0 结束")
  295. return []
  296. # 飞书审批(如需要)
  297. data_dir = _HERE / "outputs" / "data"
  298. if CREATION_APPROVAL_REQUIRED:
  299. logger.info(
  300. "[phase0] CREATION_APPROVAL_REQUIRED=True → 飞书审批 %d 条广告候选",
  301. len(pending_ad_records),
  302. )
  303. sheet_meta, actions = run_ad_approval_workflow(
  304. records=pending_ad_records,
  305. xlsx_output_dir=data_dir,
  306. timeout_minutes=CREATION_APPROVAL_TIMEOUT_MINUTES,
  307. )
  308. for i, rec in enumerate(pending_ad_records, start=1):
  309. rec["action"] = actions.get(i, "skip")
  310. else:
  311. for rec in pending_ad_records:
  312. rec["action"] = "approve"
  313. # POST 真建 approve 项
  314. created: list[dict] = []
  315. for rec in pending_ad_records:
  316. if rec.get("action") != "approve":
  317. continue
  318. adgroup_id = post_ad_with_prepared_body(
  319. account_id=int(rec["account_id"]),
  320. body=rec["_request_body"],
  321. )
  322. if adgroup_id:
  323. created.append({
  324. "account_id": rec["account_id"],
  325. "adgroup_id": adgroup_id,
  326. "adgroup_name": rec["adgroup_name"],
  327. "wechat_position": rec.get("wechat_position"),
  328. })
  329. logger.info(
  330. "[phase0] 完成:候选 %d 条,approve %d 条,实际建 %d 条",
  331. len(pending_ad_records),
  332. sum(1 for r in pending_ad_records if r.get("action") == "approve"),
  333. len(created),
  334. )
  335. return created
  336. def _wait_created_ads_visible(created_ads: list[dict], timeout_seconds: int = 60) -> None:
  337. """等待刚创建的广告在 /adgroups/get 中可见。
  338. 腾讯 adgroups/add 成功后,list 接口存在短暂一致性延迟。若立刻进入 Phase 1,
  339. 可能只扫到部分新广告,导致同一轮只给一条广告补创意。
  340. """
  341. if not created_ads:
  342. return
  343. from tools.ad_api import _get
  344. by_account: dict[int, set[int]] = {}
  345. for ad in created_ads:
  346. by_account.setdefault(int(ad["account_id"]), set()).add(int(ad["adgroup_id"]))
  347. deadline = time.time() + timeout_seconds
  348. remaining = {acct: set(ids) for acct, ids in by_account.items()}
  349. while time.time() < deadline and any(remaining.values()):
  350. for account_id, ids in list(remaining.items()):
  351. if not ids:
  352. continue
  353. try:
  354. resp = _get("/adgroups/get", {
  355. "account_id": account_id,
  356. "page": 1,
  357. "page_size": 100,
  358. "fields": ["adgroup_id", "configured_status", "system_status"],
  359. })
  360. visible = {
  361. int(item["adgroup_id"])
  362. for item in ((resp.get("data") or {}).get("list") or [])
  363. if item.get("adgroup_id") is not None
  364. }
  365. ids.difference_update(visible)
  366. except Exception as e:
  367. logger.warning(
  368. "[phase0] 等待新广告可见失败 account=%d: %s",
  369. account_id, e,
  370. )
  371. if any(remaining.values()):
  372. logger.info("[phase0] 等待新广告在列表接口可见: %s", remaining)
  373. time.sleep(5)
  374. not_visible = {acct: sorted(ids) for acct, ids in remaining.items() if ids}
  375. if not_visible:
  376. logger.warning("[phase0] 部分新广告仍未在 list 可见,继续 Phase 1: %s", not_visible)
  377. else:
  378. logger.info("[phase0] 新建广告均已在 list 接口可见")
  379. def phase1_prepare(target_creatives: int = TARGET_CREATIVES_PER_AD) -> list[dict]:
  380. """Phase 1:扫账户 → 关联点过滤 → 准备 pending records(不 POST 腾讯)。
  381. Returns:
  382. pending_records — 每个元素是 prepare_one_creative_for_ad 返回的 dict
  383. """
  384. creation_accounts = get_creation_account_ids()
  385. if not creation_accounts:
  386. logger.error("[phase1] 待投放账户配置为空,Phase 1 退出")
  387. return []
  388. creation_accounts = _filter_creation_accounts(creation_accounts, "phase1")
  389. if not creation_accounts:
  390. logger.info("[phase1] 临时过滤后无待处理账户")
  391. return []
  392. excluded_ad_ids = load_excluded_ad_ids_from_adjustment()
  393. logger.info("[phase1] 关联点过滤集合 size=%d", len(excluded_ad_ids))
  394. pending_records: list[dict] = []
  395. # 素材排重只读取已有明确结果的 DB 记录。
  396. # pending/prepared 没有审批或投放结果,不进入排重。
  397. # 但同一轮审批候选需要做展示去重,避免表格里同 crowd_package 反复出现同一素材。
  398. # 这个 set 只存在内存里,不写历史排重库;进程结束即失效。
  399. display_used_material_ids_by_crowd: dict[str, set[str]] = {}
  400. # landing_video 按素材来源拆池。历史素材和 AI 生成素材互不占用,但池内都严格去重。
  401. landing_usage_counts_by_crowd: dict[str, dict[str, dict[int, int]]] = {}
  402. def accept_pending_record(
  403. rec: dict,
  404. *,
  405. crowd_package: str,
  406. display_excluded_material_ids: set[str],
  407. crowd_landing_counts: dict[str, dict[int, int]],
  408. landing_counts_for_ad: dict[int, int],
  409. recovered: bool = False,
  410. ) -> None:
  411. pending_records.append(rec)
  412. material_id = rec.get("_material_id")
  413. if material_id:
  414. display_excluded_material_ids.add(str(material_id))
  415. logger.info(
  416. "[phase1] 本轮展示去重登记 crowd=%r material=%s size=%d%s",
  417. crowd_package, material_id,
  418. len(display_excluded_material_ids),
  419. " recovered" if recovered else "",
  420. )
  421. landing_video_id = rec.get("landing_video_id")
  422. if landing_video_id is not None:
  423. landing_video_id = int(landing_video_id)
  424. landing_counts_for_ad[landing_video_id] = (
  425. landing_counts_for_ad.get(landing_video_id, 0) + 1
  426. )
  427. actual_material_source = (
  428. MATERIAL_SOURCE_AI_GENERATED
  429. if str(rec.get("material_source") or "") == MATERIAL_SOURCE_AI_GENERATED
  430. or str(rec.get("_material_id") or "").startswith("ai:")
  431. else MATERIAL_SOURCE_HISTORY
  432. )
  433. source_counts = crowd_landing_counts.setdefault(actual_material_source, {})
  434. source_counts[landing_video_id] = source_counts.get(landing_video_id, 0) + 1
  435. logger.info(
  436. "[phase1] 同人群包 landing 使用登记 crowd=%r material_source=%s landing=%d count=%d max=%d%s",
  437. crowd_package, actual_material_source, landing_video_id,
  438. source_counts[landing_video_id],
  439. MAX_SAME_LANDING_PER_AD_IN_RUN,
  440. " recovered" if recovered else "",
  441. )
  442. logger.info(
  443. "[phase1] 本轮同广告 landing 计数 adgroup=%s landing=%d count=%d limit=%d%s",
  444. rec.get("adgroup_id"), landing_video_id,
  445. landing_counts_for_ad[landing_video_id],
  446. MAX_SAME_LANDING_PER_AD_IN_RUN,
  447. " recovered" if recovered else "",
  448. )
  449. if not recovered:
  450. try:
  451. record_prepared_material_usage(rec)
  452. except Exception as e:
  453. logger.warning(
  454. "[phase1] material usage 记录失败 account=%s adgroup=%s material=%s:%s",
  455. rec.get("account_id"), rec.get("adgroup_id"),
  456. rec.get("_material_id"), e,
  457. )
  458. for account_id in creation_accounts:
  459. logger.info("=" * 60)
  460. logger.info("[phase1] 账户 %d 处理开始", account_id)
  461. try:
  462. ads = find_ads_needing_creatives(account_id, min_creatives=target_creatives)
  463. except Exception as e:
  464. logger.exception("[phase1] account=%d find_ads 失败,跳过:%s", account_id, e)
  465. continue
  466. ads_after_filter = [a for a in ads if a["adgroup_id"] not in excluded_ad_ids]
  467. excluded_count = len(ads) - len(ads_after_filter)
  468. if excluded_count:
  469. logger.info(
  470. "[phase1] account=%d 关联点过滤排除 %d 条",
  471. account_id, excluded_count,
  472. )
  473. if not ads_after_filter:
  474. logger.info("[phase1] account=%d 无广告需补创意", account_id)
  475. continue
  476. try:
  477. crowd_package = get_account_crowd_package(account_id)
  478. except Exception as e:
  479. logger.exception(
  480. "[phase1] account=%d 读取 crowd_package 失败,跳过:%s",
  481. account_id, e,
  482. )
  483. continue
  484. display_excluded_material_ids = display_used_material_ids_by_crowd.setdefault(
  485. crowd_package, set(),
  486. )
  487. if crowd_package not in landing_usage_counts_by_crowd:
  488. try:
  489. landing_usage_counts_by_crowd[crowd_package] = load_recent_landing_usage_counts(
  490. crowd_package,
  491. )
  492. except Exception as e:
  493. logger.warning(
  494. "[phase1] crowd=%r 读取 landing 使用历史失败,仅使用本轮排重:%s",
  495. crowd_package, e,
  496. )
  497. landing_usage_counts_by_crowd[crowd_package] = {
  498. MATERIAL_SOURCE_HISTORY: {},
  499. MATERIAL_SOURCE_AI_GENERATED: {},
  500. }
  501. logger.info(
  502. "[phase1] crowd=%r 近期 landing 使用 history=%d ai_generated=%d",
  503. crowd_package,
  504. len(landing_usage_counts_by_crowd[crowd_package].get(MATERIAL_SOURCE_HISTORY, {})),
  505. len(landing_usage_counts_by_crowd[crowd_package].get(MATERIAL_SOURCE_AI_GENERATED, {})),
  506. )
  507. crowd_landing_counts = landing_usage_counts_by_crowd[crowd_package]
  508. crowd_landing_counts.setdefault(MATERIAL_SOURCE_HISTORY, {})
  509. crowd_landing_counts.setdefault(MATERIAL_SOURCE_AI_GENERATED, {})
  510. try:
  511. material_strategy = load_account_material_strategy(account_id)
  512. except Exception as e:
  513. logger.warning(
  514. "[phase1] account=%d 读取素材策略失败,按 history 排重:%s",
  515. account_id, e,
  516. )
  517. material_strategy = None
  518. requested_material_source = (
  519. MATERIAL_SOURCE_AI_GENERATED
  520. if material_strategy is not None and material_strategy.use_ai_generated
  521. else MATERIAL_SOURCE_HISTORY
  522. )
  523. landing_max_uses = MAX_SAME_LANDING_PER_AD_IN_RUN
  524. logger.info(
  525. "[phase1] account=%d landing 排重池=%s max_uses=%d",
  526. account_id, requested_material_source, landing_max_uses,
  527. )
  528. for ad in ads_after_filter:
  529. adgroup_id = ad["adgroup_id"]
  530. already_have = ad["creative_count"]
  531. to_add = max(0, target_creatives - already_have)
  532. prepared_for_ad = 0
  533. failed_prepare_for_ad = 0
  534. landing_counts_for_ad: dict[int, int] = {}
  535. logger.info(
  536. "[phase1] adgroup=%d(have=%d need=%d)",
  537. adgroup_id, already_have, to_add,
  538. )
  539. recovered_records = load_recoverable_prepared_records(
  540. account_id=account_id,
  541. adgroup_id=adgroup_id,
  542. crowd_package=crowd_package,
  543. material_source=requested_material_source,
  544. limit=to_add,
  545. )
  546. for rec in recovered_records:
  547. accept_pending_record(
  548. rec,
  549. crowd_package=crowd_package,
  550. display_excluded_material_ids=display_excluded_material_ids,
  551. crowd_landing_counts=crowd_landing_counts,
  552. landing_counts_for_ad=landing_counts_for_ad,
  553. recovered=True,
  554. )
  555. if recovered_records:
  556. prepared_for_ad += len(recovered_records)
  557. logger.info(
  558. "[phase1] adgroup=%d 恢复未提交 prepared records=%d remaining=%d",
  559. adgroup_id, len(recovered_records), max(0, to_add - prepared_for_ad),
  560. )
  561. for _ in range(max(0, to_add - prepared_for_ad)):
  562. landing_excluded_for_ad = {
  563. vid
  564. for vid, count in landing_counts_for_ad.items()
  565. if count >= MAX_SAME_LANDING_PER_AD_IN_RUN
  566. }
  567. landing_excluded_for_source = {
  568. vid
  569. for vid, count in crowd_landing_counts
  570. .get(requested_material_source, {})
  571. .items()
  572. if count >= landing_max_uses
  573. }
  574. effective_excluded_landing_ids = (
  575. landing_excluded_for_source | landing_excluded_for_ad
  576. )
  577. try:
  578. rec = prepare_one_creative_for_ad(
  579. account_id, adgroup_id,
  580. excluded_material_ids=display_excluded_material_ids,
  581. excluded_landing_ids=effective_excluded_landing_ids,
  582. )
  583. except Exception as e:
  584. logger.exception(
  585. "[phase1] adgroup=%d 准备失败:%s", adgroup_id, e,
  586. )
  587. rec = None
  588. if rec:
  589. prepared_for_ad += 1
  590. accept_pending_record(
  591. rec,
  592. crowd_package=crowd_package,
  593. display_excluded_material_ids=display_excluded_material_ids,
  594. crowd_landing_counts=crowd_landing_counts,
  595. landing_counts_for_ad=landing_counts_for_ad,
  596. )
  597. else:
  598. failed_prepare_for_ad += 1
  599. # 2026-06-10 用户要求:单条 prepare 失败 → continue 不 break
  600. # 同广告剩余 to_add 创意还能继续试,不被一次失败拖累
  601. logger.info(
  602. "[phase1] adgroup=%d 本条创意 prepare 失败,试下一条",
  603. adgroup_id,
  604. )
  605. logger.info(
  606. "[phase1] adgroup=%d 补创意完成 target=%d have_before=%d "
  607. "planned=%d prepared=%d failed_prepare=%d",
  608. adgroup_id, target_creatives, already_have, to_add,
  609. prepared_for_ad, failed_prepare_for_ad,
  610. )
  611. if prepared_for_ad < to_add:
  612. logger.warning(
  613. "[phase1] adgroup=%d 未补满:缺口=%d。详细原因见上方 "
  614. "prepare_one_creative source_summary/失败日志",
  615. adgroup_id, to_add - prepared_for_ad,
  616. )
  617. logger.info("=" * 60)
  618. logger.info("[phase1] 准备完成,共 %d 条 pending records", len(pending_records))
  619. return pending_records
  620. def _write_pending_records(records: list[dict], output_dir: Path) -> Path:
  621. """落 Phase 1 产物 JSON(供 Phase 3 独立 apply 用 / 追溯)。"""
  622. output_dir.mkdir(parents=True, exist_ok=True)
  623. now = now_in_timezone()
  624. date_str = now.strftime("%Y%m%d")
  625. ts = now.strftime("%H%M%S")
  626. out_path = output_dir / f"creation_pending_{date_str}_{ts}.json"
  627. with open(out_path, "w", encoding="utf-8") as f:
  628. json.dump(records, f, ensure_ascii=False, indent=2)
  629. return out_path
  630. def phase2_approval(records: list[dict], xlsx_output_dir: Path) -> dict[int, str]:
  631. """Phase 2:飞书审批(若需要)。
  632. Returns: actions {row_idx: action}, 1-based row_idx
  633. """
  634. from tools.im_approval_creation import run_approval_workflow
  635. sheet_meta, actions = run_approval_workflow(
  636. records=records,
  637. xlsx_output_dir=xlsx_output_dir,
  638. timeout_minutes=CREATION_APPROVAL_TIMEOUT_MINUTES,
  639. )
  640. logger.info(
  641. "[phase2] 审批完成 sheet_url=%s actions=%d/%d",
  642. sheet_meta.get("url"), len(actions), len(records),
  643. )
  644. return actions
  645. def run_once() -> dict:
  646. """完整主循环:Phase 0 → Phase 1 → Phase 2(若开关 True)→ Phase 3。"""
  647. run_started = now_in_timezone().isoformat()
  648. sync_stats: dict = {}
  649. # Phase -1:每日主流程启动时先同步飞书「自动化账户」配置表。
  650. logger.info("=" * 60)
  651. logger.info("[main] Phase -1 启动 — 同步飞书自动化账户配置")
  652. try:
  653. from sync_feishu_account_config import sync_from_feishu
  654. sync_stats = sync_from_feishu()
  655. logger.info("[main] Phase -1 完成: %s", sync_stats)
  656. except Exception as e:
  657. logger.exception("[main] Phase -1 同步飞书配置失败,本轮停止:%s", e)
  658. return {
  659. "run_started": run_started,
  660. "run_finished": now_in_timezone().isoformat(),
  661. "approval_required": CREATION_APPROVAL_REQUIRED,
  662. "sync_stats": {"error": str(e)},
  663. "phase0_created_ads": 0,
  664. "total": {"phase1_prepared": 0},
  665. }
  666. # Phase 0:模块 A 建广告(满足每账户 ADS_PER_ACCOUNT 条)
  667. logger.info("=" * 60)
  668. if _env_flag("CREATION_SKIP_PHASE0"):
  669. logger.info("[main] CREATION_SKIP_PHASE0=True → 跳过模块 A 建广告")
  670. created_ads = []
  671. else:
  672. logger.info("[main] Phase 0 启动 — 模块 A 检查 + 建广告")
  673. created_ads = phase0_create_ads()
  674. logger.info("[main] Phase 0 完成:本轮新建广告 %d 条", len(created_ads))
  675. _wait_created_ads_visible(created_ads)
  676. # Phase 1:模块 B 给所有广告(新+旧)补创意
  677. logger.info("=" * 60)
  678. logger.info("[main] Phase 1 启动 — 模块 B 给广告补创意")
  679. pending_records = phase1_prepare()
  680. if not pending_records:
  681. logger.info("[main] Phase 1 无 pending records,主循环退出")
  682. return {
  683. "run_started": run_started,
  684. "run_finished": now_in_timezone().isoformat(),
  685. "approval_required": CREATION_APPROVAL_REQUIRED,
  686. "sync_stats": sync_stats,
  687. "phase0_created_ads": len(created_ads),
  688. "total": {"phase1_prepared": 0},
  689. }
  690. data_dir = _HERE / "outputs" / "data"
  691. pending_path = _write_pending_records(pending_records, data_dir)
  692. logger.info("[main] pending records 已写入: %s", pending_path)
  693. # Phase 2:审批(或 skip)
  694. if CREATION_APPROVAL_REQUIRED:
  695. logger.info("[main] CREATION_APPROVAL_REQUIRED=True → 进 Phase 2 飞书审批")
  696. actions = phase2_approval(pending_records, data_dir)
  697. for i, rec in enumerate(pending_records, start=1):
  698. rec["action"] = actions.get(i, "skip")
  699. else:
  700. logger.info("[main] CREATION_APPROVAL_REQUIRED=False → 全 records approve")
  701. for rec in pending_records:
  702. rec["action"] = "approve"
  703. # Phase 3:执行
  704. logger.info("=" * 60)
  705. logger.info("[main] Phase 3 执行启动")
  706. summary = apply_pending_records(pending_records)
  707. summary["approval_required"] = CREATION_APPROVAL_REQUIRED
  708. summary["pending_records_path"] = str(pending_path)
  709. summary_path = write_apply_summary(summary, data_dir)
  710. logger.info("[main] summary 已写入: %s", summary_path)
  711. # 发执行汇报
  712. _send_apply_summary_to_feishu(summary)
  713. summary["run_started"] = run_started
  714. summary["run_finished"] = now_in_timezone().isoformat()
  715. summary["sync_stats"] = sync_stats
  716. summary["phase0_created_ads"] = len(created_ads)
  717. summary["phase0_ads"] = created_ads
  718. return summary
  719. def main() -> int:
  720. _setup_logging()
  721. logger.info("=" * 60)
  722. logger.info("模块 B 创意搭建子系统 — 主循环启动")
  723. logger.info("TARGET_CREATIVES_PER_AD = %d", TARGET_CREATIVES_PER_AD)
  724. logger.info("CREATION_APPROVAL_REQUIRED = %s", CREATION_APPROVAL_REQUIRED)
  725. logger.info("WHITELIST_ACCOUNTS = %s", WHITELIST_ACCOUNTS)
  726. logger.info("=" * 60)
  727. summary = run_once()
  728. t = summary.get("total") or {}
  729. logger.info("=" * 60)
  730. logger.info("[main] 主循环结束")
  731. if "phase1_prepared" in t:
  732. logger.info(" phase1_prepared = %d", t["phase1_prepared"])
  733. else:
  734. logger.info(" records = %d", t.get("records", 0))
  735. logger.info(" approved = %d", t.get("approved", 0))
  736. logger.info(" posted_ok = %d", t.get("posted_ok", 0))
  737. logger.info(" posted_failed = %d", t.get("posted_failed", 0))
  738. logger.info("=" * 60)
  739. return 0 if t.get("posted_failed", 0) == 0 else 1
  740. if __name__ == "__main__":
  741. sys.exit(main())