plan_map.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from __future__ import annotations
  2. from dataclasses import dataclass
  3. from typing import Any, TypeVar
  4. T = TypeVar("T")
  5. # 品类 → (生成计划ID, 发布计划ID);按 pair 去重后做均匀分发。
  6. AIGC_PLAN_ID_MAP: dict[str, dict[str, str]] = {
  7. "健康知识": {"生成ID": "20260408092313211598604", "发布ID": "20260408115944193153417"},
  8. # "历史名人": {"生成ID": "20260408083251311809309", "发布ID": "20260408115139124511126"},
  9. # "知识科普": {"生成ID": "20260408083824905654920", "发布ID": "20260408115231567509261"},
  10. "搞笑段子": {"生成ID": "20260408091536533918237", "发布ID": "20260408115842127748387"},
  11. # "社会风气": {"生成ID": "20260408084318884115213", "发布ID": "20260408115315950129776"},
  12. "人生忠告": {"生成ID": "20260408085205791658566", "发布ID": "20260408115405410408001"},
  13. "国际时政": {"生成ID": "20260408090208237400605", "发布ID": "20260408115616925523989"},
  14. # "生活技巧科普": {"生成ID": "20260408083824905654920", "发布ID": "20260408115231567509261"},
  15. # "贪污腐败": {"生成ID": "20260408090309503416878", "发布ID": "20260408115653908856043"},
  16. "民生政策": {"生成ID": "20260408090721867506475", "发布ID": "20260408115727030928177"},
  17. "对口型表演": {"生成ID": "20260408092122328523262", "发布ID": "20260408115914659162376"},
  18. "中国战争史": {"生成ID": "20260408090950446586451", "发布ID": "20260408115804931772327"},
  19. "人财诈骗": {"生成ID": "20260408093140652233649", "发布ID": "20260408120019784463902"},
  20. "当代正能量人物": {"生成ID": "20260408083148399635274", "发布ID": "20260408115046382803287"},
  21. "国家科技力量": {"生成ID": "20260408085807674913378", "发布ID": "20260408115542550181196"},
  22. "国家力量": {"生成ID": "20260408085807674913378", "发布ID": "20260408115542550181196"},
  23. "通用": {"生成ID": "20260408085649635441036", "发布ID": "20260408115439581604474"},
  24. }
  25. @dataclass(frozen=True)
  26. class AigcPlanPair:
  27. """一组生成计划 + 发布计划(去重后的分发单元)。"""
  28. label: str
  29. produce_plan_id: str
  30. publish_plan_id: str
  31. def list_unique_plan_pairs(
  32. plan_map: dict[str, dict[str, str]] | None = None,
  33. ) -> list[AigcPlanPair]:
  34. """从映射表提取不重复的 (生成ID, 发布ID) 计划对。"""
  35. source = plan_map or AIGC_PLAN_ID_MAP
  36. seen: set[tuple[str, str]] = set()
  37. pairs: list[AigcPlanPair] = []
  38. for label, ids in source.items():
  39. produce_plan_id = str(ids.get("生成ID") or "").strip()
  40. publish_plan_id = str(ids.get("发布ID") or "").strip()
  41. if not produce_plan_id or not publish_plan_id:
  42. continue
  43. key = (produce_plan_id, publish_plan_id)
  44. if key in seen:
  45. continue
  46. seen.add(key)
  47. pairs.append(
  48. AigcPlanPair(
  49. label=label,
  50. produce_plan_id=produce_plan_id,
  51. publish_plan_id=publish_plan_id,
  52. )
  53. )
  54. return pairs
  55. def distribute_evenly(items: list[T], bucket_count: int) -> list[list[T]]:
  56. """轮询均匀分配到 bucket_count 个桶。"""
  57. if bucket_count <= 0:
  58. raise ValueError("bucket_count 必须大于 0")
  59. if not items:
  60. return []
  61. buckets: list[list[T]] = [[] for _ in range(bucket_count)]
  62. for index, item in enumerate(items):
  63. buckets[index % bucket_count].append(item)
  64. return [bucket for bucket in buckets if bucket]
  65. def chunk_list(items: list[T], size: int) -> list[list[T]]:
  66. if size <= 0:
  67. raise ValueError("size 必须大于 0")
  68. return [items[i : i + size] for i in range(0, len(items), size)]
  69. def assignment_summary(
  70. plan_pairs: list[AigcPlanPair],
  71. buckets: list[list[Any]],
  72. ) -> list[dict[str, Any]]:
  73. """返回每个计划分到的数量摘要。"""
  74. summary: list[dict[str, Any]] = []
  75. for plan, bucket in zip(plan_pairs, buckets, strict=False):
  76. if not bucket:
  77. continue
  78. summary.append(
  79. {
  80. "plan_label": plan.label,
  81. "produce_plan_id": plan.produce_plan_id,
  82. "publish_plan_id": plan.publish_plan_id,
  83. "video_count": len(bucket),
  84. }
  85. )
  86. return summary