pq_video_searcher.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. raw_data = '&'.join([f"{k}={v}" for k, v in data.items()])
  25. print(data)
  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)]