aigc_platform_api.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """
  2. AIGC接口调用
  3. 调用AIGC接口创建爬取计划,绑定生成计划
  4. """
  5. import logging
  6. import requests
  7. from agent import ToolResult
  8. logger = logging.getLogger(__name__)
  9. AIGC_BASE_URL = "https://aigc-api.aiddit.com"
  10. CRAWLER_PLAN_CREATE_URL = f"{AIGC_BASE_URL}/aigc/crawler/plan/save"
  11. DEFAULT_TOKEN = "8bf14f27fc3a486788f3383452422d72"
  12. DEFAULT_TIMEOUT = 60.0
  13. async def create_crawler_plan_by_douyin_account_id(
  14. account_id: str,
  15. sort_type: str = "最新"
  16. ) -> ToolResult:
  17. """
  18. 根据抖音账号ID创建爬取计划
  19. Args:
  20. account_id: 抖音账号ID
  21. sort_type: 搜索时的视频排序方式(最新/最热),默认最新
  22. Returns:
  23. ToolResult: 包含以下内容
  24. - output: 创建的爬取计划ID
  25. """
  26. # 验证 account_id 格式
  27. if not account_id or not isinstance(account_id, str):
  28. logger.error("create_crawler_plan_by_douyin_account_id invalid account_id", extra={"account_id": account_id})
  29. return ToolResult(
  30. title="根据抖音账号ID创建爬取计划失败",
  31. output="",
  32. error="account_id 参数无效:必须是非空字符串",
  33. )
  34. if not account_id.startswith("MS4wLjABAAAA"):
  35. logger.error("create_crawler_plan_by_douyin_account_id invalid sec_uid format", extra={"account_id": account_id})
  36. return ToolResult(
  37. title="根据抖音账号ID创建爬取计划失败",
  38. output="",
  39. error=f"account_id 格式错误:必须以 MS4wLjABAAAA 开头,当前值: {account_id[:min(20, len(account_id))]}...",
  40. )
  41. if len(account_id) < 70 or len(account_id) > 90:
  42. logger.error("create_crawler_plan_by_douyin_account_id invalid account_id length", extra={"account_id": account_id, "length": len(account_id)})
  43. return ToolResult(
  44. title="根据抖音账号ID创建爬取计划失败",
  45. output="",
  46. error=f"account_id 长度异常:期望 70-90 字符,实际 {len(account_id)} 字符。这可能是编造或截断的数据。",
  47. )
  48. params = {
  49. "baseInfo": {
  50. "token": DEFAULT_TOKEN,
  51. "userName": ""
  52. },
  53. "params": {
  54. "accountFilters": [],
  55. "channel": 2,
  56. "contentFilters": [],
  57. "contentModal": 4,
  58. "crawlerComment": 0,
  59. "crawlerMode": 4,
  60. "filterAccountMatchMode": 2,
  61. "filterContentMatchMode": 2,
  62. "frequencyType": 1,
  63. "inputModeValues": [
  64. account_id
  65. ],
  66. "modelValueConfig": {
  67. "sortType": sort_type
  68. },
  69. "name": f"【Agent自动创建】抖音账号ID爬取计划_{account_id[:min(30, len(account_id))]}",
  70. "planType": 2,
  71. "searchModeValues": [],
  72. "selectModeValues": [],
  73. "srtExtractFlag": 1,
  74. "videoKeyFrameType": 1,
  75. "voiceExtractFlag": 1
  76. }
  77. }
  78. try:
  79. response = requests.post(
  80. CRAWLER_PLAN_CREATE_URL,
  81. json=params,
  82. headers={"Content-Type": "application/json"},
  83. timeout=DEFAULT_TIMEOUT
  84. )
  85. response.raise_for_status()
  86. response_json = response.json()
  87. if response_json.get("code") != 0:
  88. return ToolResult(
  89. title="根据抖音账号ID创建爬取计划失败",
  90. output=response_json.get("msg", "接口异常"),
  91. error=f"create crawler plan interface error",
  92. )
  93. crawler_plan_id = response_json.get("data", {}).get("id", "")
  94. return ToolResult(
  95. title="根据抖音账号ID创建爬取计划",
  96. output=crawler_plan_id,
  97. long_term_memory="Create crawler plan by DouYin Account ID",
  98. )
  99. except Exception as e:
  100. logger.error(e, extra={"account_id": account_id})
  101. return ToolResult(
  102. title="根据抖音账号ID创建爬取计划失败",
  103. output="",
  104. error=f"创建爬取计划错误:{str(e)}",
  105. )