1 from __future__
import unicode_literals
7 from .discoverygo
import DiscoveryGoBaseIE
8 from ..compat
import compat_urllib_parse_unquote
9 from ..utils
import ExtractorError
10 from ..compat
import compat_HTTPError
13 class DiscoveryIE(DiscoveryGoBaseIE
):
14 _VALID_URL
= r
'''(?x)https?://
16 (?:(?:www|go)\.)?discovery|
19 investigationdiscovery|
37 )\.com/tv-shows/(?P<show_slug>[^/]+)/(?:video|full-episode)s/(?P<id>[^./?#]+)'''
39 'url': 'https://go.discovery.com/tv-shows/cash-cab/videos/riding-with-matthew-perry',
41 'id': '5a2f35ce6b66d17a5026e29e',
43 'title': 'Riding with Matthew Perry',
44 'description': 'md5:a34333153e79bc4526019a5129e7f878',
48 'skip_download': True, # requires ffmpeg
51 'url': 'https://www.investigationdiscovery.com/tv-shows/final-vision/full-episodes/final-vision',
52 'only_matching': True,
54 'url': 'https://go.discovery.com/tv-shows/alaskan-bush-people/videos/follow-your-own-road',
55 'only_matching': True,
57 # using `show_slug` is important to get the correct video data
58 'url': 'https://www.sciencechannel.com/tv-shows/mythbusters-on-science/full-episodes/christmas-special',
59 'only_matching': True,
61 _GEO_COUNTRIES
= ['US']
63 _API_BASE_URL
= 'https://api.discovery.com/v1/'
65 def _real_extract(self
, url
):
66 site
, show_slug
, display_id
= re
.match(self
._VALID
_URL
, url
).groups()
69 cookies
= self
._get
_cookies
(url
)
71 # prefer Affiliate Auth Token over Anonymous Auth Token
72 auth_storage_cookie
= cookies
.get('eosAf') or cookies
.get('eosAn')
73 if auth_storage_cookie
and auth_storage_cookie
.value
:
74 auth_storage
= self
._parse
_json
(compat_urllib_parse_unquote(
75 compat_urllib_parse_unquote(auth_storage_cookie
.value
)),
76 display_id
, fatal
=False) or {}
77 access_token
= auth_storage
.get('a') or auth_storage
.get('access_token')
80 access_token
= self
._download
_json
(
81 'https://%s.com/anonymous' % site
, display_id
,
82 'Downloading token JSON metadata', query
={
83 'authRel': 'authorization',
84 'client_id': '3020a40c2356a645b4b4',
85 'nonce': ''.join([random
.choice(string
.ascii_letters
) for _
in range(32)]),
86 'redirectUri': 'https://fusion.ddmcdn.com/app/mercury-sdk/180/redirectHandler.html?https://www.%s.com' % site
,
89 headers
= self
.geo_verification_headers()
90 headers
['Authorization'] = 'Bearer ' + access_token
93 video
= self
._download
_json
(
94 self
._API
_BASE
_URL
+ 'content/videos',
95 display_id
, 'Downloading content JSON metadata',
96 headers
=headers
, query
={
98 'fields': 'authenticated,description.detailed,duration,episodeNumber,id,name,parental.rating,season.number,show,tags',
100 'show_slug': show_slug
,
102 video_id
= video
['id']
103 stream
= self
._download
_json
(
104 self
._API
_BASE
_URL
+ 'streaming/video/' + video_id
,
105 display_id
, 'Downloading streaming JSON metadata', headers
=headers
)
106 except ExtractorError
as e
:
107 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
in (401, 403):
108 e_description
= self
._parse
_json
(
109 e
.cause
.read().decode(), display_id
)['description']
110 if 'resource not available for country' in e_description
:
111 self
.raise_geo_restricted(countries
=self
._GEO
_COUNTRIES
)
112 if 'Authorized Networks' in e_description
:
113 raise ExtractorError(
114 'This video is only available via cable service provider subscription that'
115 ' is not currently supported. You may want to use --cookies.', expected
=True)
116 raise ExtractorError(e_description
)
119 return self
._extract
_video
_info
(video
, stream
, display_id
)