task_handler.py 6.7 KB

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