2021-01-13 11:31:25 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2022-12-25 15:33:46 +01:00
|
|
|
"""
|
|
|
|
Dailymotion (Videos)
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. _REST GET: https://developers.dailymotion.com/tools/
|
|
|
|
.. _Global API Parameters: https://developers.dailymotion.com/api/#global-parameters
|
|
|
|
.. _Video filters API: https://developers.dailymotion.com/api/#video-filters
|
|
|
|
.. _Fields selection: https://developers.dailymotion.com/api/#fields-selection
|
2022-04-08 11:17:45 +02:00
|
|
|
|
2015-05-02 15:45:17 +02:00
|
|
|
"""
|
2014-09-01 15:36:53 +02:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2022-04-08 11:17:45 +02:00
|
|
|
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
|
|
|
|
|
2023-06-25 12:37:31 +02:00
|
|
|
from searx.network import get, raise_for_httperror # see https://github.com/searxng/searxng/issues/762
|
2022-04-08 11:17:45 +02:00
|
|
|
from searx.utils import html_to_text
|
2023-06-25 12:37:31 +02:00
|
|
|
from searx.exceptions import SearxEngineAPIException
|
2022-12-25 15:33:46 +01:00
|
|
|
from searx.locales import region_tag, language_tag
|
2022-10-03 18:09:37 +02:00
|
|
|
from searx.enginelib.traits import EngineTraits
|
2013-12-30 22:42:37 +01:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger: logging.Logger
|
|
|
|
|
|
|
|
traits: EngineTraits
|
|
|
|
|
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 = {
|
2022-05-07 18:23:10 +02:00
|
|
|
"day": timedelta(days=1),
|
2022-04-08 11:17:45 +02:00
|
|
|
"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
|
2022-12-25 15:33:46 +01:00
|
|
|
safesearch_params = {
|
|
|
|
2: {'is_created_for_kids': 'true'},
|
|
|
|
1: {'is_created_for_kids': 'true'},
|
|
|
|
0: {},
|
|
|
|
}
|
|
|
|
"""True if this video is "Created for Kids" / intends to target an audience
|
|
|
|
under the age of 16 (``is_created_for_kids`` in `Video filters API`_ )
|
|
|
|
"""
|
2016-11-06 03:51:38 +01:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
family_filter_map = {
|
|
|
|
2: 'true',
|
|
|
|
1: 'true',
|
|
|
|
0: 'false',
|
|
|
|
}
|
|
|
|
"""By default, the family filter is turned on. Setting this parameter to
|
|
|
|
``false`` will stop filtering-out explicit content from searches and global
|
|
|
|
contexts (``family_filter`` in `Global API Parameters`_ ).
|
|
|
|
"""
|
2022-04-08 11:17:45 +02:00
|
|
|
|
|
|
|
result_fields = [
|
|
|
|
'allow_embed',
|
|
|
|
'description',
|
|
|
|
'title',
|
|
|
|
'created_time',
|
|
|
|
'duration',
|
|
|
|
'url',
|
|
|
|
'thumbnail_360_url',
|
|
|
|
'id',
|
|
|
|
]
|
2022-12-25 15:33:46 +01:00
|
|
|
"""`Fields selection`_, by default, a few fields are returned. To request more
|
|
|
|
specific fields, the ``fields`` parameter is used with the list of fields
|
|
|
|
SearXNG needs in the response to build a video result list.
|
|
|
|
"""
|
2022-04-08 11:17:45 +02:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
search_url = 'https://api.dailymotion.com/videos?'
|
|
|
|
"""URL to retrieve a list of videos.
|
2022-04-15 14:31:19 +02:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
- `REST GET`_
|
|
|
|
- `Global API Parameters`_
|
|
|
|
- `Video filters API`_
|
|
|
|
"""
|
2022-04-15 14:31:19 +02:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
iframe_src = "https://www.dailymotion.com/embed/video/{video_id}"
|
|
|
|
"""URL template to embed video in SearXNG's result list."""
|
2022-04-15 14:31:19 +02:00
|
|
|
|
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
|
|
|
|
|
2023-06-25 12:37:31 +02:00
|
|
|
eng_region: str = traits.get_region(params['searxng_locale'], 'en_US') # type: ignore
|
2022-12-25 15:33:46 +01:00
|
|
|
eng_lang = traits.get_language(params['searxng_locale'], 'en')
|
2022-04-15 14:31:19 +02:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
args = {
|
2022-04-08 11:17:45 +02:00
|
|
|
'search': query,
|
2022-12-25 15:33:46 +01:00
|
|
|
'family_filter': family_filter_map.get(params['safesearch'], 'false'),
|
|
|
|
'thumbnail_ratio': 'original', # original|widescreen|square
|
|
|
|
# https://developers.dailymotion.com/api/#video-filters
|
|
|
|
'languages': eng_lang,
|
2022-05-07 18:23:10 +02:00
|
|
|
'page': params['pageno'],
|
2022-12-25 15:33:46 +01:00
|
|
|
'password_protected': 'false',
|
|
|
|
'private': 'false',
|
|
|
|
'sort': 'relevance',
|
|
|
|
'limit': number_of_results,
|
|
|
|
'fields': ','.join(result_fields),
|
2022-04-08 11:17:45 +02:00
|
|
|
}
|
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
args.update(safesearch_params.get(params['safesearch'], {}))
|
|
|
|
|
|
|
|
# Don't add localization and country arguments if the user does select a
|
|
|
|
# language (:de, :en, ..)
|
|
|
|
|
|
|
|
if len(params['searxng_locale'].split('-')) > 1:
|
|
|
|
# https://developers.dailymotion.com/api/#global-parameters
|
|
|
|
args['localization'] = eng_region
|
|
|
|
args['country'] = eng_region.split('_')[1]
|
|
|
|
# Insufficient rights for the `ams_country' parameter of route `GET /videos'
|
|
|
|
# 'ams_country': eng_region.split('_')[1],
|
2022-04-08 11:17:45 +02:00
|
|
|
|
|
|
|
time_delta = time_delta_dict.get(params["time_range"])
|
|
|
|
if time_delta:
|
|
|
|
created_after = datetime.now() - time_delta
|
2022-12-25 15:33:46 +01:00
|
|
|
args['created_after'] = datetime.timestamp(created_after)
|
2022-04-08 11:17:45 +02:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
query_str = urlencode(args)
|
|
|
|
params['url'] = search_url + query_str
|
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
|
|
|
|
2023-06-25 12:37:31 +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
|
|
|
|
|
|
|
|
2022-10-03 18:09:37 +02:00
|
|
|
def fetch_traits(engine_traits: EngineTraits):
|
2022-12-25 15:33:46 +01:00
|
|
|
"""Fetch locales & languages from dailymotion.
|
2022-10-03 18:09:37 +02:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
Locales fetched from `api/locales <https://api.dailymotion.com/locales>`_.
|
2022-10-03 18:09:37 +02:00
|
|
|
There are duplications in the locale codes returned from Dailymotion which
|
|
|
|
can be ignored::
|
|
|
|
|
|
|
|
en_EN --> en_GB, en_US
|
|
|
|
ar_AA --> ar_EG, ar_AE, ar_SA
|
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
The language list `api/languages <https://api.dailymotion.com/languages>`_
|
|
|
|
contains over 7000 *languages* codes (see PR1071_). We use only those
|
|
|
|
language codes that are used in the locales.
|
2022-10-03 18:09:37 +02:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
.. _PR1071: https://github.com/searxng/searxng/pull/1071
|
2022-10-03 18:09:37 +02:00
|
|
|
|
2022-12-25 15:33:46 +01:00
|
|
|
"""
|
2022-10-03 18:09:37 +02:00
|
|
|
|
2023-06-25 12:37:31 +02:00
|
|
|
resp = get('https://api.dailymotion.com/locales')
|
|
|
|
if not resp.ok: # type: ignore
|
2022-12-25 15:33:46 +01:00
|
|
|
print("ERROR: response from dailymotion/locales is not OK.")
|
2022-10-03 18:09:37 +02:00
|
|
|
|
2023-06-25 12:37:31 +02:00
|
|
|
for item in resp.json()['list']: # type: ignore
|
2022-10-03 18:09:37 +02:00
|
|
|
eng_tag = item['locale']
|
|
|
|
if eng_tag in ('en_EN', 'ar_AA'):
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
sxng_tag = region_tag(babel.Locale.parse(eng_tag))
|
|
|
|
except babel.UnknownLocaleError:
|
|
|
|
print("ERROR: item unknown --> %s" % item)
|
|
|
|
continue
|
|
|
|
|
|
|
|
conflict = engine_traits.regions.get(sxng_tag)
|
|
|
|
if conflict:
|
|
|
|
if conflict != eng_tag:
|
|
|
|
print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_tag))
|
|
|
|
continue
|
|
|
|
engine_traits.regions[sxng_tag] = eng_tag
|
2022-12-25 15:33:46 +01:00
|
|
|
|
|
|
|
locale_lang_list = [x.split('_')[0] for x in engine_traits.regions.values()]
|
|
|
|
|
2023-06-25 12:37:31 +02:00
|
|
|
resp = get('https://api.dailymotion.com/languages')
|
|
|
|
if not resp.ok: # type: ignore
|
2022-12-25 15:33:46 +01:00
|
|
|
print("ERROR: response from dailymotion/languages is not OK.")
|
|
|
|
|
2023-06-25 12:37:31 +02:00
|
|
|
for item in resp.json()['list']: # type: ignore
|
2022-12-25 15:33:46 +01:00
|
|
|
eng_tag = item['code']
|
|
|
|
if eng_tag in locale_lang_list:
|
|
|
|
sxng_tag = language_tag(babel.Locale.parse(eng_tag))
|
|
|
|
engine_traits.languages[sxng_tag] = eng_tag
|