discoverygo.py 5.8 KB

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