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