| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  | """Dailymotion (Videos)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-02 15:45:17 +02:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:36:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  | from typing import Set | 
					
						
							|  |  |  | from datetime import datetime, timedelta | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | from urllib.parse import urlencode | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  | import time | 
					
						
							|  |  |  | import babel | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from searx.exceptions import SearxEngineAPIException | 
					
						
							|  |  |  | from searx.network import raise_for_httperror | 
					
						
							|  |  |  | from searx.utils import html_to_text | 
					
						
							| 
									
										
										
										
											2013-12-30 22:42:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://www.dailymotion.com', | 
					
						
							|  |  |  |     "wikidata_id": 'Q769222', | 
					
						
							|  |  |  |     "official_api_documentation": 'https://www.dailymotion.com/developer', | 
					
						
							|  |  |  |     "use_official_api": True, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'JSON', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:36:53 +02:00
										 |  |  | # engine dependent config | 
					
						
							| 
									
										
										
										
											2013-12-30 22:42:37 +01:00
										 |  |  | categories = ['videos'] | 
					
						
							| 
									
										
										
										
											2014-09-01 15:36:53 +02:00
										 |  |  | paging = True | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  | number_of_results = 10 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | time_range_support = True | 
					
						
							|  |  |  | time_delta_dict = { | 
					
						
							|  |  |  |     "day":  timedelta(days=1), | 
					
						
							|  |  |  |     "week": timedelta(days=7), | 
					
						
							|  |  |  |     "month": timedelta(days=31), | 
					
						
							|  |  |  |     "year": timedelta(days=365), | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-12-30 22:42:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  | safesearch = True | 
					
						
							|  |  |  | safesearch_params = {2: '&is_created_for_kids=true', 1: '&is_created_for_kids=true', 0: ''} | 
					
						
							| 
									
										
										
										
											2016-11-06 03:51:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  | # search-url | 
					
						
							|  |  |  | # - https://developers.dailymotion.com/tools/ | 
					
						
							|  |  |  | # - https://www.dailymotion.com/doc/api/obj-video.html | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | result_fields = [ | 
					
						
							|  |  |  |     'allow_embed', | 
					
						
							|  |  |  |     'description', | 
					
						
							|  |  |  |     'title', | 
					
						
							|  |  |  |     'created_time', | 
					
						
							|  |  |  |     'duration', | 
					
						
							|  |  |  |     'url', | 
					
						
							|  |  |  |     'thumbnail_360_url', | 
					
						
							|  |  |  |     'id', | 
					
						
							|  |  |  | ] | 
					
						
							|  |  |  | search_url = ( | 
					
						
							|  |  |  |     'https://api.dailymotion.com/videos?' | 
					
						
							|  |  |  |     'fields={fields}&password_protected={password_protected}&private={private}&sort={sort}&limit={limit}' | 
					
						
							|  |  |  | ).format( | 
					
						
							|  |  |  |     fields=','.join(result_fields), | 
					
						
							|  |  |  |     password_protected= 'false', | 
					
						
							|  |  |  |     private='false', | 
					
						
							|  |  |  |     sort='relevance', | 
					
						
							|  |  |  |     limit=number_of_results, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | iframe_src = "https://www.dailymotion.com/embed/video/{video_id}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # The request query filters by 'languages' & 'country', therefore instead of | 
					
						
							|  |  |  | # fetching only languages we need to fetch locales. | 
					
						
							|  |  |  | supported_languages_url = 'https://api.dailymotion.com/locales' | 
					
						
							| 
									
										
										
										
											2022-04-15 14:31:19 +02:00
										 |  |  | supported_languages_iso639: Set[str] = set() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def init(_engine_settings): | 
					
						
							|  |  |  |     global supported_languages_iso639 | 
					
						
							|  |  |  |     supported_languages_iso639 = set([language.split('_')[0] for language in supported_languages]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 22:59:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-30 22:42:37 +01:00
										 |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2014-09-07 17:14:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  |     if not query: | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     language = params['language'] | 
					
						
							|  |  |  |     if language == 'all': | 
					
						
							|  |  |  |         language = 'en-US' | 
					
						
							|  |  |  |     locale = babel.Locale.parse(language, sep='-') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 14:31:19 +02:00
										 |  |  |     language_iso639 = locale.language | 
					
						
							|  |  |  |     if locale.language not in supported_languages_iso639: | 
					
						
							|  |  |  |         language_iso639 = 'en' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  |     query_args = { | 
					
						
							|  |  |  |         'search': query, | 
					
						
							| 
									
										
										
										
											2022-04-15 14:31:19 +02:00
										 |  |  |         'languages': language_iso639, | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  |         'page':  params['pageno'], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if locale.territory: | 
					
						
							|  |  |  |         localization = locale.language + '_' + locale.territory | 
					
						
							|  |  |  |         if localization in supported_languages: | 
					
						
							|  |  |  |             query_args['country'] = locale.territory | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     time_delta = time_delta_dict.get(params["time_range"]) | 
					
						
							|  |  |  |     if time_delta: | 
					
						
							|  |  |  |         created_after = datetime.now() - time_delta | 
					
						
							|  |  |  |         query_args['created_after'] = datetime.timestamp(created_after) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     query_str = urlencode(query_args) | 
					
						
							|  |  |  |     params['url'] = search_url + '&' + query_str + safesearch_params.get(params['safesearch'], '') | 
					
						
							|  |  |  |     params['raise_for_httperror'] = False | 
					
						
							| 
									
										
										
										
											2014-09-01 15:36:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-30 22:42:37 +01:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:36:53 +02:00
										 |  |  | # get response from search-request | 
					
						
							| 
									
										
										
										
											2013-12-30 22:42:37 +01:00
										 |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2014-09-01 15:36:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  |     search_res = resp.json() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # check for an API error | 
					
						
							|  |  |  |     if 'error' in search_res: | 
					
						
							|  |  |  |         raise SearxEngineAPIException(search_res['error'].get('message')) | 
					
						
							| 
									
										
										
										
											2014-09-01 15:36:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  |     raise_for_httperror(resp) | 
					
						
							| 
									
										
										
										
											2014-09-01 15:36:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # parse results | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  |     for res in search_res.get('list', []): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-30 22:42:37 +01:00
										 |  |  |         title = res['title'] | 
					
						
							|  |  |  |         url = res['url'] | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-31 08:37:51 +02:00
										 |  |  |         content = html_to_text(res['description']) | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  |         if len(content) > 300: | 
					
						
							|  |  |  |             content = content[:300] + '...' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-05 02:04:23 +01:00
										 |  |  |         publishedDate = datetime.fromtimestamp(res['created_time'], None) | 
					
						
							| 
									
										
										
										
											2014-01-05 13:55:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  |         length = time.gmtime(res.get('duration')) | 
					
						
							|  |  |  |         if length.tm_hour: | 
					
						
							|  |  |  |             length = time.strftime("%H:%M:%S", length) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             length = time.strftime("%M:%S", length) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         thumbnail = res['thumbnail_360_url'] | 
					
						
							| 
									
										
										
										
											2015-05-02 11:43:12 +02:00
										 |  |  |         thumbnail = thumbnail.replace("http://", "https://") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  |         item = { | 
					
						
							|  |  |  |             'template': 'videos.html', | 
					
						
							|  |  |  |             'url': url, | 
					
						
							|  |  |  |             'title': title, | 
					
						
							|  |  |  |             'content': content, | 
					
						
							|  |  |  |             'publishedDate': publishedDate, | 
					
						
							|  |  |  |             'length': length, | 
					
						
							|  |  |  |             'thumbnail': thumbnail, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # HINT: no mater what the value is, without API token videos can't shown | 
					
						
							|  |  |  |         # embedded | 
					
						
							|  |  |  |         if res['allow_embed']: | 
					
						
							|  |  |  |             item['iframe_src'] = iframe_src.format(video_id=res['id']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         results.append(item) | 
					
						
							| 
									
										
										
										
											2014-01-19 22:59:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:36:53 +02:00
										 |  |  |     # return results | 
					
						
							|  |  |  |     return results | 
					
						
							| 
									
										
										
										
											2016-11-06 03:51:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # get supported languages from their site | 
					
						
							| 
									
										
										
										
											2016-12-15 07:34:43 +01:00
										 |  |  | def _fetch_supported_languages(resp): | 
					
						
							| 
									
										
										
										
											2022-04-08 11:17:45 +02:00
										 |  |  |     response_json = resp.json() | 
					
						
							|  |  |  |     return [ | 
					
						
							|  |  |  |         item['locale'] | 
					
						
							|  |  |  |         for item in response_json['list'] | 
					
						
							|  |  |  |     ] |