task_handler.py 9.7 KB

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