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.

176 lines
6.0 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. determine_ext,
  7. extract_attributes,
  8. ExtractorError,
  9. int_or_none,
  10. parse_age_limit,
  11. remove_end,
  12. unescapeHTML,
  13. )
  14. class DiscoveryGoBaseIE(InfoExtractor):
  15. _VALID_URL_TEMPLATE = r'''(?x)https?://(?:www\.)?(?:
  16. discovery|
  17. investigationdiscovery|
  18. discoverylife|
  19. animalplanet|
  20. ahctv|
  21. destinationamerica|
  22. sciencechannel|
  23. tlc|
  24. velocitychannel
  25. )go\.com/%s(?P<id>[^/?#&]+)'''
  26. def _extract_video_info(self, video, stream, display_id):
  27. title = video['name']
  28. if not stream:
  29. if video.get('authenticated') is True:
  30. raise ExtractorError(
  31. 'This video is only available via cable service provider subscription that'
  32. ' is not currently supported. You may want to use --cookies.', expected=True)
  33. else:
  34. raise ExtractorError('Unable to find stream')
  35. STREAM_URL_SUFFIX = 'streamUrl'
  36. formats = []
  37. for stream_kind in ('', 'hds'):
  38. suffix = STREAM_URL_SUFFIX.capitalize() if stream_kind else STREAM_URL_SUFFIX
  39. stream_url = stream.get('%s%s' % (stream_kind, suffix))
  40. if not stream_url:
  41. continue
  42. if stream_kind == '':
  43. formats.extend(self._extract_m3u8_formats(
  44. stream_url, display_id, 'mp4', entry_protocol='m3u8_native',
  45. m3u8_id='hls', fatal=False))
  46. elif stream_kind == 'hds':
  47. formats.extend(self._extract_f4m_formats(
  48. stream_url, display_id, f4m_id=stream_kind, fatal=False))
  49. self._sort_formats(formats)
  50. video_id = video.get('id') or display_id
  51. description = video.get('description', {}).get('detailed')
  52. duration = int_or_none(video.get('duration'))
  53. series = video.get('show', {}).get('name')
  54. season_number = int_or_none(video.get('season', {}).get('number'))
  55. episode_number = int_or_none(video.get('episodeNumber'))
  56. tags = video.get('tags')
  57. age_limit = parse_age_limit(video.get('parental', {}).get('rating'))
  58. subtitles = {}
  59. captions = stream.get('captions')
  60. if isinstance(captions, list):
  61. for caption in captions:
  62. subtitle_url = caption.get('fileUrl')
  63. if (not subtitle_url or not isinstance(subtitle_url, compat_str) or
  64. not subtitle_url.startswith('http')):
  65. continue
  66. lang = caption.get('fileLang', 'en')
  67. ext = determine_ext(subtitle_url)
  68. subtitles.setdefault(lang, []).append({
  69. 'url': subtitle_url,
  70. 'ext': 'ttml' if ext == 'xml' else ext,
  71. })
  72. return {
  73. 'id': video_id,
  74. 'display_id': display_id,
  75. 'title': title,
  76. 'description': description,
  77. 'duration': duration,
  78. 'series': series,
  79. 'season_number': season_number,
  80. 'episode_number': episode_number,
  81. 'tags': tags,
  82. 'age_limit': age_limit,
  83. 'formats': formats,
  84. 'subtitles': subtitles,
  85. }
  86. class DiscoveryGoIE(DiscoveryGoBaseIE):
  87. _VALID_URL = DiscoveryGoBaseIE._VALID_URL_TEMPLATE % r'(?:[^/]+/)+'
  88. _GEO_COUNTRIES = ['US']
  89. _TEST = {
  90. 'url': 'https://www.discoverygo.com/bering-sea-gold/reaper-madness/',
  91. 'info_dict': {
  92. 'id': '58c167d86b66d12f2addeb01',
  93. 'ext': 'mp4',
  94. 'title': 'Reaper Madness',
  95. 'description': 'md5:09f2c625c99afb8946ed4fb7865f6e78',
  96. 'duration': 2519,
  97. 'series': 'Bering Sea Gold',
  98. 'season_number': 8,
  99. 'episode_number': 6,
  100. 'age_limit': 14,
  101. },
  102. }
  103. def _real_extract(self, url):
  104. display_id = self._match_id(url)
  105. webpage = self._download_webpage(url, display_id)
  106. container = extract_attributes(
  107. self._search_regex(
  108. r'(<div[^>]+class=["\']video-player-container[^>]+>)',
  109. webpage, 'video container'))
  110. video = self._parse_json(
  111. container.get('data-video') or container.get('data-json'),
  112. display_id)
  113. stream = video.get('stream')
  114. return self._extract_video_info(video, stream, display_id)
  115. class DiscoveryGoPlaylistIE(DiscoveryGoBaseIE):
  116. _VALID_URL = DiscoveryGoBaseIE._VALID_URL_TEMPLATE % ''
  117. _TEST = {
  118. 'url': 'https://www.discoverygo.com/bering-sea-gold/',
  119. 'info_dict': {
  120. 'id': 'bering-sea-gold',
  121. 'title': 'Bering Sea Gold',
  122. 'description': 'md5:cc5c6489835949043c0cc3ad66c2fa0e',
  123. },
  124. 'playlist_mincount': 6,
  125. }
  126. @classmethod
  127. def suitable(cls, url):
  128. return False if DiscoveryGoIE.suitable(url) else super(
  129. DiscoveryGoPlaylistIE, cls).suitable(url)
  130. def _real_extract(self, url):
  131. display_id = self._match_id(url)
  132. webpage = self._download_webpage(url, display_id)
  133. entries = []
  134. for mobj in re.finditer(r'data-json=(["\'])(?P<json>{.+?})\1', webpage):
  135. data = self._parse_json(
  136. mobj.group('json'), display_id,
  137. transform_source=unescapeHTML, fatal=False)
  138. if not isinstance(data, dict) or data.get('type') != 'episode':
  139. continue
  140. episode_url = data.get('socialUrl')
  141. if not episode_url:
  142. continue
  143. entries.append(self.url_result(
  144. episode_url, ie=DiscoveryGoIE.ie_key(),
  145. video_id=data.get('id')))
  146. return self.playlist_result(
  147. entries, display_id,
  148. remove_end(self._og_search_title(
  149. webpage, fatal=False), ' | Discovery GO'),
  150. self._og_search_description(webpage))