nowness.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. from .brightcove import BrightcoveIE
  4. from .common import InfoExtractor
  5. from ..utils import ExtractorError
  6. from ..compat import compat_urllib_request
  7. class NownessBaseIE(InfoExtractor):
  8. def extract_url_result(self, post):
  9. if post['type'] == 'video':
  10. for media in post['media']:
  11. if media['type'] == 'video':
  12. video_id = media['content']
  13. source = media['source']
  14. if source == 'brightcove':
  15. player_code = self._download_webpage(
  16. 'http://www.nowness.com/iframe?id=%s' % video_id, video_id,
  17. note='Downloading player JavaScript',
  18. errnote='Player download failed')
  19. bc_url = BrightcoveIE._extract_brightcove_url(player_code)
  20. if bc_url is None:
  21. raise ExtractorError('Could not find player definition')
  22. return self.url_result(bc_url, 'Brightcove')
  23. elif source == 'vimeo':
  24. return self.url_result('http://vimeo.com/%s' % video_id, 'Vimeo')
  25. elif source == 'youtube':
  26. return self.url_result(video_id, 'Youtube')
  27. elif source == 'cinematique':
  28. # youtube-dl currently doesn't support cinematique
  29. # return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique')
  30. pass
  31. def api_request(self, url, request_path):
  32. display_id = self._match_id(url)
  33. lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
  34. request = compat_urllib_request.Request('http://api.nowness.com/api/' + request_path % display_id, headers={
  35. 'X-Nowness-Language': lang,
  36. })
  37. json_data = self._download_json(request, display_id)
  38. return display_id, json_data
  39. class NownessIE(NownessBaseIE):
  40. IE_NAME = 'nowness'
  41. _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(?:story|(?:series|category)/[^/]+)/(?P<id>[^/]+?)(?:$|[?#])'
  42. _TESTS = [
  43. {
  44. 'url': 'https://www.nowness.com/story/candor-the-art-of-gesticulation',
  45. 'md5': '068bc0202558c2e391924cb8cc470676',
  46. 'info_dict': {
  47. 'id': '2520295746001',
  48. 'ext': 'mp4',
  49. 'title': 'Candor: The Art of Gesticulation',
  50. 'description': 'Candor: The Art of Gesticulation',
  51. 'thumbnail': 're:^https?://.*\.jpg',
  52. 'uploader': 'Nowness',
  53. }
  54. },
  55. {
  56. 'url': 'https://cn.nowness.com/story/kasper-bjorke-ft-jaakko-eino-kalevi-tnr',
  57. 'md5': 'e79cf125e387216f86b2e0a5b5c63aa3',
  58. 'info_dict': {
  59. 'id': '3716354522001',
  60. 'ext': 'mp4',
  61. 'title': 'Kasper Bjørke ft. Jaakko Eino Kalevi: TNR',
  62. 'description': 'Kasper Bjørke ft. Jaakko Eino Kalevi: TNR',
  63. 'thumbnail': 're:^https?://.*\.jpg',
  64. 'uploader': 'Nowness',
  65. }
  66. },
  67. ]
  68. def _real_extract(self, url):
  69. display_id, post = self.api_request(url, 'post/getBySlug/%s')
  70. return self.extract_url_result(post)
  71. class NownessPlaylistIE(NownessBaseIE):
  72. IE_NAME = 'nowness:playlist'
  73. _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P<id>\d+)'
  74. _TEST = {
  75. 'url': 'https://www.nowness.com/playlist/3286/i-guess-thats-why-they-call-it-the-blues',
  76. 'info_dict':
  77. {
  78. 'id': '3286',
  79. },
  80. 'playlist_mincount': 8,
  81. }
  82. def _real_extract(self, url):
  83. playlist_id, playlist = self.api_request(url, 'post?PlaylistId=%s')
  84. entries = [self.extract_url_result(item) for item in playlist['items']]
  85. return self.playlist_result(entries, playlist_id)
  86. class NownessSerieIE(NownessBaseIE):
  87. IE_NAME = 'nowness:serie'
  88. _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/series/(?P<id>[^/]+?)(?:$|[?#])'
  89. _TEST = {
  90. 'url': 'https://www.nowness.com/series/60-seconds',
  91. 'info_dict':
  92. {
  93. 'id': '60',
  94. },
  95. 'playlist_mincount': 4,
  96. }
  97. def _real_extract(self, url):
  98. display_id, serie = self.api_request(url, 'series/getBySlug/%s')
  99. serie_id = str(serie['id'])
  100. entries = [self.extract_url_result(post) for post in serie['posts']]
  101. return self.playlist_result(entries, serie_id)