pq_video_searcher.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from typing import Dict, List
  2. import requests
  3. from pqai_agent.toolkit.base import BaseToolkit
  4. from pqai_agent.toolkit.function_tool import FunctionTool
  5. from pqai_agent.toolkit.tool_registry import register_toolkit
  6. @register_toolkit
  7. class PQVideoSearcher(BaseToolkit):
  8. API_URL = "https://vlogapi.piaoquantv.com/longvideoapi/search/userandvideo/list"
  9. def search_pq_video(self, keywords: List[str]) -> List[Dict]:
  10. """
  11. Search for videos on the PQ Video (票圈视频) platform using keywords.
  12. 使用关键词在票圈视频平台上搜索视频。使用时请按要求提供关键词列表。
  13. Args:
  14. keywords (List[str]): 关键词列表,至多5个词,且必须按相关性从高到低排列,每个词至多4个字。
  15. Returns:
  16. List[Dict]: List of video metadata dictionaries.
  17. """
  18. if not keywords:
  19. return []
  20. keyword = keywords[0]
  21. data = {
  22. 'keyWord': keyword,
  23. 'pageSize': "10",
  24. 'pageNo': "1"
  25. }
  26. try:
  27. response = requests.post(self.API_URL, data=data)
  28. response.raise_for_status() # Raise an error for bad responses
  29. result = response.json()
  30. data = result.get("data", {})
  31. videos = data.get("videos", {}).get("videos", [])
  32. ret = []
  33. for item in videos:
  34. if item.get("auditStatus") == 5 and item.get("transcodeStatus") == 3 and item.get("title"):
  35. ret.append({
  36. "id": item["id"],
  37. "coverImgPath": item["coverImg"]["coverImgPath"],
  38. "title": item["title"],
  39. "totalTime": item["totalTime"],
  40. "videoPath": item["videoPath"],
  41. "playCountFormatStr": item["playCountFormatStr"]
  42. })
  43. ret = ret[:4]
  44. return ret
  45. except Exception as e:
  46. return []
  47. def get_tools(self) -> List[FunctionTool]:
  48. return [FunctionTool(self.search_pq_video)]