You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.7 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. parse_iso8601,
  8. float_or_none,
  9. ExtractorError,
  10. int_or_none,
  11. )
  12. class NineCNineMediaBaseIE(InfoExtractor):
  13. _API_BASE_TEMPLATE = 'http://capi.9c9media.com/destinations/%s/platforms/desktop/contents/%s/'
  14. class NineCNineMediaStackIE(NineCNineMediaBaseIE):
  15. IE_NAME = '9c9media:stack'
  16. _VALID_URL = r'9c9media:stack:(?P<destination_code>[^:]+):(?P<content_id>\d+):(?P<content_package>\d+):(?P<id>\d+)'
  17. def _real_extract(self, url):
  18. destination_code, content_id, package_id, stack_id = re.match(self._VALID_URL, url).groups()
  19. stack_base_url_template = self._API_BASE_TEMPLATE + 'contentpackages/%s/stacks/%s/manifest.'
  20. stack_base_url = stack_base_url_template % (destination_code, content_id, package_id, stack_id)
  21. formats = []
  22. formats.extend(self._extract_m3u8_formats(
  23. stack_base_url + 'm3u8', stack_id, 'mp4',
  24. 'm3u8_native', m3u8_id='hls', fatal=False))
  25. formats.extend(self._extract_f4m_formats(
  26. stack_base_url + 'f4m', stack_id,
  27. f4m_id='hds', fatal=False))
  28. mp4_url = self._download_webpage(stack_base_url + 'pd', stack_id, fatal=False)
  29. if mp4_url:
  30. formats.append({
  31. 'url': mp4_url,
  32. 'format_id': 'mp4',
  33. })
  34. self._sort_formats(formats)
  35. return {
  36. 'id': stack_id,
  37. 'formats': formats,
  38. }
  39. class NineCNineMediaIE(NineCNineMediaBaseIE):
  40. IE_NAME = '9c9media'
  41. _VALID_URL = r'9c9media:(?P<destination_code>[^:]+):(?P<id>\d+)'
  42. def _real_extract(self, url):
  43. destination_code, content_id = re.match(self._VALID_URL, url).groups()
  44. api_base_url = self._API_BASE_TEMPLATE % (destination_code, content_id)
  45. content = self._download_json(api_base_url, content_id, query={
  46. '$include': '[Media,Season,ContentPackages]',
  47. })
  48. title = content['Name']
  49. if len(content['ContentPackages']) > 1:
  50. raise ExtractorError('multiple content packages')
  51. content_package = content['ContentPackages'][0]
  52. package_id = content_package['Id']
  53. content_package_url = api_base_url + 'contentpackages/%s/' % package_id
  54. content_package = self._download_json(content_package_url, content_id)
  55. if content_package.get('Constraints', {}).get('Security', {}).get('Type') == 'adobe-drm':
  56. raise ExtractorError('This video is DRM protected.', expected=True)
  57. stacks = self._download_json(content_package_url + 'stacks/', package_id)['Items']
  58. multistacks = len(stacks) > 1
  59. thumbnails = []
  60. for image in content.get('Images', []):
  61. image_url = image.get('Url')
  62. if not image_url:
  63. continue
  64. thumbnails.append({
  65. 'url': image_url,
  66. 'width': int_or_none(image.get('Width')),
  67. 'height': int_or_none(image.get('Height')),
  68. })
  69. tags, categories = [], []
  70. for source_name, container in (('Tags', tags), ('Genres', categories)):
  71. for e in content.get(source_name, []):
  72. e_name = e.get('Name')
  73. if not e_name:
  74. continue
  75. container.append(e_name)
  76. description = content.get('Desc') or content.get('ShortDesc')
  77. season = content.get('Season', {})
  78. base_info = {
  79. 'description': description,
  80. 'timestamp': parse_iso8601(content.get('BroadcastDateTime')),
  81. 'episode_number': int_or_none(content.get('Episode')),
  82. 'season': season.get('Name'),
  83. 'season_number': season.get('Number'),
  84. 'season_id': season.get('Id'),
  85. 'series': content.get('Media', {}).get('Name'),
  86. 'tags': tags,
  87. 'categories': categories,
  88. }
  89. entries = []
  90. for stack in stacks:
  91. stack_id = compat_str(stack['Id'])
  92. entry = {
  93. '_type': 'url_transparent',
  94. 'url': '9c9media:stack:%s:%s:%s:%s' % (destination_code, content_id, package_id, stack_id),
  95. 'id': stack_id,
  96. 'title': '%s_part%s' % (title, stack['Name']) if multistacks else title,
  97. 'duration': float_or_none(stack.get('Duration')),
  98. 'ie_key': 'NineCNineMediaStack',
  99. }
  100. entry.update(base_info)
  101. entries.append(entry)
  102. return {
  103. '_type': 'multi_video',
  104. 'id': content_id,
  105. 'title': title,
  106. 'description': description,
  107. 'entries': entries,
  108. }