[youtube] Improve metadata extraction for age gate content (closes #21943)
[youtube-dl] / youtube_dl / extractor / discovery.py
1 from __future__ import unicode_literals
2
3 import random
4 import re
5 import string
6
7 from .discoverygo import DiscoveryGoBaseIE
8 from ..compat import (
9     compat_str,
10     compat_urllib_parse_unquote,
11 )
12 from ..utils import (
13     ExtractorError,
14     try_get,
15 )
16 from ..compat import compat_HTTPError
17
18
19 class DiscoveryIE(DiscoveryGoBaseIE):
20     _VALID_URL = r'''(?x)https?://
21         (?P<site>
22             (?:(?:www|go)\.)?discovery|
23             (?:www\.)?
24                 (?:
25                     investigationdiscovery|
26                     discoverylife|
27                     animalplanet|
28                     ahctv|
29                     destinationamerica|
30                     sciencechannel|
31                     tlc|
32                     velocity
33                 )|
34             watch\.
35                 (?:
36                     hgtv|
37                     foodnetwork|
38                     travelchannel|
39                     diynetwork|
40                     cookingchanneltv|
41                     motortrend
42                 )
43         )\.com(?P<path>/tv-shows/[^/]+/(?:video|full-episode)s/(?P<id>[^./?#]+))'''
44     _TESTS = [{
45         'url': 'https://www.discovery.com/tv-shows/cash-cab/videos/dave-foley',
46         'info_dict': {
47             'id': '5a2d9b4d6b66d17a5026e1fd',
48             'ext': 'mp4',
49             'title': 'Dave Foley',
50             'description': 'md5:4b39bcafccf9167ca42810eb5f28b01f',
51             'duration': 608,
52         },
53         'params': {
54             'skip_download': True,  # requires ffmpeg
55         }
56     }, {
57         'url': 'https://www.investigationdiscovery.com/tv-shows/final-vision/full-episodes/final-vision',
58         'only_matching': True,
59     }, {
60         'url': 'https://go.discovery.com/tv-shows/alaskan-bush-people/videos/follow-your-own-road',
61         'only_matching': True,
62     }]
63     _GEO_COUNTRIES = ['US']
64     _GEO_BYPASS = False
65
66     def _real_extract(self, url):
67         site, path, display_id = re.match(self._VALID_URL, url).groups()
68         webpage = self._download_webpage(url, display_id)
69
70         react_data = self._parse_json(self._search_regex(
71             r'window\.__reactTransmitPacket\s*=\s*({.+?});',
72             webpage, 'react data'), display_id)
73         content_blocks = react_data['layout'][path]['contentBlocks']
74         video = next(cb for cb in content_blocks if cb.get('type') == 'video')['content']['items'][0]
75         video_id = video['id']
76
77         access_token = None
78         cookies = self._get_cookies(url)
79
80         # prefer Affiliate Auth Token over Anonymous Auth Token
81         auth_storage_cookie = cookies.get('eosAf') or cookies.get('eosAn')
82         if auth_storage_cookie and auth_storage_cookie.value:
83             auth_storage = self._parse_json(compat_urllib_parse_unquote(
84                 compat_urllib_parse_unquote(auth_storage_cookie.value)),
85                 video_id, fatal=False) or {}
86             access_token = auth_storage.get('a') or auth_storage.get('access_token')
87
88         if not access_token:
89             access_token = self._download_json(
90                 'https://%s.com/anonymous' % site, display_id, query={
91                     'authRel': 'authorization',
92                     'client_id': try_get(
93                         react_data, lambda x: x['application']['apiClientId'],
94                         compat_str) or '3020a40c2356a645b4b4',
95                     'nonce': ''.join([random.choice(string.ascii_letters) for _ in range(32)]),
96                     'redirectUri': 'https://fusion.ddmcdn.com/app/mercury-sdk/180/redirectHandler.html?https://www.%s.com' % site,
97                 })['access_token']
98
99         try:
100             headers = self.geo_verification_headers()
101             headers['Authorization'] = 'Bearer ' + access_token
102
103             stream = self._download_json(
104                 'https://api.discovery.com/v1/streaming/video/' + video_id,
105                 display_id, 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)
117             raise
118
119         return self._extract_video_info(video, stream, display_id)