1 from __future__ 
import unicode_literals
 
   7 from .discoverygo 
import DiscoveryGoBaseIE
 
  10     compat_urllib_parse_unquote
, 
  16 from ..compat 
import compat_HTTPError
 
  19 class DiscoveryIE(DiscoveryGoBaseIE
): 
  20     _VALID_URL 
= r
'''(?x)https?:// 
  25                     investigationdiscovery| 
  43         )\.com(?P<path>/tv-shows/[^/]+/(?:video|full-episode)s/(?P<id>[^./?#]+))''' 
  45         'url': 'https://www.discovery.com/tv-shows/cash-cab/videos/dave-foley', 
  47             'id': '5a2d9b4d6b66d17a5026e1fd', 
  49             'title': 'Dave Foley', 
  50             'description': 'md5:4b39bcafccf9167ca42810eb5f28b01f', 
  54             'skip_download': True,  # requires ffmpeg 
  57         'url': 'https://www.investigationdiscovery.com/tv-shows/final-vision/full-episodes/final-vision', 
  58         'only_matching': True, 
  60     _GEO_COUNTRIES 
= ['US'] 
  63     def _real_extract(self
, url
): 
  64         site
, path
, display_id 
= re
.match(self
._VALID
_URL
, url
).groups() 
  65         webpage 
= self
._download
_webpage
(url
, display_id
) 
  67         react_data 
= self
._parse
_json
(self
._search
_regex
( 
  68             r
'window\.__reactTransmitPacket\s*=\s*({.+?});', 
  69             webpage
, 'react data'), display_id
) 
  70         content_blocks 
= react_data
['layout'][path
]['contentBlocks'] 
  71         video 
= next(cb 
for cb 
in content_blocks 
if cb
.get('type') == 'video')['content']['items'][0] 
  72         video_id 
= video
['id'] 
  75         cookies 
= self
._get
_cookies
(url
) 
  77         # prefer Affiliate Auth Token over Anonymous Auth Token 
  78         auth_storage_cookie 
= cookies
.get('eosAf') or cookies
.get('eosAn') 
  79         if auth_storage_cookie 
and auth_storage_cookie
.value
: 
  80             auth_storage 
= self
._parse
_json
(compat_urllib_parse_unquote( 
  81                 compat_urllib_parse_unquote(auth_storage_cookie
.value
)), 
  82                 video_id
, fatal
=False) or {} 
  83             access_token 
= auth_storage
.get('a') or auth_storage
.get('access_token') 
  86             access_token 
= self
._download
_json
( 
  87                 'https://%s.com/anonymous' % site
, display_id
, query
={ 
  88                     'authRel': 'authorization', 
  90                         react_data
, lambda x
: x
['application']['apiClientId'], 
  91                         compat_str
) or '3020a40c2356a645b4b4', 
  92                     'nonce': ''.join([random
.choice(string
.ascii_letters
) for _ 
in range(32)]), 
  93                     'redirectUri': 'https://fusion.ddmcdn.com/app/mercury-sdk/180/redirectHandler.html?https://www.%s.com' % site
, 
  97             headers 
= self
.geo_verification_headers() 
  98             headers
['Authorization'] = 'Bearer ' + access_token
 
 100             stream 
= self
._download
_json
( 
 101                 'https://api.discovery.com/v1/streaming/video/' + video_id
, 
 102                 display_id
, headers
=headers
) 
 103         except ExtractorError 
as e
: 
 104             if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code 
in (401, 403): 
 105                 e_description 
= self
._parse
_json
( 
 106                     e
.cause
.read().decode(), display_id
)['description'] 
 107                 if 'resource not available for country' in e_description
: 
 108                     self
.raise_geo_restricted(countries
=self
._GEO
_COUNTRIES
) 
 109                 if 'Authorized Networks' in e_description
: 
 110                     raise ExtractorError( 
 111                         'This video is only available via cable service provider subscription that' 
 112                         ' is not currently supported. You may want to use --cookies.', expected
=True) 
 113                 raise ExtractorError(e_description
) 
 116         return self
._extract
_video
_info
(video
, stream
, display_id
)