dailymotion.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 cgi import escape
  14. from datetime import datetime
  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. # do search-request
  25. def request(query, params):
  26. if params['language'] == 'all':
  27. locale = 'en-US'
  28. else:
  29. locale = params['language']
  30. params['url'] = search_url.format(
  31. query=urlencode({'search': query, 'localization': locale}),
  32. pageno=params['pageno'])
  33. return params
  34. # get response from search-request
  35. def response(resp):
  36. results = []
  37. search_res = loads(resp.text)
  38. # return empty array if there are no results
  39. if 'list' not in search_res:
  40. return []
  41. # parse results
  42. for res in search_res['list']:
  43. title = res['title']
  44. url = res['url']
  45. content = escape(res['description'])
  46. thumbnail = res['thumbnail_360_url']
  47. publishedDate = datetime.fromtimestamp(res['created_time'], None)
  48. embedded = embedded_url.format(videoid=res['id'])
  49. # http to https
  50. thumbnail = thumbnail.replace("http://", "https://")
  51. results.append({'template': 'videos.html',
  52. 'url': url,
  53. 'title': title,
  54. 'content': content,
  55. 'publishedDate': publishedDate,
  56. 'embedded': embedded,
  57. 'thumbnail': thumbnail})
  58. # return results
  59. return results