dailymotion.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. supported_languages = ["af", "ak", "am", "ar", "an", "as", "av", "ae", "ay", "az",
  19. "ba", "bm", "be", "bn", "bi", "bo", "bs", "br", "bg", "ca",
  20. "cs", "ch", "ce", "cu", "cv", "kw", "co", "cr", "cy", "da",
  21. "de", "dv", "dz", "el", "en", "eo", "et", "eu", "ee", "fo",
  22. "fa", "fj", "fi", "fr", "fy", "ff", "gd", "ga", "gl", "gv",
  23. "gn", "gu", "ht", "ha", "sh", "he", "hz", "hi", "ho", "hr",
  24. "hu", "hy", "ig", "io", "ii", "iu", "ie", "ia", "id", "ik",
  25. "is", "it", "jv", "ja", "kl", "kn", "ks", "ka", "kr", "kk",
  26. "km", "ki", "rw", "ky", "kv", "kg", "ko", "kj", "ku", "lo",
  27. "la", "lv", "li", "ln", "lt", "lb", "lu", "lg", "mh", "ml",
  28. "mr", "mk", "mg", "mt", "mn", "mi", "ms", "my", "na", "nv",
  29. "nr", "nd", "ng", "ne", "nl", "nn", "nb", "no", "ny", "oc",
  30. "oj", "or", "om", "os", "pa", "pi", "pl", "pt", "ps", "qu",
  31. "rm", "ro", "rn", "ru", "sg", "sa", "si", "sk", "sl", "se",
  32. "sm", "sn", "sd", "so", "st", "es", "sq", "sc", "sr", "ss",
  33. "su", "sw", "sv", "ty", "ta", "tt", "te", "tg", "tl", "th",
  34. "ti", "to", "tn", "ts", "tk", "tr", "tw", "ug", "uk", "ur",
  35. "uz", "ve", "vi", "vo", "wa", "wo", "xh", "yi", "yo", "za", "zh", "zu"]
  36. # search-url
  37. # see http://www.dailymotion.com/doc/api/obj-video.html
  38. 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
  39. embedded_url = '<iframe frameborder="0" width="540" height="304" ' +\
  40. 'data-src="//www.dailymotion.com/embed/video/{videoid}" allowfullscreen></iframe>'
  41. # do search-request
  42. def request(query, params):
  43. if params['language'] == 'all':
  44. locale = 'en-US'
  45. else:
  46. locale = params['language']
  47. params['url'] = search_url.format(
  48. query=urlencode({'search': query, 'localization': locale}),
  49. pageno=params['pageno'])
  50. return params
  51. # get response from search-request
  52. def response(resp):
  53. results = []
  54. search_res = loads(resp.text)
  55. # return empty array if there are no results
  56. if 'list' not in search_res:
  57. return []
  58. # parse results
  59. for res in search_res['list']:
  60. title = res['title']
  61. url = res['url']
  62. content = res['description']
  63. thumbnail = res['thumbnail_360_url']
  64. publishedDate = datetime.fromtimestamp(res['created_time'], None)
  65. embedded = embedded_url.format(videoid=res['id'])
  66. # http to https
  67. thumbnail = thumbnail.replace("http://", "https://")
  68. results.append({'template': 'videos.html',
  69. 'url': url,
  70. 'title': title,
  71. 'content': content,
  72. 'publishedDate': publishedDate,
  73. 'embedded': embedded,
  74. 'thumbnail': thumbnail})
  75. # return results
  76. return results