task_handler.py 9.3 KB

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