.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
invidious.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Invidious (Videos)
3
4If you want to use invidious with SearXNG you should setup one locally.
5No public instance offer a public API now
6
7- https://github.com/searxng/searxng/issues/2722#issuecomment-2884993248
8
9"""
10
11import time
12import random
13from urllib.parse import quote_plus, urlparse
14from dateutil import parser
15
16from searx.utils import humanize_number
17
18# about
19about = {
20 "website": 'https://api.invidious.io/',
21 "wikidata_id": 'Q79343316',
22 "official_api_documentation": 'https://docs.invidious.io/api/',
23 "use_official_api": True,
24 "require_api_key": False,
25 "results": 'JSON',
26}
27
28# engine dependent config
29categories = ["videos", "music"]
30paging = True
31time_range_support = True
32
33# base_url can be overwritten by a list of URLs in the settings.yml
34base_url: list | str = []
35
36
37def init(_):
38 if not base_url:
39 raise ValueError("missing invidious base_url")
40
41
42def request(query, params):
43 time_range_dict = {
44 "day": "today",
45 "week": "week",
46 "month": "month",
47 "year": "year",
48 }
49
50 if isinstance(base_url, list):
51 params["base_url"] = random.choice(base_url)
52 else:
53 params["base_url"] = base_url
54
55 search_url = params["base_url"] + "/api/v1/search?q={query}"
56 params["url"] = search_url.format(query=quote_plus(query)) + "&page={pageno}".format(pageno=params["pageno"])
57
58 if params["time_range"] in time_range_dict:
59 params["url"] += "&date={timerange}".format(timerange=time_range_dict[params["time_range"]])
60
61 if params["language"] != "all":
62 lang = params["language"].split("-")
63 if len(lang) == 2:
64 params["url"] += "&range={lrange}".format(lrange=lang[1])
65
66 return params
67
68
69def response(resp):
70 results = []
71
72 search_results = resp.json()
73 base_invidious_url = resp.search_params['base_url'] + "/watch?v="
74
75 for result in search_results:
76 rtype = result.get("type", None)
77 if rtype == "video":
78 videoid = result.get("videoId", None)
79 if not videoid:
80 continue
81
82 url = base_invidious_url + videoid
83 thumbs = result.get("videoThumbnails", [])
84 thumb = next((th for th in thumbs if th["quality"] == "sddefault"), None)
85 if thumb:
86 thumbnail = thumb.get("url", "")
87 else:
88 thumbnail = ""
89
90 # some instances return a partial thumbnail url
91 # we check if the url is partial, and prepend the base_url if it is
92 if thumbnail and not urlparse(thumbnail).netloc:
93 thumbnail = resp.search_params['base_url'] + thumbnail
94
95 publishedDate = parser.parse(time.ctime(result.get("published", 0)))
96 length = time.gmtime(result.get("lengthSeconds"))
97 if length.tm_hour:
98 length = time.strftime("%H:%M:%S", length)
99 else:
100 length = time.strftime("%M:%S", length)
101
102 results.append(
103 {
104 "url": url,
105 "title": result.get("title", ""),
106 "content": result.get("description", ""),
107 "length": length,
108 "views": humanize_number(result['viewCount']),
109 "template": "videos.html",
110 "author": result.get("author"),
111 "publishedDate": publishedDate,
112 "iframe_src": resp.search_params['base_url'] + '/embed/' + videoid,
113 "thumbnail": thumbnail,
114 }
115 )
116
117 return results
request(query, params)
Definition invidious.py:42