task_handler.py 5.2 KB

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