dailymotion.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. # 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=title,description,duration,url,thumbnail_360_url&sort=relevance&limit=5&page={pageno}&{query}' # noqa
  21. # do search-request
  22. def request(query, params):
  23. if params['language'] == 'all':
  24. locale = 'en-US'
  25. else:
  26. locale = params['language']
  27. params['url'] = search_url.format(
  28. query=urlencode({'search': query, 'localization': locale}),
  29. pageno=params['pageno'])
  30. return params
  31. # get response from search-request
  32. def response(resp):
  33. results = []
  34. search_res = loads(resp.text)
  35. # return empty array if there are no results
  36. if not 'list' in search_res:
  37. return []
  38. # parse results
  39. for res in search_res['list']:
  40. title = res['title']
  41. url = res['url']
  42. #content = res['description']
  43. content = ''
  44. thumbnail = res['thumbnail_360_url']
  45. results.append({'template': 'videos.html',
  46. 'url': url,
  47. 'title': title,
  48. 'content': content,
  49. 'thumbnail': thumbnail})
  50. # return results
  51. return results