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