pq_video_searcher.py 2.0 KB

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