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