.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
dailymotion.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""
3Dailymotion (Videos)
4~~~~~~~~~~~~~~~~~~~~
5
6.. _REST GET: https://developers.dailymotion.com/tools/
7.. _Global API Parameters: https://developers.dailymotion.com/api/#global-parameters
8.. _Video filters API: https://developers.dailymotion.com/api/#video-filters
9.. _Fields selection: https://developers.dailymotion.com/api/#fields-selection
10
11"""
12
13from typing import TYPE_CHECKING
14
15from datetime import datetime, timedelta
16from urllib.parse import urlencode
17import time
18import babel
19
20from searx.network import get, raise_for_httperror # see https://github.com/searxng/searxng/issues/762
21from searx.utils import html_to_text
22from searx.exceptions import SearxEngineAPIException
23from searx.locales import region_tag, language_tag
24from searx.enginelib.traits import EngineTraits
25
26if TYPE_CHECKING:
27 import logging
28
29 logger: logging.Logger
30
31traits: EngineTraits
32
33# about
34about = {
35 "website": 'https://www.dailymotion.com',
36 "wikidata_id": 'Q769222',
37 "official_api_documentation": 'https://www.dailymotion.com/developer',
38 "use_official_api": True,
39 "require_api_key": False,
40 "results": 'JSON',
41}
42
43# engine dependent config
44categories = ['videos']
45paging = True
46number_of_results = 10
47
48time_range_support = True
49time_delta_dict = {
50 "day": timedelta(days=1),
51 "week": timedelta(days=7),
52 "month": timedelta(days=31),
53 "year": timedelta(days=365),
54}
55
56safesearch = True
57safesearch_params = {
58 2: {'is_created_for_kids': 'true'},
59 1: {'is_created_for_kids': 'true'},
60 0: {},
61}
62"""True if this video is "Created for Kids" / intends to target an audience
63under the age of 16 (``is_created_for_kids`` in `Video filters API`_ )
64"""
65
66family_filter_map = {
67 2: 'true',
68 1: 'true',
69 0: 'false',
70}
71"""By default, the family filter is turned on. Setting this parameter to
72``false`` will stop filtering-out explicit content from searches and global
73contexts (``family_filter`` in `Global API Parameters`_ ).
74"""
75
76result_fields = [
77 'allow_embed',
78 'description',
79 'title',
80 'created_time',
81 'duration',
82 'url',
83 'thumbnail_360_url',
84 'id',
85]
86"""`Fields selection`_, by default, a few fields are returned. To request more
87specific fields, the ``fields`` parameter is used with the list of fields
88SearXNG needs in the response to build a video result list.
89"""
90
91search_url = 'https://api.dailymotion.com/videos?'
92"""URL to retrieve a list of videos.
93
94- `REST GET`_
95- `Global API Parameters`_
96- `Video filters API`_
97"""
98
99iframe_src = "https://www.dailymotion.com/embed/video/{video_id}"
100"""URL template to embed video in SearXNG's result list."""
101
102
103def request(query, params):
104
105 if not query:
106 return False
107
108 eng_region: str = traits.get_region(params['searxng_locale'], 'en_US') # type: ignore
109 eng_lang = traits.get_language(params['searxng_locale'], 'en')
110
111 args = {
112 'search': query,
113 'family_filter': family_filter_map.get(params['safesearch'], 'false'),
114 'thumbnail_ratio': 'original', # original|widescreen|square
115 # https://developers.dailymotion.com/api/#video-filters
116 'languages': eng_lang,
117 'page': params['pageno'],
118 'password_protected': 'false',
119 'private': 'false',
120 'sort': 'relevance',
121 'limit': number_of_results,
122 'fields': ','.join(result_fields),
123 }
124
125 args.update(safesearch_params.get(params['safesearch'], {}))
126
127 # Don't add localization and country arguments if the user does select a
128 # language (:de, :en, ..)
129
130 if len(params['searxng_locale'].split('-')) > 1:
131 # https://developers.dailymotion.com/api/#global-parameters
132 args['localization'] = eng_region
133 args['country'] = eng_region.split('_')[1]
134 # Insufficient rights for the `ams_country' parameter of route `GET /videos'
135 # 'ams_country': eng_region.split('_')[1],
136
137 time_delta = time_delta_dict.get(params["time_range"])
138 if time_delta:
139 created_after = datetime.now() - time_delta
140 args['created_after'] = datetime.timestamp(created_after)
141
142 query_str = urlencode(args)
143 params['url'] = search_url + query_str
144
145 return params
146
147
148# get response from search-request
149def response(resp):
150 results = []
151
152 search_res = resp.json()
153
154 # check for an API error
155 if 'error' in search_res:
156 raise SearxEngineAPIException(search_res['error'].get('message'))
157
158 raise_for_httperror(resp)
159
160 # parse results
161 for res in search_res.get('list', []):
162
163 title = res['title']
164 url = res['url']
165
166 content = html_to_text(res['description'])
167 if len(content) > 300:
168 content = content[:300] + '...'
169
170 publishedDate = datetime.fromtimestamp(res['created_time'], None)
171
172 length = time.gmtime(res.get('duration'))
173 if length.tm_hour:
174 length = time.strftime("%H:%M:%S", length)
175 else:
176 length = time.strftime("%M:%S", length)
177
178 thumbnail = res['thumbnail_360_url']
179 thumbnail = thumbnail.replace("http://", "https://")
180
181 item = {
182 'template': 'videos.html',
183 'url': url,
184 'title': title,
185 'content': content,
186 'publishedDate': publishedDate,
187 'length': length,
188 'thumbnail': thumbnail,
189 }
190
191 # HINT: no mater what the value is, without API token videos can't shown
192 # embedded
193 if res['allow_embed']:
194 item['iframe_src'] = iframe_src.format(video_id=res['id'])
195
196 results.append(item)
197
198 # return results
199 return results
200
201
202def fetch_traits(engine_traits: EngineTraits):
203 """Fetch locales & languages from dailymotion.
204
205 Locales fetched from `api/locales <https://api.dailymotion.com/locales>`_.
206 There are duplications in the locale codes returned from Dailymotion which
207 can be ignored::
208
209 en_EN --> en_GB, en_US
210 ar_AA --> ar_EG, ar_AE, ar_SA
211
212 The language list `api/languages <https://api.dailymotion.com/languages>`_
213 contains over 7000 *languages* codes (see PR1071_). We use only those
214 language codes that are used in the locales.
215
216 .. _PR1071: https://github.com/searxng/searxng/pull/1071
217
218 """
219
220 resp = get('https://api.dailymotion.com/locales')
221 if not resp.ok: # type: ignore
222 print("ERROR: response from dailymotion/locales is not OK.")
223
224 for item in resp.json()['list']: # type: ignore
225 eng_tag = item['locale']
226 if eng_tag in ('en_EN', 'ar_AA'):
227 continue
228 try:
229 sxng_tag = region_tag(babel.Locale.parse(eng_tag))
230 except babel.UnknownLocaleError:
231 print("ERROR: item unknown --> %s" % item)
232 continue
233
234 conflict = engine_traits.regions.get(sxng_tag)
235 if conflict:
236 if conflict != eng_tag:
237 print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_tag))
238 continue
239 engine_traits.regions[sxng_tag] = eng_tag
240
241 locale_lang_list = [x.split('_')[0] for x in engine_traits.regions.values()]
242
243 resp = get('https://api.dailymotion.com/languages')
244 if not resp.ok: # type: ignore
245 print("ERROR: response from dailymotion/languages is not OK.")
246
247 for item in resp.json()['list']: # type: ignore
248 eng_tag = item['code']
249 if eng_tag in locale_lang_list:
250 sxng_tag = language_tag(babel.Locale.parse(eng_tag))
251 engine_traits.languages[sxng_tag] = eng_tag
fetch_traits(EngineTraits engine_traits)