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