task_handler.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from datetime import datetime
  2. from applications.tasks.algorithm_tasks import AccountCategoryAnalysis
  3. from applications.tasks.cold_start_tasks import ArticlePoolColdStart
  4. from applications.tasks.crawler_tasks import CrawlerToutiao
  5. from applications.tasks.crawler_tasks import WeixinAccountManager
  6. from applications.tasks.crawler_tasks import CrawlerGzhAccountArticles
  7. from applications.tasks.crawler_tasks import CrawlerGzhSearchArticles
  8. from applications.tasks.data_recycle_tasks import RecycleDailyPublishArticlesTask
  9. from applications.tasks.data_recycle_tasks import CheckDailyPublishArticlesTask
  10. from applications.tasks.data_recycle_tasks import UpdateRootSourceIdAndUpdateTimeTask
  11. from applications.tasks.data_recycle_tasks import RecycleFwhDailyPublishArticlesTask
  12. from applications.tasks.llm_tasks import TitleRewrite
  13. from applications.tasks.llm_tasks import ArticlePoolCategoryGeneration
  14. from applications.tasks.llm_tasks import CandidateAccountQualityScoreRecognizer
  15. from applications.tasks.monitor_tasks import check_kimi_balance
  16. from applications.tasks.monitor_tasks import GetOffVideos
  17. from applications.tasks.monitor_tasks import CheckVideoAuditStatus
  18. from applications.tasks.monitor_tasks import InnerGzhArticlesMonitor
  19. from applications.tasks.monitor_tasks import OutsideGzhArticlesMonitor
  20. from applications.tasks.monitor_tasks import OutsideGzhArticlesCollector
  21. from applications.tasks.monitor_tasks import TaskProcessingMonitor
  22. from applications.tasks.task_mapper import TaskMapper
  23. class TaskHandler(TaskMapper):
  24. def __init__(self, data, log_service, db_client, trace_id):
  25. self.data = data
  26. self.log_client = log_service
  27. self.db_client = db_client
  28. self.trace_id = trace_id
  29. # ---------- 下面是若干复合任务的局部实现 ----------
  30. async def _check_kimi_balance_handler(self) -> int:
  31. response = await check_kimi_balance()
  32. await self.log_client.log(
  33. contents={
  34. "trace_id": self.trace_id,
  35. "task": "check_kimi_balance",
  36. "data": response,
  37. }
  38. )
  39. return self.TASK_SUCCESS_STATUS
  40. async def _get_off_videos_task_handler(self) -> int:
  41. sub_task = GetOffVideos(self.db_client, self.log_client, self.trace_id)
  42. return await sub_task.deal()
  43. async def _check_video_audit_status_handler(self) -> int:
  44. sub_task = CheckVideoAuditStatus(self.db_client, self.log_client, self.trace_id)
  45. return await sub_task.deal()
  46. async def _task_processing_monitor_handler(self) -> int:
  47. sub_task = TaskProcessingMonitor(self.db_client)
  48. await sub_task.deal()
  49. return self.TASK_SUCCESS_STATUS
  50. async def _inner_gzh_articles_monitor_handler(self) -> int:
  51. sub_task = InnerGzhArticlesMonitor(self.db_client)
  52. return await sub_task.deal()
  53. async def _title_rewrite_handler(self):
  54. sub_task = TitleRewrite(self.db_client, self.log_client, self.trace_id)
  55. return await sub_task.deal()
  56. async def _update_root_source_id_handler(self) -> int:
  57. sub_task = UpdateRootSourceIdAndUpdateTimeTask(self.db_client, self.log_client)
  58. await sub_task.deal()
  59. return self.TASK_SUCCESS_STATUS
  60. async def _outside_monitor_handler(self) -> int:
  61. collector = OutsideGzhArticlesCollector(self.db_client)
  62. await collector.deal()
  63. monitor = OutsideGzhArticlesMonitor(self.db_client)
  64. return await monitor.deal() # 应返回 SUCCESS / FAILED
  65. async def _recycle_article_data_handler(self) -> int:
  66. date_str = self.data.get("date_string") or datetime.now().strftime("%Y-%m-%d")
  67. recycle = RecycleDailyPublishArticlesTask(
  68. self.db_client, self.log_client, date_str
  69. )
  70. await recycle.deal()
  71. check = CheckDailyPublishArticlesTask(self.db_client, self.log_client, date_str)
  72. await check.deal()
  73. return self.TASK_SUCCESS_STATUS
  74. async def _crawler_toutiao_handler(self) -> int:
  75. sub_task = CrawlerToutiao(self.db_client, self.log_client, self.trace_id)
  76. method = self.data.get("method", "account")
  77. media_type = self.data.get("media_type", "article")
  78. category_list = self.data.get("category_list", [])
  79. match method:
  80. case "account":
  81. await sub_task.crawler_task(media_type=media_type)
  82. case "recommend":
  83. await sub_task.crawl_toutiao_recommend_task(category_list)
  84. case "search":
  85. await sub_task.search_candidate_accounts()
  86. case _:
  87. raise ValueError(f"Unsupported method {method}")
  88. return self.TASK_SUCCESS_STATUS
  89. async def _article_pool_cold_start_handler(self) -> int:
  90. cold_start = ArticlePoolColdStart(
  91. self.db_client, self.log_client, self.trace_id
  92. )
  93. platform = self.data.get("platform", "weixin")
  94. crawler_methods = self.data.get("crawler_methods", [])
  95. category_list = self.data.get("category_list", [])
  96. strategy = self.data.get("strategy", "strategy_v1")
  97. await cold_start.deal(platform=platform, crawl_methods=crawler_methods, category_list=category_list, strategy=strategy)
  98. return self.TASK_SUCCESS_STATUS
  99. async def _candidate_account_quality_score_handler(self) -> int:
  100. task = CandidateAccountQualityScoreRecognizer(
  101. self.db_client, self.log_client, self.trace_id
  102. )
  103. await task.deal()
  104. return self.TASK_SUCCESS_STATUS
  105. async def _article_pool_category_generation_handler(self) -> int:
  106. task = ArticlePoolCategoryGeneration(
  107. self.db_client, self.log_client, self.trace_id
  108. )
  109. limit_num = self.data.get("limit")
  110. await task.deal(limit=limit_num)
  111. return self.TASK_SUCCESS_STATUS
  112. async def _crawler_account_manager_handler(self) -> int:
  113. platform = self.data.get("platform", "weixin")
  114. account_id_list = self.data.get("account_id_list")
  115. match platform:
  116. case "weixin":
  117. task = WeixinAccountManager(
  118. self.db_client, self.log_client, self.trace_id
  119. )
  120. case _:
  121. raise ValueError(f"Unsupported platform {platform}")
  122. await task.deal(platform=platform, account_id_list=account_id_list)
  123. return self.TASK_SUCCESS_STATUS
  124. async def _crawler_gzh_article_handler(self) -> int:
  125. account_method = self.data.get("account_method")
  126. crawl_mode = self.data.get("crawl_mode")
  127. strategy = self.data.get("strategy")
  128. match crawl_mode:
  129. case "account":
  130. task = CrawlerGzhAccountArticles(
  131. self.db_client, self.log_client, self.trace_id
  132. )
  133. await task.deal(account_method, strategy)
  134. case "search":
  135. task = CrawlerGzhSearchArticles(
  136. self.db_client, self.log_client, self.trace_id
  137. )
  138. await task.deal(strategy)
  139. case _:
  140. raise ValueError(f"Unsupported crawl mode {crawl_mode}")
  141. return self.TASK_SUCCESS_STATUS
  142. async def _recycle_fwh_article_handler(self) -> int:
  143. task = RecycleFwhDailyPublishArticlesTask(self.db_client, self.log_client)
  144. await task.deal()
  145. return self.TASK_SUCCESS_STATUS
  146. async def _account_category_analysis_handler(self) -> int:
  147. task = AccountCategoryAnalysis(
  148. pool=self.db_client, log_client=self.log_client, trace_id=self.trace_id, data=self.data, date_string=None
  149. )
  150. await task.deal()
  151. return self.TASK_SUCCESS_STATUS