.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
peertube.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Peertube and :py:obj:`SepiaSearch <searx.engines.sepiasearch>` do share
3(more or less) the same REST API and the schema of the JSON result is identical.
4
5"""
6
7import re
8from urllib.parse import urlencode
9from datetime import datetime
10from dateutil.parser import parse
11from dateutil.relativedelta import relativedelta
12
13import babel
14
15from searx.network import get # see https://github.com/searxng/searxng/issues/762
16from searx.locales import language_tag
17from searx.utils import html_to_text, humanize_number
18from searx.enginelib.traits import EngineTraits
19
20traits: EngineTraits
21
22about = {
23 # pylint: disable=line-too-long
24 "website": 'https://joinpeertube.org',
25 "wikidata_id": 'Q50938515',
26 "official_api_documentation": 'https://docs.joinpeertube.org/api-rest-reference.html#tag/Search/operation/searchVideos',
27 "use_official_api": True,
28 "require_api_key": False,
29 "results": 'JSON',
30}
31
32# engine dependent config
33categories = ["videos"]
34paging = True
35base_url = "https://peer.tube"
36"""Base URL of the Peertube instance. A list of instances is available at:
37
38- https://instances.joinpeertube.org/instances
39"""
40
41time_range_support = True
42time_range_table = {
43 'day': relativedelta(),
44 'week': relativedelta(weeks=-1),
45 'month': relativedelta(months=-1),
46 'year': relativedelta(years=-1),
47}
48
49safesearch = True
50safesearch_table = {0: 'both', 1: 'false', 2: 'false'}
51
52
53def minute_to_hm(minute):
54 if isinstance(minute, int):
55 return "%d:%02d" % (divmod(minute, 60))
56 return None
57
58
59def request(query, params):
60 """Assemble request for the Peertube API"""
61
62 if not query:
63 return False
64
65 # eng_region = traits.get_region(params['searxng_locale'], 'en_US')
66 eng_lang = traits.get_language(params['searxng_locale'], None)
67
68 params['url'] = (
69 base_url.rstrip("/")
70 + "/api/v1/search/videos?"
71 + urlencode(
72 {
73 'search': query,
74 'searchTarget': 'search-index', # Vidiversum
75 'resultType': 'videos',
76 'start': (params['pageno'] - 1) * 10,
77 'count': 10,
78 # -createdAt: sort by date ascending / createdAt: date descending
79 'sort': '-match', # sort by *match descending*
80 'nsfw': safesearch_table[params['safesearch']],
81 }
82 )
83 )
84
85 if eng_lang is not None:
86 params['url'] += '&languageOneOf[]=' + eng_lang
87 params['url'] += '&boostLanguages[]=' + eng_lang
88
89 if params['time_range'] in time_range_table:
90 time = datetime.now().date() + time_range_table[params['time_range']]
91 params['url'] += '&startDate=' + time.isoformat()
92
93 return params
94
95
96def response(resp):
97 return video_response(resp)
98
99
101 """Parse video response from SepiaSearch and Peertube instances."""
102 results = []
103
104 json_data = resp.json()
105
106 if 'data' not in json_data:
107 return []
108
109 for result in json_data['data']:
110 metadata = [
111 x
112 for x in [
113 result.get('channel', {}).get('displayName'),
114 result.get('channel', {}).get('name') + '@' + result.get('channel', {}).get('host'),
115 ', '.join(result.get('tags', [])),
116 ]
117 if x
118 ]
119
120 results.append(
121 {
122 'url': result['url'],
123 'title': result['name'],
124 'content': html_to_text(result.get('description') or ''),
125 'author': result.get('account', {}).get('displayName'),
126 'length': minute_to_hm(result.get('duration')),
127 'views': humanize_number(result['views']),
128 'template': 'videos.html',
129 'publishedDate': parse(result['publishedAt']),
130 'iframe_src': result.get('embedUrl'),
131 'thumbnail': result.get('thumbnailUrl') or result.get('previewUrl'),
132 'metadata': ' | '.join(metadata),
133 }
134 )
135
136 return results
137
138
139def fetch_traits(engine_traits: EngineTraits):
140 """Fetch languages from peertube's search-index source code.
141
142 See videoLanguages_ in commit `8ed5c729 - Refactor and redesign client`_
143
144 .. _8ed5c729 - Refactor and redesign client:
145 https://framagit.org/framasoft/peertube/search-index/-/commit/8ed5c729
146 .. _videoLanguages:
147 https://framagit.org/framasoft/peertube/search-index/-/commit/8ed5c729#3d8747f9a60695c367c70bb64efba8f403721fad_0_291
148 """
149
150 resp = get(
151 'https://framagit.org/framasoft/peertube/search-index/-/raw/master/client/src/components/Filters.vue',
152 # the response from search-index repository is very slow
153 timeout=60,
154 )
155
156 if not resp.ok: # type: ignore
157 print("ERROR: response from peertube is not OK.")
158 return
159
160 js_lang = re.search(r"videoLanguages \‍(\‍)[^\n]+(.*?)\]", resp.text, re.DOTALL) # type: ignore
161 if not js_lang:
162 print("ERROR: can't determine languages from peertube")
163 return
164
165 for lang in re.finditer(r"\{ id: '([a-z]+)', label:", js_lang.group(1)):
166 eng_tag = lang.group(1)
167 if eng_tag == 'oc':
168 # Occitanis not known by babel, its closest relative is Catalan
169 # but 'ca' is already in the list of engine_traits.languages -->
170 # 'oc' will be ignored.
171 continue
172 try:
173 sxng_tag = language_tag(babel.Locale.parse(eng_tag))
174 except babel.UnknownLocaleError:
175 print("ERROR: %s is unknown by babel" % eng_tag)
176 continue
177
178 conflict = engine_traits.languages.get(sxng_tag)
179 if conflict:
180 if conflict != eng_tag:
181 print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_tag))
182 continue
183 engine_traits.languages[sxng_tag] = eng_tag
184
185 engine_traits.languages['zh_Hans'] = 'zh'
186 engine_traits.languages['zh_Hant'] = 'zh'
request(query, params)
Definition peertube.py:59
fetch_traits(EngineTraits engine_traits)
Definition peertube.py:139