1 from __future__ 
import unicode_literals
 
   5 from .common 
import InfoExtractor
 
   6 from ..compat 
import compat_str
 
  17 class DiscoveryGoBaseIE(InfoExtractor
): 
  18     _VALID_URL_TEMPLATE 
= r
'''(?x)https?://(?:www\.)?(?: 
  20             investigationdiscovery| 
  28         )go\.com/%s(?P<id>[^/?#&]+)''' 
  31 class DiscoveryGoIE(DiscoveryGoBaseIE
): 
  32     _VALID_URL 
= DiscoveryGoBaseIE
._VALID
_URL
_TEMPLATE 
% r
'(?:[^/]+/)+' 
  33     _GEO_COUNTRIES 
= ['US'] 
  35         'url': 'https://www.discoverygo.com/bering-sea-gold/reaper-madness/', 
  37             'id': '58c167d86b66d12f2addeb01', 
  39             'title': 'Reaper Madness', 
  40             'description': 'md5:09f2c625c99afb8946ed4fb7865f6e78', 
  42             'series': 'Bering Sea Gold', 
  49     def _real_extract(self
, url
): 
  50         display_id 
= self
._match
_id
(url
) 
  52         webpage 
= self
._download
_webpage
(url
, display_id
) 
  54         container 
= extract_attributes( 
  56                 r
'(<div[^>]+class=["\']video
-player
-container
[^
>]+>)', 
  57                 webpage, 'video container
')) 
  59         video = self._parse_json( 
  60             container.get('data
-video
') or container.get('data
-json
'), 
  65         stream = video.get('stream
') 
  67             if video.get('authenticated
') is True: 
  69                     'This video 
is only available via cable service provider subscription that
' 
  70                     ' is not currently supported
. You may want to use 
--cookies
.', expected=True) 
  72                 raise ExtractorError('Unable to find stream
') 
  73         STREAM_URL_SUFFIX = 'streamUrl
' 
  75         for stream_kind in ('', 'hds
'): 
  76             suffix = STREAM_URL_SUFFIX.capitalize() if stream_kind else STREAM_URL_SUFFIX 
  77             stream_url = stream.get('%s%s' % (stream_kind, suffix)) 
  81                 formats.extend(self._extract_m3u8_formats( 
  82                     stream_url, display_id, 'mp4
', entry_protocol='m3u8_native
', 
  83                     m3u8_id='hls
', fatal=False)) 
  84             elif stream_kind == 'hds
': 
  85                 formats.extend(self._extract_f4m_formats( 
  86                     stream_url, display_id, f4m_id=stream_kind, fatal=False)) 
  87         self._sort_formats(formats) 
  89         video_id = video.get('id') or display_id 
  90         description = video.get('description
', {}).get('detailed
') 
  91         duration = int_or_none(video.get('duration
')) 
  93         series = video.get('show
', {}).get('name
') 
  94         season_number = int_or_none(video.get('season
', {}).get('number
')) 
  95         episode_number = int_or_none(video.get('episodeNumber
')) 
  97         tags = video.get('tags
') 
  98         age_limit = parse_age_limit(video.get('parental
', {}).get('rating
')) 
 101         captions = stream.get('captions
') 
 102         if isinstance(captions, list): 
 103             for caption in captions: 
 104                 subtitle_url = caption.get('fileUrl
') 
 105                 if (not subtitle_url or not isinstance(subtitle_url, compat_str) or 
 106                         not subtitle_url.startswith('http
')): 
 108                 lang = caption.get('fileLang
', 'en
') 
 109                 subtitles.setdefault(lang, []).append({'url
': subtitle_url}) 
 113             'display_id
': display_id, 
 115             'description
': description, 
 116             'duration
': duration, 
 118             'season_number
': season_number, 
 119             'episode_number
': episode_number, 
 121             'age_limit
': age_limit, 
 123             'subtitles
': subtitles, 
 127 class DiscoveryGoPlaylistIE(DiscoveryGoBaseIE): 
 128     _VALID_URL = DiscoveryGoBaseIE._VALID_URL_TEMPLATE % '' 
 130         'url
': 'https
://www
.discoverygo
.com
/bering
-sea
-gold
/', 
 132             'id': 'bering
-sea
-gold
', 
 133             'title
': 'Bering Sea Gold
', 
 134             'description
': 'md5
:cc5c6489835949043c0cc3ad66c2fa0e
', 
 136         'playlist_mincount
': 6, 
 140     def suitable(cls, url): 
 141         return False if DiscoveryGoIE.suitable(url) else super( 
 142             DiscoveryGoPlaylistIE, cls).suitable(url) 
 144     def _real_extract(self, url): 
 145         display_id = self._match_id(url) 
 147         webpage = self._download_webpage(url, display_id) 
 150         for mobj in re.finditer(r'data
-json
=(["\'])(?P<json>{.+?})\1', webpage): 
 151             data = self._parse_json( 
 152                 mobj.group('json'), display_id, 
 153                 transform_source=unescapeHTML, fatal=False) 
 154             if not isinstance(data, dict) or data.get('type') != 'episode': 
 156             episode_url = data.get('socialUrl') 
 159             entries.append(self.url_result( 
 160                 episode_url, ie=DiscoveryGoIE.ie_key(), 
 161                 video_id=data.get('id'))) 
 163         return self.playlist_result( 
 165             remove_end(self._og_search_title( 
 166                 webpage, fatal=False), ' | Discovery GO'), 
 167             self._og_search_description(webpage))