summaryrefslogtreecommitdiff
path: root/searx/engines/invidious.py
blob: 1d6d69f64d561882feacff5f72221a291d449d2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
 Invidious (Videos)
"""

from urllib.parse import quote_plus
from dateutil import parser
import time

# about
about = {
    "website": 'https://instances.invidio.us/',
    "wikidata_id": 'Q79343316',
    "official_api_documentation": 'https://github.com/omarroth/invidious/wiki/API',
    "use_official_api": True,
    "require_api_key": False,
    "results": 'JSON',
}

# engine dependent config
categories = ["videos", "music"]
paging = True
language_support = True
time_range_support = True

# search-url
base_url = "https://invidio.us/"


# do search-request
def request(query, params):
    time_range_dict = {
        "day": "today",
        "week": "week",
        "month": "month",
        "year": "year",
    }
    search_url = base_url + "api/v1/search?q={query}"
    params["url"] = search_url.format(
        query=quote_plus(query)
    ) + "&page={pageno}".format(pageno=params["pageno"])

    if params["time_range"] in time_range_dict:
        params["url"] += "&date={timerange}".format(
            timerange=time_range_dict[params["time_range"]]
        )

    if params["language"] != "all":
        lang = params["language"].split("-")
        if len(lang) == 2:
            params["url"] += "&range={lrange}".format(lrange=lang[1])

    return params


# get response from search-request
def response(resp):
    results = []

    search_results = resp.json()
    embedded_url = (
        '<iframe width="540" height="304" '
        + 'data-src="'
        + base_url
        + 'embed/{videoid}" '
        + 'frameborder="0" allowfullscreen></iframe>'
    )

    base_invidious_url = base_url + "watch?v="

    for result in search_results:
        rtype = result.get("type", None)
        if rtype == "video":
            videoid = result.get("videoId", None)
            if not videoid:
                continue

            url = base_invidious_url + videoid
            embedded = embedded_url.format(videoid=videoid)
            thumbs = result.get("videoThumbnails", [])
            thumb = next(
                (th for th in thumbs if th["quality"] == "sddefault"), None
            )
            if thumb:
                thumbnail = thumb.get("url", "")
            else:
                thumbnail = ""

            publishedDate = parser.parse(
                time.ctime(result.get("published", 0))
            )
            length = time.gmtime(result.get("lengthSeconds"))
            if length.tm_hour:
                length = time.strftime("%H:%M:%S", length)
            else:
                length = time.strftime("%M:%S", length)

            results.append(
                {
                    "url": url,
                    "title": result.get("title", ""),
                    "content": result.get("description", ""),
                    'length': length,
                    "template": "videos.html",
                    "author": result.get("author"),
                    "publishedDate": publishedDate,
                    "embedded": embedded,
                    "thumbnail": thumbnail,
                }
            )

    return results