.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
iqiyi.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""iQiyi: A search engine for retrieving videos from iQiyi."""
3
4from urllib.parse import urlencode
5from datetime import datetime, timedelta
6
7from searx.exceptions import SearxEngineAPIException
8
9about = {
10 "website": "https://www.iqiyi.com/",
11 "wikidata_id": "Q15913890",
12 "use_official_api": False,
13 "require_api_key": False,
14 "results": "JSON",
15 "language": "zh",
16}
17
18paging = True
19time_range_support = True
20categories = ["videos"]
21
22time_range_dict = {'day': '1', 'week': '2', 'month': '3'}
23
24base_url = "https://mesh.if.iqiyi.com"
25
26
27def request(query, params):
28 query_params = {"key": query, "pageNum": params["pageno"], "pageSize": 25}
29
30 if time_range_dict.get(params['time_range']):
31 query_params["sitePublishDate"] = time_range_dict[params['time_range']]
32
33 params["url"] = f"{base_url}/portal/lw/search/homePageV3?{urlencode(query_params)}"
34 return params
35
36
37def response(resp):
38 try:
39 data = resp.json()
40 except Exception as e:
41 raise SearxEngineAPIException(f"Invalid response: {e}") from e
42 results = []
43
44 if "data" not in data or "templates" not in data["data"]:
45 raise SearxEngineAPIException("Invalid response")
46
47 for entry in data["data"]["templates"]:
48 album_info = entry.get("albumInfo", {})
49
50 published_date = None
51 release_time = album_info.get("releaseTime", {}).get("value")
52 if release_time:
53 try:
54 published_date = datetime.strptime(release_time, "%Y-%m-%d")
55 except (ValueError, TypeError):
56 pass
57
58 length = None
59 subscript_content = album_info.get("subscriptContent")
60 if subscript_content:
61 try:
62 time_parts = subscript_content.split(":")
63 if len(time_parts) == 2:
64 minutes, seconds = map(int, time_parts)
65 length = timedelta(minutes=minutes, seconds=seconds)
66 elif len(time_parts) == 3:
67 hours, minutes, seconds = map(int, time_parts)
68 length = timedelta(hours=hours, minutes=minutes, seconds=seconds)
69 except (ValueError, TypeError):
70 pass
71
72 results.append(
73 {
74 'url': album_info.get("pageUrl", "").replace("http://", "https://"),
75 'title': album_info.get("title", ""),
76 'content': album_info.get("brief", {}).get("value", ""),
77 'template': 'videos.html',
78 'length': length,
79 'publishedDate': published_date,
80 'thumbnail': album_info.get("img", ""),
81 }
82 )
83
84 return results
request(query, params)
Definition iqiyi.py:27