iqiyi.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, timedelta
  5. from searx.exceptions import SearxEngineAPIException
  6. about = {
  7. "website": "https://www.iqiyi.com/",
  8. "wikidata_id": "Q15913890",
  9. "use_official_api": False,
  10. "require_api_key": False,
  11. "results": "JSON",
  12. }
  13. paging = True
  14. time_range_support = True
  15. categories = ["videos"]
  16. time_range_dict = {'day': '1', 'week': '2', 'month': '3'}
  17. base_url = "https://mesh.if.iqiyi.com"
  18. def request(query, params):
  19. query_params = {"key": query, "pageNum": params["pageno"], "pageSize": 25}
  20. if time_range_dict.get(params['time_range']):
  21. query_params["sitePublishDate"] = time_range_dict[params['time_range']]
  22. params["url"] = f"{base_url}/portal/lw/search/homePageV3?{urlencode(query_params)}"
  23. return params
  24. def response(resp):
  25. try:
  26. data = resp.json()
  27. except Exception as e:
  28. raise SearxEngineAPIException(f"Invalid response: {e}") from e
  29. results = []
  30. if "data" not in data or "templates" not in data["data"]:
  31. raise SearxEngineAPIException("Invalid response")
  32. for entry in data["data"]["templates"]:
  33. album_info = entry.get("albumInfo", {})
  34. published_date = None
  35. release_time = album_info.get("releaseTime", {}).get("value")
  36. if release_time:
  37. try:
  38. published_date = datetime.strptime(release_time, "%Y-%m-%d")
  39. except (ValueError, TypeError):
  40. pass
  41. length = None
  42. subscript_content = album_info.get("subscriptContent")
  43. if subscript_content:
  44. try:
  45. time_parts = subscript_content.split(":")
  46. if len(time_parts) == 2:
  47. minutes, seconds = map(int, time_parts)
  48. length = timedelta(minutes=minutes, seconds=seconds)
  49. elif len(time_parts) == 3:
  50. hours, minutes, seconds = map(int, time_parts)
  51. length = timedelta(hours=hours, minutes=minutes, seconds=seconds)
  52. except (ValueError, TypeError):
  53. pass
  54. results.append(
  55. {
  56. 'url': album_info.get("pageUrl", "").replace("http://", "https://"),
  57. 'title': album_info.get("title", ""),
  58. 'content': album_info.get("brief", {}).get("value", ""),
  59. 'template': 'videos.html',
  60. 'length': length,
  61. 'publishedDate': published_date,
  62. 'thumbnail': album_info.get("img", ""),
  63. }
  64. )
  65. return results