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.

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