@@ -0,0 +1,100 @@
1+# Invidious (Videos)
2+#
3+# @website https://invidio.us/
4+# @provide-api yes (https://github.com/omarroth/invidious/wiki/API)
5+#
6+# @using-api yes
7+# @results JSON
8+# @stable yes
9+# @parse url, title, content, publishedDate, thumbnail, embedded
10+
11+from searx.url_utils import quote_plus
12+from dateutil import parser
13+import time
14+
15+# engine dependent config
16+categories = ["videos", "music"]
17+paging = True
18+language_support = True
19+time_range_support = True
20+
21+# search-url
22+base_url = "https://invidio.us/"
23+
24+
25+# do search-request
26+def request(query, params):
27+ time_range_dict = {
28+ "day": "today",
29+ "week": "week",
30+ "month": "month",
31+ "year": "year",
32+ }
33+ search_url = base_url + "api/v1/search?q={query}"
34+ params["url"] = search_url.format(
35+ query=quote_plus(query)
36+ ) + "&page={pageno}".format(pageno=params["pageno"])
37+
38+ if params["time_range"] in time_range_dict:
39+ params["url"] += "&date={timerange}".format(
40+ timerange=time_range_dict[params["time_range"]]
41+ )
42+
43+ if params["language"] != "all":
44+ lang = params["language"].split("-")
45+ if len(lang) == 2:
46+ params["url"] += "&range={lrange}".format(lrange=lang[1])
47+
48+ return params
49+
50+
51+# get response from search-request
52+def response(resp):
53+ results = []
54+
55+ search_results = resp.json()
56+ embedded_url = (
57+ '<iframe width="540" height="304" '
58+ + 'data-src="'
59+ + base_url
60+ + 'embed/{videoid}" '
61+ + 'frameborder="0" allowfullscreen></iframe>'
62+ )
63+
64+ base_invidious_url = base_url + "watch?v="
65+
66+ for result in search_results:
67+ rtype = result.get("type", None)
68+ if rtype == "video":
69+ videoid = result.get("videoId", None)
70+ if not videoid:
71+ continue
72+
73+ url = base_invidious_url + videoid
74+ embedded = embedded_url.format(videoid=videoid)
75+ thumbs = result.get("videoThumbnails", [])
76+ thumb = next(
77+ (th for th in thumbs if th["quality"] == "sddefault"), None
78+ )
79+ if thumb:
80+ thumbnail = thumb.get("url", "")
81+ else:
82+ thumbnail = ""
83+
84+ publishedDate = parser.parse(
85+ time.ctime(result.get("published", 0))
86+ )
87+
88+ results.append(
89+ {
90+ "url": url,
91+ "title": result.get("title", ""),
92+ "content": result.get("description", ""),
93+ "template": "videos.html",
94+ "publishedDate": publishedDate,
95+ "embedded": embedded,
96+ "thumbnail": thumbnail,
97+ }
98+ )
99+
100+ return results