]>
Commit | Line | Data |
---|---|---|
cb0c2310 | 1 | import random |
cb0c2310 RA |
2 | import string |
3 | ||
4 | from .discoverygo import DiscoveryGoBaseIE | |
53511165 | 5 | from ..compat import compat_urllib_parse_unquote |
3d2623a8 | 6 | from ..networking.exceptions import HTTPError |
53511165 | 7 | from ..utils import ExtractorError |
53bfd6b2 | 8 | |
9 | ||
cb0c2310 | 10 | class DiscoveryIE(DiscoveryGoBaseIE): |
7216e9bf RA |
11 | _VALID_URL = r'''(?x)https?:// |
12 | (?P<site> | |
2f7aa680 RA |
13 | go\.discovery| |
14 | www\. | |
7216e9bf | 15 | (?: |
7216e9bf RA |
16 | investigationdiscovery| |
17 | discoverylife| | |
18 | animalplanet| | |
19 | ahctv| | |
20 | destinationamerica| | |
21 | sciencechannel| | |
2f7aa680 | 22 | tlc |
7216e9bf RA |
23 | )| |
24 | watch\. | |
25 | (?: | |
26 | hgtv| | |
27 | foodnetwork| | |
28 | travelchannel| | |
29 | diynetwork| | |
30 | cookingchanneltv| | |
31 | motortrend | |
32 | ) | |
995f319b | 33 | )\.com/tv-shows/(?P<show_slug>[^/]+)/(?:video|full-episode)s/(?P<id>[^./?#]+)''' |
65ba8b23 | 34 | _TESTS = [{ |
53511165 | 35 | 'url': 'https://go.discovery.com/tv-shows/cash-cab/videos/riding-with-matthew-perry', |
53bfd6b2 | 36 | 'info_dict': { |
53511165 | 37 | 'id': '5a2f35ce6b66d17a5026e29e', |
65ba8b23 | 38 | 'ext': 'mp4', |
53511165 RA |
39 | 'title': 'Riding with Matthew Perry', |
40 | 'description': 'md5:a34333153e79bc4526019a5129e7f878', | |
41 | 'duration': 84, | |
9b05bd42 | 42 | }, |
65ba8b23 YCH |
43 | 'params': { |
44 | 'skip_download': True, # requires ffmpeg | |
45 | } | |
46 | }, { | |
cb0c2310 RA |
47 | 'url': 'https://www.investigationdiscovery.com/tv-shows/final-vision/full-episodes/final-vision', |
48 | 'only_matching': True, | |
3b446ab3 RA |
49 | }, { |
50 | 'url': 'https://go.discovery.com/tv-shows/alaskan-bush-people/videos/follow-your-own-road', | |
51 | 'only_matching': True, | |
995f319b RA |
52 | }, { |
53 | # using `show_slug` is important to get the correct video data | |
54 | 'url': 'https://www.sciencechannel.com/tv-shows/mythbusters-on-science/full-episodes/christmas-special', | |
55 | 'only_matching': True, | |
65ba8b23 | 56 | }] |
cb0c2310 RA |
57 | _GEO_COUNTRIES = ['US'] |
58 | _GEO_BYPASS = False | |
53511165 | 59 | _API_BASE_URL = 'https://api.discovery.com/v1/' |
53bfd6b2 | 60 | |
61 | def _real_extract(self, url): | |
5ad28e7f | 62 | site, show_slug, display_id = self._match_valid_url(url).groups() |
19dbaeec | 63 | |
3cc0d0b8 RA |
64 | access_token = None |
65 | cookies = self._get_cookies(url) | |
66 | ||
67 | # prefer Affiliate Auth Token over Anonymous Auth Token | |
68 | auth_storage_cookie = cookies.get('eosAf') or cookies.get('eosAn') | |
69 | if auth_storage_cookie and auth_storage_cookie.value: | |
70 | auth_storage = self._parse_json(compat_urllib_parse_unquote( | |
71 | compat_urllib_parse_unquote(auth_storage_cookie.value)), | |
53511165 | 72 | display_id, fatal=False) or {} |
3cc0d0b8 RA |
73 | access_token = auth_storage.get('a') or auth_storage.get('access_token') |
74 | ||
75 | if not access_token: | |
76 | access_token = self._download_json( | |
53511165 RA |
77 | 'https://%s.com/anonymous' % site, display_id, |
78 | 'Downloading token JSON metadata', query={ | |
3cc0d0b8 | 79 | 'authRel': 'authorization', |
53511165 | 80 | 'client_id': '3020a40c2356a645b4b4', |
efa944f4 | 81 | 'nonce': ''.join(random.choices(string.ascii_letters, k=32)), |
2f7aa680 | 82 | 'redirectUri': 'https://www.discovery.com/', |
3cc0d0b8 | 83 | })['access_token'] |
93f7a31b | 84 | |
53511165 RA |
85 | headers = self.geo_verification_headers() |
86 | headers['Authorization'] = 'Bearer ' + access_token | |
2122d715 | 87 | |
53511165 RA |
88 | try: |
89 | video = self._download_json( | |
90 | self._API_BASE_URL + 'content/videos', | |
91 | display_id, 'Downloading content JSON metadata', | |
92 | headers=headers, query={ | |
b3d39be2 RA |
93 | 'embed': 'show.name', |
94 | 'fields': 'authenticated,description.detailed,duration,episodeNumber,id,name,parental.rating,season.number,show,tags', | |
53511165 | 95 | 'slug': display_id, |
995f319b | 96 | 'show_slug': show_slug, |
53511165 RA |
97 | })[0] |
98 | video_id = video['id'] | |
cb0c2310 | 99 | stream = self._download_json( |
53511165 RA |
100 | self._API_BASE_URL + 'streaming/video/' + video_id, |
101 | display_id, 'Downloading streaming JSON metadata', headers=headers) | |
cb0c2310 | 102 | except ExtractorError as e: |
3d2623a8 | 103 | if isinstance(e.cause, HTTPError) and e.cause.status in (401, 403): |
cb0c2310 | 104 | e_description = self._parse_json( |
3d2623a8 | 105 | e.cause.response.read().decode(), display_id)['description'] |
cb0c2310 RA |
106 | if 'resource not available for country' in e_description: |
107 | self.raise_geo_restricted(countries=self._GEO_COUNTRIES) | |
108 | if 'Authorized Networks' in e_description: | |
109 | raise ExtractorError( | |
110 | 'This video is only available via cable service provider subscription that' | |
111 | ' is not currently supported. You may want to use --cookies.', expected=True) | |
112 | raise ExtractorError(e_description) | |
113 | raise | |
65ba8b23 | 114 | |
cb0c2310 | 115 | return self._extract_video_info(video, stream, display_id) |