hotspot_profile.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """
  2. 热点宝画像数据工具(示例)
  3. 调用内部爬虫服务获取账号/内容的粉丝画像。
  4. """
  5. import asyncio
  6. import json
  7. from typing import Optional
  8. import httpx
  9. from agent.tools import tool, ToolResult
  10. ACCOUNT_FANS_PORTRAIT_API = "http://crawapi.piaoquantv.com/crawler/dou_yin/re_dian_bao/account_fans_portrait"
  11. CONTENT_FANS_PORTRAIT_API = "http://crawapi.piaoquantv.com/crawler/dou_yin/re_dian_bao/video_like_portrait"
  12. DEFAULT_TIMEOUT = 60.0
  13. @tool(description="获取抖音账号粉丝画像(热点宝),支持选择画像维度")
  14. async def get_account_fans_portrait(
  15. account_id: str,
  16. need_province: bool = False,
  17. need_city: bool = False,
  18. need_city_level: bool = False,
  19. need_gender: bool = False,
  20. need_age: bool = True,
  21. need_phone_brand: bool = False,
  22. need_phone_price: bool = False,
  23. timeout: Optional[float] = None,
  24. ) -> ToolResult:
  25. """
  26. 获取抖音账号粉丝画像
  27. """
  28. try:
  29. payload = {
  30. "account_id": account_id,
  31. "need_province": need_province,
  32. "need_city": need_city,
  33. "need_city_level": need_city_level,
  34. "need_gender": need_gender,
  35. "need_age": need_age,
  36. "need_phone_brand": need_phone_brand,
  37. "need_phone_price": need_phone_price,
  38. }
  39. request_timeout = timeout if timeout is not None else DEFAULT_TIMEOUT
  40. async with httpx.AsyncClient(timeout=request_timeout) as client:
  41. response = await client.post(
  42. ACCOUNT_FANS_PORTRAIT_API,
  43. json=payload,
  44. headers={"Content-Type": "application/json"},
  45. )
  46. response.raise_for_status()
  47. data = response.json()
  48. return ToolResult(
  49. title=f"账号粉丝画像: {account_id}",
  50. output=json.dumps(data, ensure_ascii=False, indent=2),
  51. long_term_memory=f"Fetched fans portrait for account '{account_id}'",
  52. )
  53. except httpx.HTTPStatusError as e:
  54. return ToolResult(
  55. title="账号粉丝画像获取失败",
  56. output="",
  57. error=f"HTTP error {e.response.status_code}: {e.response.text}",
  58. )
  59. except Exception as e:
  60. return ToolResult(
  61. title="账号粉丝画像获取失败",
  62. output="",
  63. error=str(e),
  64. )
  65. @tool(description="获取抖音内容粉丝画像(热点宝),支持选择画像维度")
  66. async def get_content_fans_portrait(
  67. content_id: str,
  68. need_province: bool = False,
  69. need_city: bool = False,
  70. need_city_level: bool = False,
  71. need_gender: bool = False,
  72. need_age: bool = True,
  73. need_phone_brand: bool = False,
  74. need_phone_price: bool = False,
  75. timeout: Optional[float] = None,
  76. ) -> ToolResult:
  77. """
  78. 获取抖音内容粉丝画像
  79. """
  80. try:
  81. payload = {
  82. "content_id": content_id,
  83. "need_province": need_province,
  84. "need_city": need_city,
  85. "need_city_level": need_city_level,
  86. "need_gender": need_gender,
  87. "need_age": need_age,
  88. "need_phone_brand": need_phone_brand,
  89. "need_phone_price": need_phone_price,
  90. }
  91. request_timeout = timeout if timeout is not None else DEFAULT_TIMEOUT
  92. async with httpx.AsyncClient(timeout=request_timeout) as client:
  93. response = await client.post(
  94. CONTENT_FANS_PORTRAIT_API,
  95. json=payload,
  96. headers={"Content-Type": "application/json"},
  97. )
  98. response.raise_for_status()
  99. data = response.json()
  100. return ToolResult(
  101. title=f"内容粉丝画像: {content_id}",
  102. output=json.dumps(data, ensure_ascii=False, indent=2),
  103. long_term_memory=f"Fetched fans portrait for content '{content_id}'",
  104. )
  105. except httpx.HTTPStatusError as e:
  106. return ToolResult(
  107. title="内容粉丝画像获取失败",
  108. output="",
  109. error=f"HTTP error {e.response.status_code}: {e.response.text}",
  110. )
  111. except Exception as e:
  112. return ToolResult(
  113. title="内容粉丝画像获取失败",
  114. output="",
  115. error=str(e),
  116. )
  117. # async def main():
  118. # # result = await get_account_fans_portrait(
  119. # # account_id="MS4wLjABAAAAXvRdWJsdPKkh9Ja3ZirxoB8pAaxNXUXs1KUe14gW0IoqDz-D-fG0xZ8c5kSfTPXx"
  120. # # )
  121. # # print(result.output)
  122. # result = await get_content_fans_portrait(
  123. # content_id="7495776724350405928"
  124. # )
  125. # print(result.output)
  126. #
  127. # if __name__ == "__main__":
  128. # asyncio.run(main())