task_handler.py 11 KB

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