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