]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/discovery.py
[youtube] Prefer UTC upload date for videos (#2223)
[yt-dlp.git] / yt_dlp / extractor / discovery.py
1 from __future__ import unicode_literals
2
3 import random
4 import string
5
6 from .discoverygo import DiscoveryGoBaseIE
7 from ..compat import compat_urllib_parse_unquote
8 from ..utils import ExtractorError
9 from ..compat import compat_HTTPError
10
11
12 class DiscoveryIE(DiscoveryGoBaseIE):
13 _VALID_URL = r'''(?x)https?://
14 (?P<site>
15 go\.discovery|
16 www\.
17 (?:
18 investigationdiscovery|
19 discoverylife|
20 animalplanet|
21 ahctv|
22 destinationamerica|
23 sciencechannel|
24 tlc
25 )|
26 watch\.
27 (?:
28 hgtv|
29 foodnetwork|
30 travelchannel|
31 diynetwork|
32 cookingchanneltv|
33 motortrend
34 )
35 )\.com/tv-shows/(?P<show_slug>[^/]+)/(?:video|full-episode)s/(?P<id>[^./?#]+)'''
36 _TESTS = [{
37 'url': 'https://go.discovery.com/tv-shows/cash-cab/videos/riding-with-matthew-perry',
38 'info_dict': {
39 'id': '5a2f35ce6b66d17a5026e29e',
40 'ext': 'mp4',
41 'title': 'Riding with Matthew Perry',
42 'description': 'md5:a34333153e79bc4526019a5129e7f878',
43 'duration': 84,
44 },
45 'params': {
46 'skip_download': True, # requires ffmpeg
47 }
48 }, {
49 'url': 'https://www.investigationdiscovery.com/tv-shows/final-vision/full-episodes/final-vision',
50 'only_matching': True,
51 }, {
52 'url': 'https://go.discovery.com/tv-shows/alaskan-bush-people/videos/follow-your-own-road',
53 'only_matching': True,
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,
58 }]
59 _GEO_COUNTRIES = ['US']
60 _GEO_BYPASS = False
61 _API_BASE_URL = 'https://api.discovery.com/v1/'
62
63 def _real_extract(self, url):
64 site, show_slug, display_id = self._match_valid_url(url).groups()
65
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)),
74 display_id, fatal=False) or {}
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(
79 'https://%s.com/anonymous' % site, display_id,
80 'Downloading token JSON metadata', query={
81 'authRel': 'authorization',
82 'client_id': '3020a40c2356a645b4b4',
83 'nonce': ''.join([random.choice(string.ascii_letters) for _ in range(32)]),
84 'redirectUri': 'https://www.discovery.com/',
85 })['access_token']
86
87 headers = self.geo_verification_headers()
88 headers['Authorization'] = 'Bearer ' + access_token
89
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={
95 'embed': 'show.name',
96 'fields': 'authenticated,description.detailed,duration,episodeNumber,id,name,parental.rating,season.number,show,tags',
97 'slug': display_id,
98 'show_slug': show_slug,
99 })[0]
100 video_id = video['id']
101 stream = self._download_json(
102 self._API_BASE_URL + 'streaming/video/' + video_id,
103 display_id, 'Downloading streaming JSON metadata', headers=headers)
104 except ExtractorError as e:
105 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (401, 403):
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
116
117 return self._extract_video_info(video, stream, display_id)