douyin_user_videos.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. 抖音账号历史作品工具(示例)
  3. 调用内部爬虫服务获取指定账号的历史作品列表。
  4. """
  5. import json
  6. from typing import Optional
  7. import asyncio
  8. import httpx
  9. from agent.tools import tool, ToolResult
  10. # API 基础配置
  11. DOUYIN_BLOGGER_API = "http://crawapi.piaoquantv.com/crawler/dou_yin/blogger"
  12. DEFAULT_TIMEOUT = 60.0
  13. @tool(description="根据账号ID获取抖音历史作品,支持排序与游标")
  14. async def douyin_user_videos(
  15. account_id: str,
  16. sort_type: str = "最新",
  17. cursor: str = "",
  18. timeout: Optional[float] = None,
  19. ) -> ToolResult:
  20. """
  21. 抖音账号历史作品查询
  22. Args:
  23. account_id: 抖音账号ID(account_id)
  24. sort_type: 排序方式(默认 "最新")
  25. cursor: 分页游标(默认 "")
  26. timeout: 超时时间(秒),默认 60
  27. Returns:
  28. ToolResult: 作品列表 JSON
  29. """
  30. try:
  31. payload = {
  32. "account_id": account_id,
  33. "sort_type": sort_type,
  34. "cursor": cursor,
  35. }
  36. request_timeout = timeout if timeout is not None else DEFAULT_TIMEOUT
  37. async with httpx.AsyncClient(timeout=request_timeout) as client:
  38. response = await client.post(
  39. DOUYIN_BLOGGER_API,
  40. json=payload,
  41. headers={"Content-Type": "application/json"},
  42. )
  43. response.raise_for_status()
  44. data = response.json()
  45. return ToolResult(
  46. title=f"账号作品: {account_id}",
  47. output=json.dumps(data, ensure_ascii=False, indent=2),
  48. long_term_memory=f"Fetched Douyin blogger videos for '{account_id}'",
  49. )
  50. except httpx.HTTPStatusError as e:
  51. return ToolResult(
  52. title="账号作品获取失败",
  53. output="",
  54. error=f"HTTP error {e.response.status_code}: {e.response.text}",
  55. )
  56. except Exception as e:
  57. return ToolResult(
  58. title="账号作品获取失败",
  59. output="",
  60. error=str(e),
  61. )
  62. # async def main():
  63. # result = await douyin_user_videos(
  64. # account_id="MS4wLjABAAAAPRCMGPAFM1VGcJrxRuvTXgJp0Sk95EW1DynNmbKSPg8",
  65. # sort_type="最新",
  66. # cursor=""
  67. # )
  68. # print(result.output)
  69. #
  70. # if __name__ == "__main__":
  71. # asyncio.run(main())