Jelajahi Sumber

feat:添加根据抖音账号ID爬取计划的逻辑

ZhaoHP 13 jam lalu
induk
melakukan
d309feccee
1 mengubah file dengan 117 tambahan dan 0 penghapusan
  1. 117 0
      examples/content_finder/tools/aigc_platform_api.py

+ 117 - 0
examples/content_finder/tools/aigc_platform_api.py

@@ -0,0 +1,117 @@
+"""
+AIGC接口调用
+调用AIGC接口创建爬取计划,绑定生成计划
+"""
+import logging
+
+import requests
+
+from agent import ToolResult
+
+logger = logging.getLogger(__name__)
+
+AIGC_BASE_URL = "https://aigc-api.aiddit.com"
+CRAWLER_PLAN_CREATE_URL = f"{AIGC_BASE_URL}/aigc/crawler/plan/save"
+DEFAULT_TOKEN = "8bf14f27fc3a486788f3383452422d72"
+DEFAULT_TIMEOUT = 60.0
+
+
+async def create_crawler_plan_by_douyin_account_id(
+        account_id: str,
+        sort_type: str = "最新"
+) -> ToolResult:
+    """
+     根据抖音账号ID创建爬取计划
+     Args:
+         account_id: 抖音账号ID
+         sort_type: 搜索时的视频排序方式(最新/最热),默认最新
+
+     Returns:
+         ToolResult: 包含以下内容
+             - output: 创建的爬取计划ID
+    """
+
+    # 验证 account_id 格式
+    if not account_id or not isinstance(account_id, str):
+        logger.error("create_crawler_plan_by_douyin_account_id invalid account_id", extra={"account_id": account_id})
+        return ToolResult(
+            title="根据抖音账号ID创建爬取计划失败",
+            output="",
+            error="account_id 参数无效:必须是非空字符串",
+        )
+
+    if not account_id.startswith("MS4wLjABAAAA"):
+        logger.error("create_crawler_plan_by_douyin_account_id invalid sec_uid format", extra={"account_id": account_id})
+        return ToolResult(
+            title="根据抖音账号ID创建爬取计划失败",
+            output="",
+            error=f"account_id 格式错误:必须以 MS4wLjABAAAA 开头,当前值: {account_id[:min(20, len(account_id))]}...",
+        )
+
+    if len(account_id) < 70 or len(account_id) > 90:
+        logger.error("create_crawler_plan_by_douyin_account_id invalid account_id length", extra={"account_id": account_id, "length": len(account_id)})
+        return ToolResult(
+            title="根据抖音账号ID创建爬取计划失败",
+            output="",
+            error=f"account_id 长度异常:期望 70-90 字符,实际 {len(account_id)} 字符。这可能是编造或截断的数据。",
+        )
+
+    params = {
+        "baseInfo": {
+            "token": DEFAULT_TOKEN,
+            "userName": ""
+        },
+        "params": {
+            "accountFilters": [],
+            "channel": 2,
+            "contentFilters": [],
+            "contentModal": 4,
+            "crawlerComment": 0,
+            "crawlerMode": 4,
+            "filterAccountMatchMode": 2,
+            "filterContentMatchMode": 2,
+            "frequencyType": 1,
+            "inputModeValues": [
+                account_id
+            ],
+            "modelValueConfig": {
+                "sortType": sort_type
+            },
+            "name": f"【Agent自动创建】抖音账号ID爬取计划_{account_id[:min(30, len(account_id))]}",
+            "planType": 2,
+            "searchModeValues": [],
+            "selectModeValues": [],
+            "srtExtractFlag": 1,
+            "videoKeyFrameType": 1,
+            "voiceExtractFlag": 1
+        }
+    }
+    try:
+        response = requests.post(
+            CRAWLER_PLAN_CREATE_URL,
+            json=params,
+            headers={"Content-Type": "application/json"},
+            timeout=DEFAULT_TIMEOUT
+        )
+        response.raise_for_status()
+        response_json = response.json()
+        if response_json.get("code") != 0:
+            return ToolResult(
+                title="根据抖音账号ID创建爬取计划失败",
+                output=response_json.get("msg", "接口异常"),
+                error=f"create crawler plan interface error",
+            )
+
+        crawler_plan_id = response_json.get("data", {}).get("id", "")
+        return ToolResult(
+            title="根据抖音账号ID创建爬取计划",
+            output=crawler_plan_id,
+            long_term_memory="Create crawler plan by DouYin Account ID",
+        )
+    except Exception as e:
+        logger.error(e, extra={"account_id": account_id})
+        return ToolResult(
+            title="根据抖音账号ID创建爬取计划失败",
+            output="",
+            error=f"创建爬取计划错误:{str(e)}",
+        )