task_handler.py 12 KB

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