]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/dplay.py
[test/download] Fallback test to `bv`
[yt-dlp.git] / yt_dlp / extractor / dplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..compat import compat_HTTPError
8 from ..utils import (
9 determine_ext,
10 ExtractorError,
11 float_or_none,
12 int_or_none,
13 strip_or_none,
14 unified_timestamp,
15 )
16
17
18 class DPlayIE(InfoExtractor):
19 _PATH_REGEX = r'/(?P<id>[^/]+/[^/?#]+)'
20 _VALID_URL = r'''(?x)https?://
21 (?P<domain>
22 (?:www\.)?(?P<host>d
23 (?:
24 play\.(?P<country>dk|fi|jp|se|no)|
25 iscoveryplus\.(?P<plus_country>dk|es|fi|it|se|no)
26 )
27 )|
28 (?P<subdomain_country>es|it)\.dplay\.com
29 )/[^/]+''' + _PATH_REGEX
30
31 _TESTS = [{
32 # non geo restricted, via secure api, unsigned download hls URL
33 'url': 'https://www.dplay.se/videos/nugammalt-77-handelser-som-format-sverige/nugammalt-77-handelser-som-format-sverige-101',
34 'info_dict': {
35 'id': '13628',
36 'display_id': 'nugammalt-77-handelser-som-format-sverige/nugammalt-77-handelser-som-format-sverige-101',
37 'ext': 'mp4',
38 'title': 'Svensken lär sig njuta av livet',
39 'description': 'md5:d3819c9bccffd0fe458ca42451dd50d8',
40 'duration': 2649.856,
41 'timestamp': 1365453720,
42 'upload_date': '20130408',
43 'creator': 'Kanal 5',
44 'series': 'Nugammalt - 77 händelser som format Sverige',
45 'season_number': 1,
46 'episode_number': 1,
47 },
48 'params': {
49 'skip_download': True,
50 },
51 }, {
52 # geo restricted, via secure api, unsigned download hls URL
53 'url': 'http://www.dplay.dk/videoer/ted-bundy-mind-of-a-monster/ted-bundy-mind-of-a-monster',
54 'info_dict': {
55 'id': '104465',
56 'display_id': 'ted-bundy-mind-of-a-monster/ted-bundy-mind-of-a-monster',
57 'ext': 'mp4',
58 'title': 'Ted Bundy: Mind Of A Monster',
59 'description': 'md5:8b780f6f18de4dae631668b8a9637995',
60 'duration': 5290.027,
61 'timestamp': 1570694400,
62 'upload_date': '20191010',
63 'creator': 'ID - Investigation Discovery',
64 'series': 'Ted Bundy: Mind Of A Monster',
65 'season_number': 1,
66 'episode_number': 1,
67 },
68 'params': {
69 'skip_download': True,
70 },
71 }, {
72 # disco-api
73 'url': 'https://www.dplay.no/videoer/i-kongens-klr/sesong-1-episode-7',
74 'info_dict': {
75 'id': '40206',
76 'display_id': 'i-kongens-klr/sesong-1-episode-7',
77 'ext': 'mp4',
78 'title': 'Episode 7',
79 'description': 'md5:e3e1411b2b9aebeea36a6ec5d50c60cf',
80 'duration': 2611.16,
81 'timestamp': 1516726800,
82 'upload_date': '20180123',
83 'series': 'I kongens klær',
84 'season_number': 1,
85 'episode_number': 7,
86 },
87 'params': {
88 'skip_download': True,
89 },
90 'skip': 'Available for Premium users',
91 }, {
92 'url': 'http://it.dplay.com/nove/biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij/',
93 'md5': '2b808ffb00fc47b884a172ca5d13053c',
94 'info_dict': {
95 'id': '6918',
96 'display_id': 'biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij',
97 'ext': 'mp4',
98 'title': 'Luigi Di Maio: la psicosi di Stanislawskij',
99 'description': 'md5:3c7a4303aef85868f867a26f5cc14813',
100 'thumbnail': r're:^https?://.*\.jpe?g',
101 'upload_date': '20160524',
102 'timestamp': 1464076800,
103 'series': 'Biografie imbarazzanti',
104 'season_number': 1,
105 'episode': 'Episode 1',
106 'episode_number': 1,
107 },
108 }, {
109 'url': 'https://es.dplay.com/dmax/la-fiebre-del-oro/temporada-8-episodio-1/',
110 'info_dict': {
111 'id': '21652',
112 'display_id': 'la-fiebre-del-oro/temporada-8-episodio-1',
113 'ext': 'mp4',
114 'title': 'Episodio 1',
115 'description': 'md5:b9dcff2071086e003737485210675f69',
116 'thumbnail': r're:^https?://.*\.png',
117 'upload_date': '20180709',
118 'timestamp': 1531173540,
119 'series': 'La fiebre del oro',
120 'season_number': 8,
121 'episode': 'Episode 1',
122 'episode_number': 1,
123 },
124 'params': {
125 'skip_download': True,
126 },
127 }, {
128 'url': 'https://www.dplay.fi/videot/shifting-gears-with-aaron-kaufman/episode-16',
129 'only_matching': True,
130 }, {
131 'url': 'https://www.dplay.jp/video/gold-rush/24086',
132 'only_matching': True,
133 }, {
134 'url': 'https://www.discoveryplus.se/videos/nugammalt-77-handelser-som-format-sverige/nugammalt-77-handelser-som-format-sverige-101',
135 'only_matching': True,
136 }, {
137 'url': 'https://www.discoveryplus.dk/videoer/ted-bundy-mind-of-a-monster/ted-bundy-mind-of-a-monster',
138 'only_matching': True,
139 }, {
140 'url': 'https://www.discoveryplus.no/videoer/i-kongens-klr/sesong-1-episode-7',
141 'only_matching': True,
142 }, {
143 'url': 'https://www.discoveryplus.it/videos/biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij',
144 'only_matching': True,
145 }, {
146 'url': 'https://www.discoveryplus.es/videos/la-fiebre-del-oro/temporada-8-episodio-1',
147 'only_matching': True,
148 }, {
149 'url': 'https://www.discoveryplus.fi/videot/shifting-gears-with-aaron-kaufman/episode-16',
150 'only_matching': True,
151 }]
152
153 def _process_errors(self, e, geo_countries):
154 info = self._parse_json(e.cause.read().decode('utf-8'), None)
155 error = info['errors'][0]
156 error_code = error.get('code')
157 if error_code == 'access.denied.geoblocked':
158 self.raise_geo_restricted(countries=geo_countries)
159 elif error_code in ('access.denied.missingpackage', 'invalid.token'):
160 raise ExtractorError(
161 'This video is only available for registered users. You may want to use --cookies.', expected=True)
162 raise ExtractorError(info['errors'][0]['detail'], expected=True)
163
164 def _update_disco_api_headers(self, headers, disco_base, display_id, realm):
165 headers['Authorization'] = 'Bearer ' + self._download_json(
166 disco_base + 'token', display_id, 'Downloading token',
167 query={
168 'realm': realm,
169 })['data']['attributes']['token']
170
171 def _download_video_playback_info(self, disco_base, video_id, headers):
172 streaming = self._download_json(
173 disco_base + 'playback/videoPlaybackInfo/' + video_id,
174 video_id, headers=headers)['data']['attributes']['streaming']
175 streaming_list = []
176 for format_id, format_dict in streaming.items():
177 streaming_list.append({
178 'type': format_id,
179 'url': format_dict.get('url'),
180 })
181 return streaming_list
182
183 def _get_disco_api_info(self, url, display_id, disco_host, realm, country):
184 geo_countries = [country.upper()]
185 self._initialize_geo_bypass({
186 'countries': geo_countries,
187 })
188 disco_base = 'https://%s/' % disco_host
189 headers = {
190 'Referer': url,
191 }
192 self._update_disco_api_headers(headers, disco_base, display_id, realm)
193 try:
194 video = self._download_json(
195 disco_base + 'content/videos/' + display_id, display_id,
196 headers=headers, query={
197 'fields[channel]': 'name',
198 'fields[image]': 'height,src,width',
199 'fields[show]': 'name',
200 'fields[tag]': 'name',
201 'fields[video]': 'description,episodeNumber,name,publishStart,seasonNumber,videoDuration',
202 'include': 'images,primaryChannel,show,tags'
203 })
204 except ExtractorError as e:
205 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
206 self._process_errors(e, geo_countries)
207 raise
208 video_id = video['data']['id']
209 info = video['data']['attributes']
210 title = info['name'].strip()
211 formats = []
212 try:
213 streaming = self._download_video_playback_info(
214 disco_base, video_id, headers)
215 except ExtractorError as e:
216 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
217 self._process_errors(e, geo_countries)
218 raise
219 for format_dict in streaming:
220 if not isinstance(format_dict, dict):
221 continue
222 format_url = format_dict.get('url')
223 if not format_url:
224 continue
225 format_id = format_dict.get('type')
226 ext = determine_ext(format_url)
227 if format_id == 'dash' or ext == 'mpd':
228 formats.extend(self._extract_mpd_formats(
229 format_url, display_id, mpd_id='dash', fatal=False))
230 elif format_id == 'hls' or ext == 'm3u8':
231 formats.extend(self._extract_m3u8_formats(
232 format_url, display_id, 'mp4',
233 entry_protocol='m3u8_native', m3u8_id='hls',
234 fatal=False))
235 else:
236 formats.append({
237 'url': format_url,
238 'format_id': format_id,
239 })
240 self._sort_formats(formats)
241
242 creator = series = None
243 tags = []
244 thumbnails = []
245 included = video.get('included') or []
246 if isinstance(included, list):
247 for e in included:
248 attributes = e.get('attributes')
249 if not attributes:
250 continue
251 e_type = e.get('type')
252 if e_type == 'channel':
253 creator = attributes.get('name')
254 elif e_type == 'image':
255 src = attributes.get('src')
256 if src:
257 thumbnails.append({
258 'url': src,
259 'width': int_or_none(attributes.get('width')),
260 'height': int_or_none(attributes.get('height')),
261 })
262 if e_type == 'show':
263 series = attributes.get('name')
264 elif e_type == 'tag':
265 name = attributes.get('name')
266 if name:
267 tags.append(name)
268
269 return {
270 'id': video_id,
271 'display_id': display_id,
272 'title': title,
273 'description': strip_or_none(info.get('description')),
274 'duration': float_or_none(info.get('videoDuration'), 1000),
275 'timestamp': unified_timestamp(info.get('publishStart')),
276 'series': series,
277 'season_number': int_or_none(info.get('seasonNumber')),
278 'episode_number': int_or_none(info.get('episodeNumber')),
279 'creator': creator,
280 'tags': tags,
281 'thumbnails': thumbnails,
282 'formats': formats,
283 }
284
285 def _real_extract(self, url):
286 mobj = self._match_valid_url(url)
287 display_id = mobj.group('id')
288 domain = mobj.group('domain').lstrip('www.')
289 country = mobj.group('country') or mobj.group('subdomain_country') or mobj.group('plus_country')
290 host = 'disco-api.' + domain if domain[0] == 'd' else 'eu2-prod.disco-api.com'
291 return self._get_disco_api_info(
292 url, display_id, host, 'dplay' + country, country)
293
294
295 class HGTVDeIE(DPlayIE):
296 _VALID_URL = r'https?://de\.hgtv\.com/sendungen' + DPlayIE._PATH_REGEX
297 _TESTS = [{
298 'url': 'https://de.hgtv.com/sendungen/tiny-house-klein-aber-oho/wer-braucht-schon-eine-toilette/',
299 'info_dict': {
300 'id': '151205',
301 'display_id': 'tiny-house-klein-aber-oho/wer-braucht-schon-eine-toilette',
302 'ext': 'mp4',
303 'title': 'Wer braucht schon eine Toilette',
304 'description': 'md5:05b40a27e7aed2c9172de34d459134e2',
305 'duration': 1177.024,
306 'timestamp': 1595705400,
307 'upload_date': '20200725',
308 'creator': 'HGTV',
309 'series': 'Tiny House - klein, aber oho',
310 'season_number': 3,
311 'episode_number': 3,
312 },
313 }]
314
315 def _real_extract(self, url):
316 display_id = self._match_id(url)
317 return self._get_disco_api_info(
318 url, display_id, 'eu1-prod.disco-api.com', 'hgtv', 'de')
319
320
321 class DiscoveryPlusIE(DPlayIE):
322 _VALID_URL = r'https?://(?:www\.)?discoveryplus\.com/(?:\w{2}/)?video' + DPlayIE._PATH_REGEX
323 _TESTS = [{
324 'url': 'https://www.discoveryplus.com/video/property-brothers-forever-home/food-and-family',
325 'info_dict': {
326 'id': '1140794',
327 'display_id': 'property-brothers-forever-home/food-and-family',
328 'ext': 'mp4',
329 'title': 'Food and Family',
330 'description': 'The brothers help a Richmond family expand their single-level home.',
331 'duration': 2583.113,
332 'timestamp': 1609304400,
333 'upload_date': '20201230',
334 'creator': 'HGTV',
335 'series': 'Property Brothers: Forever Home',
336 'season_number': 1,
337 'episode_number': 1,
338 },
339 'skip': 'Available for Premium users',
340 }, {
341 'url': 'https://discoveryplus.com/ca/video/bering-sea-gold-discovery-ca/goldslingers',
342 'only_matching': True,
343 }]
344
345 _PRODUCT = 'dplus_us'
346 _API_URL = 'us1-prod-direct.discoveryplus.com'
347
348 def _update_disco_api_headers(self, headers, disco_base, display_id, realm):
349 headers['x-disco-client'] = f'WEB:UNKNOWN:{self._PRODUCT}:25.2.6'
350
351 def _download_video_playback_info(self, disco_base, video_id, headers):
352 return self._download_json(
353 disco_base + 'playback/v3/videoPlaybackInfo',
354 video_id, headers=headers, data=json.dumps({
355 'deviceInfo': {
356 'adBlocker': False,
357 },
358 'videoId': video_id,
359 'wisteriaProperties': {
360 'platform': 'desktop',
361 'product': self._PRODUCT,
362 },
363 }).encode('utf-8'))['data']['attributes']['streaming']
364
365 def _real_extract(self, url):
366 display_id = self._match_id(url)
367 return self._get_disco_api_info(
368 url, display_id, self._API_URL, 'go', 'us')
369
370
371 class ScienceChannelIE(DiscoveryPlusIE):
372 _VALID_URL = r'https?://(?:www\.)?sciencechannel\.com/video' + DPlayIE._PATH_REGEX
373 _TESTS = [{
374 'url': 'https://www.sciencechannel.com/video/strangest-things-science-atve-us/nazi-mystery-machine',
375 'info_dict': {
376 'id': '2842849',
377 'display_id': 'strangest-things-science-atve-us/nazi-mystery-machine',
378 'ext': 'mp4',
379 'title': 'Nazi Mystery Machine',
380 'description': 'Experts investigate the secrets of a revolutionary encryption machine.',
381 'season_number': 1,
382 'episode_number': 1,
383 },
384 'skip': 'Available for Premium users',
385 }]
386
387 _PRODUCT = 'sci'
388 _API_URL = 'us1-prod-direct.sciencechannel.com'
389
390
391 class DIYNetworkIE(DiscoveryPlusIE):
392 _VALID_URL = r'https?://(?:watch\.)?diynetwork\.com/video' + DPlayIE._PATH_REGEX
393 _TESTS = [{
394 'url': 'https://watch.diynetwork.com/video/pool-kings-diy-network/bringing-beach-life-to-texas',
395 'info_dict': {
396 'id': '2309730',
397 'display_id': 'pool-kings-diy-network/bringing-beach-life-to-texas',
398 'ext': 'mp4',
399 'title': 'Bringing Beach Life to Texas',
400 'description': 'The Pool Kings give a family a day at the beach in their own backyard.',
401 'season_number': 10,
402 'episode_number': 2,
403 },
404 'skip': 'Available for Premium users',
405 }]
406
407 _PRODUCT = 'diy'
408 _API_URL = 'us1-prod-direct.watch.diynetwork.com'
409
410
411 class AnimalPlanetIE(DiscoveryPlusIE):
412 _VALID_URL = r'https?://(?:www\.)?animalplanet\.com/video' + DPlayIE._PATH_REGEX
413 _TESTS = [{
414 'url': 'https://www.animalplanet.com/video/north-woods-law-animal-planet/squirrel-showdown',
415 'info_dict': {
416 'id': '3338923',
417 'display_id': 'north-woods-law-animal-planet/squirrel-showdown',
418 'ext': 'mp4',
419 'title': 'Squirrel Showdown',
420 'description': 'A woman is suspected of being in possession of flying squirrel kits.',
421 'season_number': 16,
422 'episode_number': 11,
423 },
424 'skip': 'Available for Premium users',
425 }]
426
427 _PRODUCT = 'apl'
428 _API_URL = 'us1-prod-direct.animalplanet.com'