dailymotion.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Dailymotion (Videos)
  4. """
  5. from json import loads
  6. from datetime import datetime
  7. from urllib.parse import urlencode
  8. from searx.utils import match_language, html_to_text
  9. # about
  10. about = {
  11. "website": 'https://www.dailymotion.com',
  12. "wikidata_id": 'Q769222',
  13. "official_api_documentation": 'https://www.dailymotion.com/developer',
  14. "use_official_api": True,
  15. "require_api_key": False,
  16. "results": 'JSON',
  17. }
  18. # engine dependent config
  19. categories = ['videos']
  20. paging = True
  21. # search-url
  22. # see http://www.dailymotion.com/doc/api/obj-video.html
  23. search_url = 'https://api.dailymotion.com/videos?fields=created_time,title,description,duration,url,thumbnail_360_url,id&sort=relevance&limit=5&page={pageno}&{query}' # noqa
  24. embedded_url = '<iframe frameborder="0" width="540" height="304" ' +\
  25. 'data-src="https://www.dailymotion.com/embed/video/{videoid}" allowfullscreen></iframe>'
  26. supported_languages_url = 'https://api.dailymotion.com/languages'
  27. # do search-request
  28. def request(query, params):
  29. if params['language'] == 'all':
  30. locale = 'en-US'
  31. else:
  32. locale = match_language(params['language'], supported_languages)
  33. params['url'] = search_url.format(
  34. query=urlencode({'search': query, 'localization': locale}),
  35. pageno=params['pageno'])
  36. return params
  37. # get response from search-request
  38. def response(resp):
  39. results = []
  40. search_res = loads(resp.text)
  41. # return empty array if there are no results
  42. if 'list' not in search_res:
  43. return []
  44. # parse results
  45. for res in search_res['list']:
  46. title = res['title']
  47. url = res['url']
  48. content = html_to_text(res['description'])
  49. thumbnail = res['thumbnail_360_url']
  50. publishedDate = datetime.fromtimestamp(res['created_time'], None)
  51. embedded = embedded_url.format(videoid=res['id'])
  52. # http to https
  53. thumbnail = thumbnail.replace("http://", "https://")
  54. results.append({'template': 'videos.html',
  55. 'url': url,
  56. 'title': title,
  57. 'content': content,
  58. 'publishedDate': publishedDate,
  59. 'embedded': embedded,
  60. 'thumbnail': thumbnail})
  61. # return results
  62. return results
  63. # get supported languages from their site
  64. def _fetch_supported_languages(resp):
  65. supported_languages = {}
  66. response_json = loads(resp.text)
  67. for language in response_json['list']:
  68. supported_languages[language['code']] = {}
  69. name = language['native_name']
  70. if name:
  71. supported_languages[language['code']]['name'] = name
  72. english_name = language['name']
  73. if english_name:
  74. supported_languages[language['code']]['english_name'] = english_name
  75. return supported_languages