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.

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