execute_creation_apply.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. """模块 B Phase 3 执行入口(P0-A 配套,2026-06-09)。
  2. 数据流:
  3. Phase 1 已准备好的 pending records(含 _request_body)+ 运营决策(action)
  4. → 对 action=approve 的 → POST 腾讯 /dynamic_creatives/add
  5. → 写 creation_run_{date}.json + 发"执行汇报"飞书消息
  6. 可独立运行(从 outputs/data/creation_pending_*.json 读 → 审批已写 → apply):
  7. python execute_creation_apply.py <pending_records_json>
  8. 也可被 execute_creation_once.py import 调用:
  9. apply_pending_records(records) -> summary
  10. """
  11. import asyncio
  12. import argparse
  13. import json
  14. import logging
  15. import sys
  16. from pathlib import Path
  17. _HERE = Path(__file__).parent
  18. sys.path.insert(0, str(_HERE.parent.parent))
  19. sys.path.insert(0, str(_HERE))
  20. from dotenv import load_dotenv # noqa: E402
  21. load_dotenv(_HERE / ".env")
  22. from config import ( # noqa: E402
  23. CREATION_APPROVAL_TIMEOUT_MINUTES,
  24. FEISHU_OPERATOR_CHAT_ID,
  25. now_in_timezone,
  26. )
  27. # 强占 sys.path[0],绕过 config import 副作用(im-client/tools.py 同名冲突)
  28. while str(_HERE) in sys.path:
  29. sys.path.remove(str(_HERE))
  30. sys.path.insert(0, str(_HERE))
  31. from tools.creative_creation import post_creative_with_prepared_body # noqa: E402
  32. from tools.ad_api import images_add # noqa: E402
  33. from tools.ai_generated_material import update_generated_material_status # noqa: E402
  34. from tools.external_recalled_material import update_external_material_status # noqa: E402
  35. from tools.creative_review import ( # noqa: E402
  36. mark_creation_submit_failed,
  37. record_creation_submission,
  38. )
  39. from tools.creative_material_usage import update_material_usage_status # noqa: E402
  40. logger = logging.getLogger("execute_creation_apply")
  41. def _update_deferred_material_status(record: dict, status: str, **kwargs) -> None:
  42. update_generated_material_status(record, status, **kwargs)
  43. update_external_material_status(record, status, **kwargs)
  44. def _fmt_quality_summary(r: dict) -> str:
  45. """Task 29:把 record 里的素材质量字段(prepare 阶段写入)格式化成一行。
  46. 返回类似 ' · 素材质量: ctr=12.5% imp=8,500 cost=420元',字段缺失时省略对应项。
  47. """
  48. ctr = r.get("material_ctr")
  49. imp = r.get("material_impressions")
  50. cost = r.get("material_cost")
  51. parts = []
  52. if ctr is not None:
  53. # ctr 取决于服务端格式,有的返回 0.125(比例),有的 12.5(百分号字面值)
  54. try:
  55. ctr_f = float(ctr)
  56. parts.append(f"ctr={ctr_f * 100:.1f}%" if ctr_f <= 1 else f"ctr={ctr_f:.1f}%")
  57. except (TypeError, ValueError):
  58. pass
  59. if imp is not None:
  60. try:
  61. parts.append(f"imp={int(imp):,}")
  62. except (TypeError, ValueError):
  63. pass
  64. if cost is not None:
  65. try:
  66. parts.append(f"cost={float(cost):.0f}元")
  67. except (TypeError, ValueError):
  68. pass
  69. return f" · 素材质量: {' '.join(parts)}" if parts else ""
  70. def _send_apply_summary_to_feishu(
  71. summary: dict, chat_id: str = ""
  72. ) -> None:
  73. """Phase 3 完成后发"执行汇报"飞书纯文本消息(创意 ID 列表 + 素材质量摘要)。
  74. Task 29(2026-06-11):每行加 ctr / cost / impressions 三个字段,
  75. 让运营在不打开 Excel 的情况下,一眼看出本次挂上的创意素材质量。
  76. """
  77. if not chat_id:
  78. chat_id = FEISHU_OPERATOR_CHAT_ID
  79. if not chat_id:
  80. logger.warning("[apply] FEISHU_OPERATOR_CHAT_ID 未配置,不发执行汇报")
  81. return
  82. try:
  83. from tools.feishu_doc import _auth_headers, _get_tenant_token
  84. import httpx
  85. t = summary["total"]
  86. lines = [
  87. "【创意搭建·执行汇报】",
  88. f"运营 approve 共 {t['approved']} 条,实际挂上 {t['posted_ok']} 条,失败 {t['posted_failed']} 条",
  89. ]
  90. approved_list = [r for r in summary["records"] if r.get("action") == "approve"]
  91. if approved_list:
  92. # Task 29:算挂上创意的素材质量均值,放汇报头(运营快速判断本批整体质量)
  93. ok_records = [r for r in approved_list if r.get("dynamic_creative_id")]
  94. valid_ctr = [float(r["material_ctr"]) for r in ok_records
  95. if r.get("material_ctr") is not None]
  96. valid_imp = [int(r["material_impressions"]) for r in ok_records
  97. if r.get("material_impressions") is not None]
  98. valid_cost = [float(r["material_cost"]) for r in ok_records
  99. if r.get("material_cost") is not None]
  100. if valid_ctr or valid_imp or valid_cost:
  101. stat_parts = []
  102. if valid_ctr:
  103. avg_ctr = sum(valid_ctr) / len(valid_ctr)
  104. stat_parts.append(
  105. f"平均 ctr={avg_ctr * 100:.1f}%" if avg_ctr <= 1
  106. else f"平均 ctr={avg_ctr:.1f}%"
  107. )
  108. if valid_imp:
  109. stat_parts.append(f"平均 imp={int(sum(valid_imp) / len(valid_imp)):,}")
  110. if valid_cost:
  111. stat_parts.append(f"平均 cost={sum(valid_cost) / len(valid_cost):.0f}元")
  112. lines.append(f"素材质量(n={len(ok_records)}): {' · '.join(stat_parts)}")
  113. lines.append("")
  114. lines.append("【已挂创意】")
  115. for r in approved_list:
  116. cid = r.get("dynamic_creative_id") or "(挂失败)"
  117. quality = _fmt_quality_summary(r)
  118. lines.append(
  119. f" · adgroup {r['adgroup_id']}({r['adgroup_name']}) "
  120. f"→ creative_id={cid} · name={r['creative_name']}{quality}"
  121. )
  122. rejected_list = [r for r in summary["records"] if r.get("action") == "reject"]
  123. hold_list = [r for r in summary["records"] if r.get("action") == "hold"]
  124. skip_list = [r for r in summary["records"] if r.get("action") == "skip"]
  125. if rejected_list or hold_list or skip_list:
  126. lines.append("")
  127. lines.append(
  128. f"【未挂】reject {len(rejected_list)} · hold {len(hold_list)} · skip {len(skip_list)}"
  129. )
  130. text = "\n".join(lines)
  131. token = _get_tenant_token()
  132. url = "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id"
  133. body = {
  134. "receive_id": chat_id,
  135. "msg_type": "text",
  136. "content": json.dumps({"text": text}, ensure_ascii=False),
  137. }
  138. resp = httpx.post(url, headers=_auth_headers(token), json=body, timeout=30)
  139. if resp.json().get("code") == 0:
  140. logger.info("[apply] 执行汇报已发送 chat=%s", chat_id)
  141. else:
  142. logger.warning("[apply] 执行汇报发送失败: %s", resp.text[:200])
  143. except Exception as e:
  144. logger.exception("[apply] 发送执行汇报异常: %s", e)
  145. def apply_pending_records(records: list[dict]) -> dict:
  146. """Phase 3:对 records 里 action=approve 的项调腾讯 POST 创意,返回 summary。
  147. Args:
  148. records: pending records,每个含 action 字段(approve/reject/hold/skip)
  149. 以及 _request_body / account_id / adgroup_id / 等
  150. Returns:
  151. summary dict:
  152. {
  153. "run_started", "run_finished",
  154. "total": {records, approved, posted_ok, posted_failed},
  155. "records": [...] # 每条含 action + dynamic_creative_id(成功时) + error(失败时)
  156. }
  157. """
  158. run_started = now_in_timezone().isoformat()
  159. posted_ok = 0
  160. posted_failed = 0
  161. approved_total = 0
  162. out_records = []
  163. for r in records:
  164. rec = dict(r)
  165. action = (rec.get("action") or "skip").lower()
  166. rec["action"] = action
  167. if action != "approve":
  168. try:
  169. update_material_usage_status(rec, action)
  170. _update_deferred_material_status(rec, action)
  171. except Exception as e:
  172. logger.warning(
  173. "[apply] 更新素材 usage 状态失败 action=%s material=%s: %s",
  174. action, rec.get("_material_id"), e,
  175. )
  176. out_records.append(rec)
  177. continue
  178. approved_total += 1
  179. body = rec.get("_request_body")
  180. if not body:
  181. rec["error"] = "missing _request_body"
  182. posted_failed += 1
  183. try:
  184. update_material_usage_status(rec, "post_failed", error=rec["error"])
  185. _update_deferred_material_status(rec, "post_failed", error=rec["error"])
  186. except Exception as e:
  187. logger.warning("[apply] 更新素材 usage 状态失败: %s", e)
  188. out_records.append(rec)
  189. continue
  190. pending_image_url = str(rec.get("_pending_image_url") or "").strip()
  191. if pending_image_url and not rec.get("_material_image_id"):
  192. try:
  193. material_image_id = images_add(int(rec["account_id"]), pending_image_url)
  194. rec["_material_image_id"] = material_image_id
  195. image_comp = body.get("creative_components", {}).get("image") or []
  196. if image_comp:
  197. image_comp[0].setdefault("value", {})["image_id"] = material_image_id
  198. _update_deferred_material_status(
  199. rec,
  200. "approve",
  201. tencent_image_id=material_image_id,
  202. )
  203. except Exception as e:
  204. rec["error"] = f"ai_image_upload_failed:{e}"
  205. posted_failed += 1
  206. mark_creation_submit_failed(rec, rec["error"])
  207. try:
  208. update_material_usage_status(rec, "post_failed", error=rec["error"])
  209. _update_deferred_material_status(rec, "post_failed", error=rec["error"])
  210. except Exception as update_e:
  211. logger.warning("[apply] 更新素材 usage 状态失败: %s", update_e)
  212. out_records.append(rec)
  213. continue
  214. cid = post_creative_with_prepared_body(
  215. account_id=int(rec["account_id"]),
  216. body=body,
  217. skip_if_exists=True,
  218. )
  219. if cid:
  220. rec["dynamic_creative_id"] = str(cid)
  221. posted_ok += 1
  222. try:
  223. update_material_usage_status(rec, "posted_ok", dynamic_creative_id=cid)
  224. _update_deferred_material_status(
  225. rec,
  226. "posted_ok",
  227. dynamic_creative_id=cid,
  228. tencent_image_id=str(rec.get("_material_image_id") or ""),
  229. )
  230. except Exception as e:
  231. logger.warning("[apply] 更新素材 usage 状态失败 cid=%s: %s", cid, e)
  232. try:
  233. record_creation_submission(rec, int(cid))
  234. except Exception as e:
  235. logger.warning("[apply] 记录创意审核扫描任务失败 cid=%s: %s", cid, e)
  236. else:
  237. rec["error"] = "post_failed"
  238. posted_failed += 1
  239. mark_creation_submit_failed(rec, "post_failed")
  240. try:
  241. update_material_usage_status(rec, "post_failed", error=rec["error"])
  242. _update_deferred_material_status(rec, "post_failed", error=rec["error"])
  243. except Exception as e:
  244. logger.warning("[apply] 更新素材 usage 状态失败: %s", e)
  245. out_records.append(rec)
  246. run_finished = now_in_timezone().isoformat()
  247. return {
  248. "run_started": run_started,
  249. "run_finished": run_finished,
  250. "total": {
  251. "records": len(records),
  252. "approved": approved_total,
  253. "posted_ok": posted_ok,
  254. "posted_failed": posted_failed,
  255. },
  256. "records": out_records,
  257. }
  258. def _strip_body_for_json(records: list[dict]) -> list[dict]:
  259. """删 _request_body 中可能含 jump_spec 的长字段,JSON 落盘体积更小。
  260. 保留 body 顶层字段(adgroup_id/name/account_id)作为追溯。"""
  261. out = []
  262. for r in records:
  263. rec = dict(r)
  264. body = rec.pop("_request_body", None)
  265. if body:
  266. rec["_body_summary"] = {
  267. "account_id": body.get("account_id"),
  268. "adgroup_id": body.get("adgroup_id"),
  269. "dynamic_creative_name": body.get("dynamic_creative_name"),
  270. }
  271. out.append(rec)
  272. return out
  273. def write_summary(summary: dict, output_dir: Path) -> Path:
  274. """把 Phase 3 执行 summary 落盘为 creation_run_{date}_{ts}.json(剥离长 body 字段),返回文件路径。"""
  275. output_dir.mkdir(parents=True, exist_ok=True)
  276. now = now_in_timezone()
  277. date_str = now.strftime("%Y%m%d")
  278. ts = now.strftime("%H%M%S")
  279. out_path = output_dir / f"creation_run_{date_str}_{ts}.json"
  280. persisted = dict(summary)
  281. persisted["records"] = _strip_body_for_json(summary["records"])
  282. with open(out_path, "w", encoding="utf-8") as f:
  283. json.dump(persisted, f, ensure_ascii=False, indent=2)
  284. return out_path
  285. def main() -> int:
  286. """独立模式:从 JSON 文件读 pending records → apply → 写 summary + 发飞书。"""
  287. logging.basicConfig(
  288. level=logging.INFO,
  289. format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
  290. datefmt="%H:%M:%S",
  291. )
  292. for noisy in ("httpx", "httpcore", "config", "db.config"):
  293. logging.getLogger(noisy).setLevel(logging.WARNING)
  294. # SLS 上报(2026-06-11 接入)— 配置缺失自动降级
  295. try:
  296. from tools.sls_setup import attach_sls_handler
  297. attach_sls_handler()
  298. except Exception as e:
  299. logger.warning("[sls] 挂载异常(降级为本地 only):%s", e)
  300. parser = argparse.ArgumentParser(
  301. description="从 pending records 执行创意创建;可选择读取已有飞书审批表决策。",
  302. )
  303. parser.add_argument("pending_records_json")
  304. parser.add_argument("--sheet-token", default="", help="已有创意审批飞书表 token")
  305. parser.add_argument("--sheet-id", default="", help="已有创意审批飞书表 sheet_id")
  306. parser.add_argument(
  307. "--approval-timeout-minutes",
  308. type=int,
  309. default=CREATION_APPROVAL_TIMEOUT_MINUTES,
  310. help="读取已有审批表时等待决策的分钟数",
  311. )
  312. args = parser.parse_args()
  313. pending_path = Path(args.pending_records_json)
  314. if not pending_path.exists():
  315. logger.error(f"文件不存在: {pending_path}")
  316. return 1
  317. with open(pending_path, encoding="utf-8") as f:
  318. records = json.load(f)
  319. if args.sheet_token and args.sheet_id:
  320. from tools.im_approval_creation import poll_approval_actions
  321. logger.info(
  322. "读取已有审批表决策 sheet_token=%s sheet_id=%s records=%d",
  323. args.sheet_token, args.sheet_id, len(records),
  324. )
  325. actions = poll_approval_actions(
  326. sheet_token=args.sheet_token,
  327. sheet_id=args.sheet_id,
  328. expected_row_count=len(records),
  329. timeout_minutes=args.approval_timeout_minutes,
  330. )
  331. for i, rec in enumerate(records, start=1):
  332. rec["action"] = actions.get(i, "skip")
  333. logger.info("审批决策读取完成: %d/%d", len(actions), len(records))
  334. logger.info(f"读到 {len(records)} 条 pending records,开始 Phase 3 执行")
  335. summary = apply_pending_records(records)
  336. out_path = write_summary(summary, _HERE / "outputs" / "data")
  337. _send_apply_summary_to_feishu(summary)
  338. t = summary["total"]
  339. logger.info(
  340. f"Phase 3 完成: approve={t['approved']} ok={t['posted_ok']} fail={t['posted_failed']}"
  341. )
  342. logger.info(f"summary: {out_path}")
  343. return 0 if t["posted_failed"] == 0 else 1
  344. if __name__ == "__main__":
  345. sys.exit(main())