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