dailymotion.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. ## Dailymotion (Videos)
  2. #
  3. # @website https://www.dailymotion.com
  4. # @provide-api yes (http://www.dailymotion.com/developer)
  5. #
  6. # @using-api yes
  7. # @results JSON
  8. # @stable yes
  9. # @parse url, title, thumbnail
  10. #
  11. # @todo set content-parameter with correct data
  12. from urllib import urlencode
  13. from json import loads
  14. from lxml import html
  15. # engine dependent config
  16. categories = ['videos']
  17. locale = 'en_US'
  18. paging = True
  19. # search-url
  20. # see http://www.dailymotion.com/doc/api/obj-video.html
  21. search_url = 'https://api.dailymotion.com/videos?fields=title,description,duration,url,thumbnail_360_url&sort=relevance&limit=5&page={pageno}&{query}' # noqa
  22. # do search-request
  23. def request(query, params):
  24. params['url'] = search_url.format(
  25. query=urlencode({'search': query, 'localization': locale}),
  26. pageno=params['pageno'])
  27. return params
  28. # get response from search-request
  29. def response(resp):
  30. results = []
  31. search_res = loads(resp.text)
  32. # return empty array if there are no results
  33. if not 'list' in search_res:
  34. return []
  35. # parse results
  36. for res in search_res['list']:
  37. title = res['title']
  38. url = res['url']
  39. #content = res['description']
  40. content = ''
  41. thumbnail = res['thumbnail_360_url']
  42. results.append({'template': 'videos.html',
  43. 'url': url,
  44. 'title': title,
  45. 'content': content,
  46. 'thumbnail': thumbnail})
  47. # return results
  48. return results