iqiyi.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """iQiyi: A search engine for retrieving videos from iQiyi."""
  3. from urllib.parse import urlencode
  4. from datetime import datetime
  5. from searx.exceptions import SearxEngineAPIException
  6. from searx.utils import parse_duration_string
  7. about = {
  8. "website": "https://www.iqiyi.com/",
  9. "wikidata_id": "Q15913890",
  10. "use_official_api": False,
  11. "require_api_key": False,
  12. "results": "JSON",
  13. "language": "zh",
  14. }
  15. paging = True
  16. time_range_support = True
  17. categories = ["videos"]
  18. time_range_dict = {'day': '1', 'week': '2', 'month': '3'}
  19. base_url = "https://mesh.if.iqiyi.com"
  20. def request(query, params):
  21. query_params = {"key": query, "pageNum": params["pageno"], "pageSize": 25}
  22. if time_range_dict.get(params['time_range']):
  23. query_params["sitePublishDate"] = time_range_dict[params['time_range']]
  24. params["url"] = f"{base_url}/portal/lw/search/homePageV3?{urlencode(query_params)}"
  25. return params
  26. def response(resp):
  27. try:
  28. data = resp.json()
  29. except Exception as e:
  30. raise SearxEngineAPIException(f"Invalid response: {e}") from e
  31. results = []
  32. if "data" not in data or "templates" not in data["data"]:
  33. raise SearxEngineAPIException("Invalid response")
  34. for entry in data["data"]["templates"]:
  35. album_info = entry.get("albumInfo", {})
  36. published_date = None
  37. release_time = album_info.get("releaseTime", {}).get("value")
  38. if release_time:
  39. try:
  40. published_date = datetime.strptime(release_time, "%Y-%m-%d")
  41. except (ValueError, TypeError):
  42. pass
  43. length = parse_duration_string(album_info.get("subscriptionContent"))
  44. results.append(
  45. {
  46. 'url': album_info.get("pageUrl", "").replace("http://", "https://"),
  47. 'title': album_info.get("title", ""),
  48. 'content': album_info.get("brief", {}).get("value", ""),
  49. 'template': 'videos.html',
  50. 'length': length,
  51. 'publishedDate': published_date,
  52. 'thumbnail': album_info.get("img", ""),
  53. }
  54. )
  55. return results