task_handler.py 8.1 KB

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