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