dailymotion.py 2.7KB

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