logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

discoverygo.py (6079B)


  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. extract_attributes,
  7. ExtractorError,
  8. int_or_none,
  9. parse_age_limit,
  10. remove_end,
  11. unescapeHTML,
  12. url_or_none,
  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 = url_or_none(caption.get('fileUrl'))
  63. if not subtitle_url or not subtitle_url.startswith('http'):
  64. continue
  65. lang = caption.get('fileLang', 'en')
  66. ext = determine_ext(subtitle_url)
  67. subtitles.setdefault(lang, []).append({
  68. 'url': subtitle_url,
  69. 'ext': 'ttml' if ext == 'xml' else ext,
  70. })
  71. return {
  72. 'id': video_id,
  73. 'display_id': display_id,
  74. 'title': title,
  75. 'description': description,
  76. 'duration': duration,
  77. 'series': series,
  78. 'season_number': season_number,
  79. 'episode_number': episode_number,
  80. 'tags': tags,
  81. 'age_limit': age_limit,
  82. 'formats': formats,
  83. 'subtitles': subtitles,
  84. }
  85. class DiscoveryGoIE(DiscoveryGoBaseIE):
  86. _VALID_URL = DiscoveryGoBaseIE._VALID_URL_TEMPLATE % r'(?:[^/]+/)+'
  87. _GEO_COUNTRIES = ['US']
  88. _TEST = {
  89. 'url': 'https://www.discoverygo.com/bering-sea-gold/reaper-madness/',
  90. 'info_dict': {
  91. 'id': '58c167d86b66d12f2addeb01',
  92. 'ext': 'mp4',
  93. 'title': 'Reaper Madness',
  94. 'description': 'md5:09f2c625c99afb8946ed4fb7865f6e78',
  95. 'duration': 2519,
  96. 'series': 'Bering Sea Gold',
  97. 'season_number': 8,
  98. 'episode_number': 6,
  99. 'age_limit': 14,
  100. },
  101. }
  102. def _real_extract(self, url):
  103. display_id = self._match_id(url)
  104. webpage = self._download_webpage(url, display_id)
  105. container = extract_attributes(
  106. self._search_regex(
  107. r'(<div[^>]+class=["\']video-player-container[^>]+>)',
  108. webpage, 'video container'))
  109. video = self._parse_json(
  110. container.get('data-video') or container.get('data-json'),
  111. display_id)
  112. stream = video.get('stream')
  113. return self._extract_video_info(video, stream, display_id)
  114. class DiscoveryGoPlaylistIE(DiscoveryGoBaseIE):
  115. _VALID_URL = DiscoveryGoBaseIE._VALID_URL_TEMPLATE % ''
  116. _TEST = {
  117. 'url': 'https://www.discoverygo.com/bering-sea-gold/',
  118. 'info_dict': {
  119. 'id': 'bering-sea-gold',
  120. 'title': 'Bering Sea Gold',
  121. 'description': 'md5:cc5c6489835949043c0cc3ad66c2fa0e',
  122. },
  123. 'playlist_mincount': 6,
  124. }
  125. @classmethod
  126. def suitable(cls, url):
  127. return False if DiscoveryGoIE.suitable(url) else super(
  128. DiscoveryGoPlaylistIE, cls).suitable(url)
  129. def _real_extract(self, url):
  130. display_id = self._match_id(url)
  131. webpage = self._download_webpage(url, display_id)
  132. entries = []
  133. for mobj in re.finditer(r'data-json=(["\'])(?P<json>{.+?})\1', webpage):
  134. data = self._parse_json(
  135. mobj.group('json'), display_id,
  136. transform_source=unescapeHTML, fatal=False)
  137. if not isinstance(data, dict) or data.get('type') != 'episode':
  138. continue
  139. episode_url = data.get('socialUrl')
  140. if not episode_url:
  141. continue
  142. entries.append(self.url_result(
  143. episode_url, ie=DiscoveryGoIE.ie_key(),
  144. video_id=data.get('id')))
  145. return self.playlist_result(
  146. entries, display_id,
  147. remove_end(self._og_search_title(
  148. webpage, fatal=False), ' | Discovery GO'),
  149. self._og_search_description(webpage))