.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
piped.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""An alternative privacy-friendly YouTube frontend which is efficient by
3design. `Piped’s architecture`_ consists of 3 components:
4
5- :py:obj:`backend <backend_url>`
6- :py:obj:`frontend <frontend_url>`
7- proxy
8
9.. _Piped’s architecture: https://docs.piped.video/docs/architecture/
10
11Configuration
12=============
13
14The :py:obj:`backend_url` and :py:obj:`frontend_url` has to be set in the engine
15named `piped` and are used by all piped engines
16
17.. code:: yaml
18
19 - name: piped
20 engine: piped
21 piped_filter: videos
22 ...
23 frontend_url: https://..
24 backend_url:
25 - https://..
26 - https://..
27
28 - name: piped.music
29 engine: piped
30 network: piped
31 shortcut: ppdm
32 piped_filter: music_songs
33 ...
34
35Known Quirks
36============
37
38The implementation to support :py:obj:`paging <searx.enginelib.Engine.paging>`
39is based on the *nextpage* method of Piped's REST API / the :py:obj:`frontend
40API <frontend_url>`. This feature is *next page driven* and plays well with the
41:ref:`infinite_scroll <settings ui>` setting in SearXNG but it does not really
42fit into SearXNG's UI to select a page by number.
43
44Implementations
45===============
46"""
47
48
49import time
50import random
51from urllib.parse import urlencode
52import datetime
53from dateutil import parser
54
55from searx.utils import humanize_number
56
57# about
58about = {
59 "website": 'https://github.com/TeamPiped/Piped/',
60 "wikidata_id": 'Q107565255',
61 "official_api_documentation": 'https://docs.piped.video/docs/api-documentation/',
62 "use_official_api": True,
63 "require_api_key": False,
64 "results": 'JSON',
65}
66
67# engine dependent config
68categories = []
69paging = True
70
71# search-url
72backend_url: list | str = "https://pipedapi.kavin.rocks"
73"""Piped-Backend_: The core component behind Piped. The value is an URL or a
74list of URLs. In the latter case instance will be selected randomly. For a
75complete list of official instances see Piped-Instances (`JSON
76<https://piped-instances.kavin.rocks/>`__)
77
78.. _Piped-Instances: https://github.com/TeamPiped/Piped/wiki/Instances
79.. _Piped-Backend: https://github.com/TeamPiped/Piped-Backend
80
81"""
82
83frontend_url: str = "https://piped.video"
84"""Piped-Frontend_: URL to use as link and for embeds.
85
86.. _Piped-Frontend: https://github.com/TeamPiped/Piped
87"""
88
89piped_filter = 'all'
90"""Content filter ``music_songs`` or ``videos``"""
91
92
93def _backend_url() -> str:
94 from searx.engines import engines # pylint: disable=import-outside-toplevel
95
96 url = engines['piped'].backend_url # type: ignore
97 if isinstance(url, list):
98 url = random.choice(url)
99 return url
100
101
102def _frontend_url() -> str:
103 from searx.engines import engines # pylint: disable=import-outside-toplevel
104
105 return engines['piped'].frontend_url # type: ignore
106
107
108def request(query, params):
109
110 args = {
111 'q': query,
112 'filter': piped_filter,
113 }
114
115 path = "/search"
116 if params['pageno'] > 1:
117 # don't use nextpage when user selected to jump back to page 1
118 nextpage = params['engine_data'].get('nextpage')
119 if nextpage:
120 path = "/nextpage/search"
121 args['nextpage'] = nextpage
122
123 params["url"] = _backend_url() + f"{path}?" + urlencode(args)
124 return params
125
126
127def response(resp):
128 results = []
129
130 json = resp.json()
131
132 for result in json["items"]:
133 # note: piped returns -1 for all upload times when filtering for music
134 uploaded = result.get("uploaded", -1)
135
136 item = {
137 # the api url differs from the frontend, hence use piped.video as default
138 "url": _frontend_url() + result.get("url", ""),
139 "title": result.get("title", ""),
140 "publishedDate": parser.parse(time.ctime(uploaded / 1000)) if uploaded != -1 else None,
141 "iframe_src": _frontend_url() + '/embed' + result.get("url", ""),
142 "views": humanize_number(result["views"]),
143 }
144 length = result.get("duration")
145 if length:
146 item["length"] = datetime.timedelta(seconds=length)
147
148 if piped_filter == 'videos':
149 item["template"] = "videos.html"
150 # if the value of shortDescription set, but is None, return empty string
151 item["content"] = result.get("shortDescription", "") or ""
152 item["thumbnail"] = result.get("thumbnail", "")
153
154 elif piped_filter == 'music_songs':
155 item["template"] = "default.html"
156 item["thumbnail"] = result.get("thumbnail", "")
157 item["content"] = result.get("uploaderName", "") or ""
158
159 results.append(item)
160
161 results.append(
162 {
163 "engine_data": json["nextpage"],
164 "key": "nextpage",
165 }
166 )
167 return results
request(query, params)
Definition piped.py:108
str _frontend_url()
Definition piped.py:102
str _backend_url()
Definition piped.py:93
::1337x
Definition 1337x.py:1