dailymotion.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. # engine dependent config
  15. categories = ['videos']
  16. paging = True
  17. language_support = True
  18. # search-url
  19. # see http://www.dailymotion.com/doc/api/obj-video.html
  20. 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
  21. embedded_url = '<iframe frameborder="0" width="540" height="304" ' +\
  22. 'data-src="//www.dailymotion.com/embed/video/{videoid}" allowfullscreen></iframe>'
  23. # do search-request
  24. def request(query, params):
  25. if params['language'] == 'all':
  26. locale = 'en-US'
  27. else:
  28. locale = params['language']
  29. params['url'] = search_url.format(
  30. query=urlencode({'search': query, 'localization': locale}),
  31. pageno=params['pageno'])
  32. return params
  33. # get response from search-request
  34. def response(resp):
  35. results = []
  36. search_res = loads(resp.text)
  37. # return empty array if there are no results
  38. if 'list' not in search_res:
  39. return []
  40. # parse results
  41. for res in search_res['list']:
  42. title = res['title']
  43. url = res['url']
  44. content = res['description']
  45. thumbnail = res['thumbnail_360_url']
  46. publishedDate = datetime.fromtimestamp(res['created_time'], None)
  47. embedded = embedded_url.format(videoid=res['id'])
  48. # http to https
  49. thumbnail = thumbnail.replace("http://", "https://")
  50. results.append({'template': 'videos.html',
  51. 'url': url,
  52. 'title': title,
  53. 'content': content,
  54. 'publishedDate': publishedDate,
  55. 'embedded': embedded,
  56. 'thumbnail': thumbnail})
  57. # return results
  58. return results