task_handler.py 8.6 KB

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