task_handler.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from datetime import datetime
  2. from applications.tasks.cold_start_tasks import ArticlePoolColdStart
  3. from applications.tasks.crawler_tasks import CrawlerToutiao
  4. from applications.tasks.crawler_tasks import WeixinAccountManager
  5. from applications.tasks.data_recycle_tasks import RecycleDailyPublishArticlesTask
  6. from applications.tasks.data_recycle_tasks import CheckDailyPublishArticlesTask
  7. from applications.tasks.data_recycle_tasks import UpdateRootSourceIdAndUpdateTimeTask
  8. from applications.tasks.llm_tasks import TitleRewrite
  9. from applications.tasks.llm_tasks import ArticlePoolCategoryGeneration
  10. from applications.tasks.llm_tasks import CandidateAccountQualityScoreRecognizer
  11. from applications.tasks.monitor_tasks import check_kimi_balance
  12. from applications.tasks.monitor_tasks import GetOffVideos
  13. from applications.tasks.monitor_tasks import CheckVideoAuditStatus
  14. from applications.tasks.monitor_tasks import InnerGzhArticlesMonitor
  15. from applications.tasks.monitor_tasks import OutsideGzhArticlesMonitor
  16. from applications.tasks.monitor_tasks import OutsideGzhArticlesCollector
  17. from applications.tasks.monitor_tasks import TaskProcessingMonitor
  18. from applications.tasks.task_mapper import TaskMapper
  19. class TaskHandler(TaskMapper):
  20. def __init__(self, data, log_service, db_client, trace_id):
  21. self.data = data
  22. self.log_client = log_service
  23. self.db_client = db_client
  24. self.trace_id = trace_id
  25. # ---------- 下面是若干复合任务的局部实现 ----------
  26. async def _check_kimi_balance_handler(self) -> int:
  27. response = await check_kimi_balance()
  28. await self.log_client.log(
  29. contents={
  30. "trace_id": self.trace_id,
  31. "task": "check_kimi_balance",
  32. "data": response,
  33. }
  34. )
  35. return self.TASK_SUCCESS_STATUS
  36. async def _get_off_videos_task_handler(self) -> int:
  37. sub_task = GetOffVideos(self.db_client, self.log_client, self.trace_id)
  38. return await sub_task.deal()
  39. async def _check_video_audit_status_handler(self) -> int:
  40. sub_task = CheckVideoAuditStatus(self.db_client, self.log_client, self.trace_id)
  41. return await sub_task.deal()
  42. async def _task_processing_monitor_handler(self) -> int:
  43. sub_task = TaskProcessingMonitor(self.db_client)
  44. await sub_task.deal()
  45. return self.TASK_SUCCESS_STATUS
  46. async def _inner_gzh_articles_monitor_handler(self) -> int:
  47. sub_task = InnerGzhArticlesMonitor(self.db_client)
  48. return await sub_task.deal()
  49. async def _title_rewrite_handler(self):
  50. sub_task = TitleRewrite(self.db_client, self.log_client)
  51. return await sub_task.deal()
  52. async def _update_root_source_id_handler(self) -> int:
  53. sub_task = UpdateRootSourceIdAndUpdateTimeTask(self.db_client, self.log_client)
  54. await sub_task.deal()
  55. return self.TASK_SUCCESS_STATUS
  56. async def _outside_monitor_handler(self) -> int:
  57. collector = OutsideGzhArticlesCollector(self.db_client)
  58. await collector.deal()
  59. monitor = OutsideGzhArticlesMonitor(self.db_client)
  60. return await monitor.deal() # 应返回 SUCCESS / FAILED
  61. async def _recycle_article_data_handler(self) -> int:
  62. date_str = self.data.get("date_string") or datetime.now().strftime("%Y-%m-%d")
  63. recycle = RecycleDailyPublishArticlesTask(
  64. self.db_client, self.log_client, date_str
  65. )
  66. await recycle.deal()
  67. check = CheckDailyPublishArticlesTask(self.db_client, self.log_client, date_str)
  68. await check.deal()
  69. return self.TASK_SUCCESS_STATUS
  70. async def _crawler_toutiao_handler(self) -> int:
  71. sub_task = CrawlerToutiao(self.db_client, self.log_client, self.trace_id)
  72. method = self.data.get("method", "account")
  73. media_type = self.data.get("media_type", "article")
  74. category_list = self.data.get("category_list", [])
  75. match method:
  76. case "account":
  77. await sub_task.crawler_task(media_type=media_type)
  78. case "recommend":
  79. await sub_task.crawl_toutiao_recommend_task(category_list)
  80. case "search":
  81. await sub_task.search_candidate_accounts()
  82. case _:
  83. raise ValueError(f"Unsupported method {method}")
  84. return self.TASK_SUCCESS_STATUS
  85. async def _article_pool_cold_start_handler(self) -> int:
  86. cold_start = ArticlePoolColdStart(
  87. self.db_client, self.log_client, self.trace_id
  88. )
  89. platform = self.data.get("platform", "weixin")
  90. crawler_methods = self.data.get("crawler_methods", [])
  91. await cold_start.deal(platform=platform, crawl_methods=crawler_methods)
  92. return self.TASK_SUCCESS_STATUS
  93. async def _candidate_account_quality_score_handler(self) -> int:
  94. task = CandidateAccountQualityScoreRecognizer(
  95. self.db_client, self.log_client, self.trace_id
  96. )
  97. await task.deal()
  98. return self.TASK_SUCCESS_STATUS
  99. async def _article_pool_category_generation_handler(self) -> int:
  100. task = ArticlePoolCategoryGeneration(
  101. self.db_client, self.log_client, self.trace_id
  102. )
  103. limit_num = self.data.get("limit")
  104. await task.deal(limit=limit_num)
  105. return self.TASK_SUCCESS_STATUS
  106. async def _crawler_account_manager_handler(self) -> int:
  107. platform = self.data.get("platform", "weixin")
  108. account_id_list = self.data.get("account_id_list")
  109. match platform:
  110. case "weixin":
  111. task = WeixinAccountManager(
  112. self.db_client, self.log_client, self.trace_id
  113. )
  114. case _:
  115. raise ValueError(f"Unsupported platform {platform}")
  116. await task.deal(platform=platform, account_id_list=account_id_list)
  117. return self.TASK_SUCCESS_STATUS