task_handler.py 5.9 KB

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