]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/nexx.py
[Nexx] Extract more metadata (#2273)
[yt-dlp.git] / yt_dlp / extractor / nexx.py
CommitLineData
4e826cd9
S
1# coding: utf-8
2from __future__ import unicode_literals
3
d91dd0ce
S
4import hashlib
5import random
4e826cd9 6import re
d91dd0ce 7import time
4e826cd9
S
8
9from .common import InfoExtractor
10from ..compat import compat_str
11from ..utils import (
d91dd0ce 12 ExtractorError,
4e826cd9
S
13 int_or_none,
14 parse_duration,
0931ba94 15 srt_subtitles_timecode,
5f969a78 16 traverse_obj,
4e826cd9 17 try_get,
d91dd0ce 18 urlencode_postdata,
4e826cd9
S
19)
20
21
22class NexxIE(InfoExtractor):
694b6154
S
23 _VALID_URL = r'''(?x)
24 (?:
0931ba94 25 https?://api\.nexx(?:\.cloud|cdn\.com)/v3(?:\.\d)?/(?P<domain_id>\d+)/videos/byid/|
b5434b5c 26 nexx:(?:(?P<domain_id_s>\d+):)?|
0704306e 27 https?://arc\.nexx\.cloud/api/video/
694b6154
S
28 )
29 (?P<id>\d+)
30 '''
4e826cd9
S
31 _TESTS = [{
32 # movie
33 'url': 'https://api.nexx.cloud/v3/748/videos/byid/128907',
2e697530 34 'md5': '31899fd683de49ad46f4ee67e53e83fe',
4e826cd9
S
35 'info_dict': {
36 'id': '128907',
37 'ext': 'mp4',
38 'title': 'Stiftung Warentest',
39 'alt_title': 'Wie ein Test abläuft',
40 'description': 'md5:d1ddb1ef63de721132abd38639cc2fd2',
4e826cd9
S
41 'creator': 'SPIEGEL TV',
42 'thumbnail': r're:^https?://.*\.jpg$',
43 'duration': 2509,
44 'timestamp': 1384264416,
45 'upload_date': '20131112',
46 },
0931ba94 47 'skip': 'Spiegel nexx CDNs are now disabled'
4e826cd9 48 }, {
0931ba94
M
49 # episode with captions
50 'url': 'https://api.nexx.cloud/v3.1/741/videos/byid/1701834',
4e826cd9 51 'info_dict': {
0931ba94 52 'id': '1701834',
4e826cd9 53 'ext': 'mp4',
0931ba94
M
54 'title': 'Mein Leben mit \'nem TikTok E-Boy 😤',
55 'alt_title': 'Mein Leben mit \'nem TikTok E-Boy 😤',
56 'description': 'md5:f84f395a881fd143f952c892deab528d',
4e826cd9 57 'thumbnail': r're:^https?://.*\.jpg$',
0931ba94
M
58 'duration': 770,
59 'timestamp': 1595600027,
60 'upload_date': '20200724',
4e826cd9
S
61 'episode_number': 2,
62 'season_number': 2,
0931ba94
M
63 'episode': 'Episode 2',
64 'season': 'Season 2',
4e826cd9
S
65 },
66 'params': {
4e826cd9
S
67 'skip_download': True,
68 },
d91dd0ce 69 }, {
d91dd0ce 70 'url': 'nexx:741:1269984',
0931ba94 71 'md5': 'd5f14e14b592501e51addd5abef95a7f',
d91dd0ce
S
72 'info_dict': {
73 'id': '1269984',
74 'ext': 'mp4',
0931ba94
M
75 'title': '1 TAG ohne KLO... wortwörtlich! ?',
76 'alt_title': '1 TAG ohne KLO... wortwörtlich! ?',
77 'description': 'md5:2016393a31991a900946432ccdd09a6f',
d91dd0ce
S
78 'thumbnail': r're:^https?://.*\.jpg$',
79 'duration': 607,
80 'timestamp': 1518614955,
81 'upload_date': '20180214',
82 },
06ea7bdd
S
83 }, {
84 # free cdn from http://www.spiegel.de/video/eifel-zoo-aufregung-um-ausgebrochene-raubtiere-video-99018031.html
85 'url': 'nexx:747:1533779',
86 'md5': '6bf6883912b82b7069fb86c2297e9893',
87 'info_dict': {
88 'id': '1533779',
89 'ext': 'mp4',
90 'title': 'Aufregung um ausgebrochene Raubtiere',
91 'alt_title': 'Eifel-Zoo',
92 'description': 'md5:f21375c91c74ad741dcb164c427999d2',
93 'thumbnail': r're:^https?://.*\.jpg$',
94 'duration': 111,
95 'timestamp': 1527874460,
96 'upload_date': '20180601',
97 },
0931ba94 98 'skip': 'Spiegel nexx CDNs are now disabled'
4e826cd9
S
99 }, {
100 'url': 'https://api.nexxcdn.com/v3/748/videos/byid/128907',
101 'only_matching': True,
694b6154
S
102 }, {
103 'url': 'nexx:748:128907',
104 'only_matching': True,
9dc7ea32
S
105 }, {
106 'url': 'nexx:128907',
107 'only_matching': True,
0704306e
S
108 }, {
109 'url': 'https://arc.nexx.cloud/api/video/128907.json',
110 'only_matching': True,
4e826cd9
S
111 }]
112
694b6154
S
113 @staticmethod
114 def _extract_domain_id(webpage):
115 mobj = re.search(
eb22d1b5 116 r'<script\b[^>]+\bsrc=["\'](?:https?:)?//(?:require|arc)\.nexx(?:\.cloud|cdn\.com)/(?:sdk/)?(?P<id>\d+)',
694b6154
S
117 webpage)
118 return mobj.group('id') if mobj else None
119
4e826cd9
S
120 @staticmethod
121 def _extract_urls(webpage):
122 # Reference:
123 # 1. https://nx-s.akamaized.net/files/201510/44.pdf
124
125 entries = []
126
127 # JavaScript Integration
694b6154
S
128 domain_id = NexxIE._extract_domain_id(webpage)
129 if domain_id:
089b97cf 130 for video_id in re.findall(
eb22d1b5 131 r'(?is)onPLAYReady.+?_play\.(?:init|(?:control\.)?addPlayer)\s*\(.+?\s*,\s*["\']?(\d+)',
089b97cf
S
132 webpage):
133 entries.append(
134 'https://api.nexx.cloud/v3/%s/videos/byid/%s'
135 % (domain_id, video_id))
4e826cd9
S
136
137 # TODO: support more embed formats
138
139 return entries
140
3f59b015
S
141 @staticmethod
142 def _extract_url(webpage):
143 return NexxIE._extract_urls(webpage)[0]
144
d91dd0ce 145 def _handle_error(self, response):
0931ba94
M
146 if traverse_obj(response, ('metadata', 'notice'), expected_type=str):
147 self.report_warning('%s said: %s' % (self.IE_NAME, response['metadata']['notice']))
d91dd0ce
S
148 status = int_or_none(try_get(
149 response, lambda x: x['metadata']['status']) or 200)
150 if 200 <= status < 300:
151 return
152 raise ExtractorError(
153 '%s said: %s' % (self.IE_NAME, response['metadata']['errorhint']),
154 expected=True)
155
156 def _call_api(self, domain_id, path, video_id, data=None, headers={}):
157 headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
158 result = self._download_json(
159 'https://api.nexx.cloud/v3/%s/%s' % (domain_id, path), video_id,
160 'Downloading %s JSON' % path, data=urlencode_postdata(data),
161 headers=headers)
162 self._handle_error(result)
163 return result['result']
164
06ea7bdd
S
165 def _extract_free_formats(self, video, video_id):
166 stream_data = video['streamdata']
167 cdn = stream_data['cdnType']
168 assert cdn == 'free'
169
170 hash = video['general']['hash']
171
172 ps = compat_str(stream_data['originalDomain'])
173 if stream_data['applyFolderHierarchy'] == 1:
174 s = ('%04d' % int(video_id))[::-1]
175 ps += '/%s/%s' % (s[0:2], s[2:4])
176 ps += '/%s/%s_' % (video_id, hash)
177
9afd74d7
RA
178 t = 'http://%s' + ps
179 fd = stream_data['azureFileDistribution'].split(',')
180 cdn_provider = stream_data['cdnProvider']
181
182 def p0(p):
183 return '_%s' % p if stream_data['applyAzureStructure'] == 1 else ''
184
185 formats = []
186 if cdn_provider == 'ak':
187 t += ','
188 for i in fd:
189 p = i.split(':')
190 t += p[1] + p0(int(p[0])) + ','
191 t += '.mp4.csmil/master.%s'
192 elif cdn_provider == 'ce':
193 k = t.split('/')
194 h = k.pop()
195 http_base = t = '/'.join(k)
196 http_base = http_base % stream_data['cdnPathHTTP']
197 t += '/asset.ism/manifest.%s?dcp_ver=aos4&videostream='
198 for i in fd:
199 p = i.split(':')
200 tbr = int(p[0])
201 filename = '%s%s%s.mp4' % (h, p[1], p0(tbr))
202 f = {
203 'url': http_base + '/' + filename,
204 'format_id': '%s-http-%d' % (cdn, tbr),
205 'tbr': tbr,
206 }
207 width_height = p[1].split('x')
208 if len(width_height) == 2:
209 f.update({
210 'width': int_or_none(width_height[0]),
211 'height': int_or_none(width_height[1]),
212 })
213 formats.append(f)
214 a = filename + ':%s' % (tbr * 1000)
215 t += a + ','
216 t = t[:-1] + '&audiostream=' + a.split(':')[0]
217 else:
218 assert False
219
220 if cdn_provider == 'ce':
221 formats.extend(self._extract_mpd_formats(
222 t % (stream_data['cdnPathDASH'], 'mpd'), video_id,
223 mpd_id='%s-dash' % cdn, fatal=False))
06ea7bdd 224 formats.extend(self._extract_m3u8_formats(
9afd74d7 225 t % (stream_data['cdnPathHLS'], 'm3u8'), video_id, 'mp4',
06ea7bdd
S
226 entry_protocol='m3u8_native', m3u8_id='%s-hls' % cdn, fatal=False))
227
228 return formats
229
5f969a78
M
230 def _extract_3q_formats(self, video, video_id):
231 stream_data = video['streamdata']
232 cdn = stream_data['cdnType']
233 assert cdn == '3q'
234
235 q_acc, q_prefix, q_locator, q_hash = stream_data['qAccount'], stream_data['qPrefix'], stream_data['qLocator'], stream_data['qHash']
236 protection_key = traverse_obj(
237 video, ('protectiondata', 'key'), expected_type=str)
238
239 def get_cdn_shield_base(shield_type=''):
240 for secure in ('', 's'):
241 cdn_shield = stream_data.get('cdnShield%sHTTP%s' % (shield_type, secure.upper()))
242 if cdn_shield:
243 return 'http%s://%s' % (secure, cdn_shield)
244 return f'http://sdn-global-{"prog" if shield_type.lower() == "prog" else "streaming"}-cache.3qsdn.com/' + (f's/{protection_key}/' if protection_key else '')
245
246 stream_base = get_cdn_shield_base()
247
248 formats = []
249 formats.extend(self._extract_m3u8_formats(
250 f'{stream_base}{q_acc}/files/{q_prefix}/{q_locator}/{q_acc}-{stream_data.get("qHEVCHash") or q_hash}.ism/manifest.m3u8',
251 video_id, 'mp4', m3u8_id=f'{cdn}-hls', fatal=False))
252 formats.extend(self._extract_mpd_formats(
253 f'{stream_base}{q_acc}/files/{q_prefix}/{q_locator}/{q_acc}-{q_hash}.ism/manifest.mpd',
254 video_id, mpd_id=f'{cdn}-dash', fatal=False))
255
256 progressive_base = get_cdn_shield_base('Prog')
257 q_references = stream_data.get('qReferences') or ''
258 fds = q_references.split(',')
259 for fd in fds:
260 ss = fd.split(':')
261 if len(ss) != 3:
262 continue
263 tbr = int_or_none(ss[1], scale=1000)
264 formats.append({
265 'url': f'{progressive_base}{q_acc}/uploads/{q_acc}-{ss[2]}.webm',
266 'format_id': f'{cdn}-{ss[0]}{"-%s" % tbr if tbr else ""}',
267 'tbr': tbr,
268 })
269
270 azure_file_distribution = stream_data.get('azureFileDistribution') or ''
271 fds = azure_file_distribution.split(',')
272 for fd in fds:
273 ss = fd.split(':')
274 if len(ss) != 3:
275 continue
276 tbr = int_or_none(ss[0])
277 width, height = ss[1].split('x') if len(ss[1].split('x')) == 2 else (None, None)
278 f = {
279 'url': f'{progressive_base}{q_acc}/files/{q_prefix}/{q_locator}/{ss[2]}.mp4',
280 'format_id': f'{cdn}-http-{"-%s" % tbr if tbr else ""}',
281 'tbr': tbr,
282 'width': int_or_none(width),
283 'height': int_or_none(height),
284 }
285 formats.append(f)
286
287 return formats
288
06ea7bdd
S
289 def _extract_azure_formats(self, video, video_id):
290 stream_data = video['streamdata']
291 cdn = stream_data['cdnType']
292 assert cdn == 'azure'
293
294 azure_locator = stream_data['azureLocator']
295
296 def get_cdn_shield_base(shield_type='', static=False):
297 for secure in ('', 's'):
298 cdn_shield = stream_data.get('cdnShield%sHTTP%s' % (shield_type, secure.upper()))
299 if cdn_shield:
300 return 'http%s://%s' % (secure, cdn_shield)
301 else:
302 if 'fb' in stream_data['azureAccount']:
303 prefix = 'df' if static else 'f'
304 else:
305 prefix = 'd' if static else 'p'
306 account = int(stream_data['azureAccount'].replace('nexxplayplus', '').replace('nexxplayfb', ''))
307 return 'http://nx-%s%02d.akamaized.net/' % (prefix, account)
308
309 language = video['general'].get('language_raw') or ''
310
311 azure_stream_base = get_cdn_shield_base()
312 is_ml = ',' in language
313 azure_manifest_url = '%s%s/%s_src%s.ism/Manifest' % (
314 azure_stream_base, azure_locator, video_id, ('_manifest' if is_ml else '')) + '%s'
315
316 protection_token = try_get(
317 video, lambda x: x['protectiondata']['token'], compat_str)
318 if protection_token:
319 azure_manifest_url += '?hdnts=%s' % protection_token
320
321 formats = self._extract_m3u8_formats(
322 azure_manifest_url % '(format=m3u8-aapl)',
323 video_id, 'mp4', 'm3u8_native',
324 m3u8_id='%s-hls' % cdn, fatal=False)
325 formats.extend(self._extract_mpd_formats(
326 azure_manifest_url % '(format=mpd-time-csf)',
327 video_id, mpd_id='%s-dash' % cdn, fatal=False))
328 formats.extend(self._extract_ism_formats(
329 azure_manifest_url % '', video_id, ism_id='%s-mss' % cdn, fatal=False))
330
331 azure_progressive_base = get_cdn_shield_base('Prog', True)
332 azure_file_distribution = stream_data.get('azureFileDistribution')
333 if azure_file_distribution:
334 fds = azure_file_distribution.split(',')
335 if fds:
336 for fd in fds:
337 ss = fd.split(':')
338 if len(ss) == 2:
339 tbr = int_or_none(ss[0])
340 if tbr:
341 f = {
342 'url': '%s%s/%s_src_%s_%d.mp4' % (
343 azure_progressive_base, azure_locator, video_id, ss[1], tbr),
344 'format_id': '%s-http-%d' % (cdn, tbr),
345 'tbr': tbr,
346 }
347 width_height = ss[1].split('x')
348 if len(width_height) == 2:
349 f.update({
350 'width': int_or_none(width_height[0]),
351 'height': int_or_none(width_height[1]),
352 })
353 formats.append(f)
354
355 return formats
356
4e826cd9 357 def _real_extract(self, url):
5ad28e7f 358 mobj = self._match_valid_url(url)
d91dd0ce
S
359 domain_id = mobj.group('domain_id') or mobj.group('domain_id_s')
360 video_id = mobj.group('id')
361
362 video = None
4e826cd9 363
7e05df71
RA
364 def find_video(result):
365 if isinstance(result, dict):
366 return result
367 elif isinstance(result, list):
368 vid = int(video_id)
369 for v in result:
370 if try_get(v, lambda x: x['general']['ID'], int) == vid:
371 return v
372 return None
373
d91dd0ce 374 response = self._download_json(
e231afb1 375 'https://arc.nexx.cloud/api/video/%s.json' % video_id,
d91dd0ce
S
376 video_id, fatal=False)
377 if response and isinstance(response, dict):
378 result = response.get('result')
7e05df71
RA
379 if result:
380 video = find_video(result)
d91dd0ce
S
381
382 # not all videos work via arc, e.g. nexx:741:1269984
383 if not video:
384 # Reverse engineered from JS code (see getDeviceID function)
385 device_id = '%d:%d:%d%d' % (
386 random.randint(1, 4), int(time.time()),
387 random.randint(1e4, 99999), random.randint(1, 9))
388
389 result = self._call_api(domain_id, 'session/init', video_id, data={
390 'nxp_devh': device_id,
391 'nxp_userh': '',
392 'precid': '0',
393 'playlicense': '0',
394 'screenx': '1920',
395 'screeny': '1080',
396 'playerversion': '6.0.00',
397 'gateway': 'html5',
398 'adGateway': '',
399 'explicitlanguage': 'en-US',
400 'addTextTemplates': '1',
401 'addDomainData': '1',
402 'addAdModel': '1',
403 }, headers={
404 'X-Request-Enable-Auth-Fallback': '1',
405 })
406
407 cid = result['general']['cid']
408
409 # As described in [1] X-Request-Token generation algorithm is
410 # as follows:
411 # md5( operation + domain_id + domain_secret )
412 # where domain_secret is a static value that will be given by nexx.tv
413 # as per [1]. Here is how this "secret" is generated (reversed
0931ba94
M
414 # from _play._factory.data.getDomainData function, search for
415 # domaintoken or enableAPIAccess). So it's actually not static
416 # and not that much of a secret.
d91dd0ce 417 # 1. https://nexxtvstorage.blob.core.windows.net/files/201610/27.pdf
0931ba94 418 secret = result['device']['domaintoken'][int(device_id[0]):]
d91dd0ce
S
419 secret = secret[0:len(secret) - int(device_id[-1])]
420
421 op = 'byid'
422
423 # Reversed from JS code for _play.api.call function (search for
424 # X-Request-Token)
425 request_token = hashlib.md5(
426 ''.join((op, domain_id, secret)).encode('utf-8')).hexdigest()
427
7e05df71 428 result = self._call_api(
d91dd0ce 429 domain_id, 'videos/%s/%s' % (op, video_id), video_id, data={
0931ba94 430 'additionalfields': 'language,channel,format,licenseby,slug,fileversion,episode,season',
d91dd0ce
S
431 'addInteractionOptions': '1',
432 'addStatusDetails': '1',
433 'addStreamDetails': '1',
0931ba94
M
434 'addFeatures': '1',
435 # Caption format selection doesn't seem to be enforced?
436 'addCaptions': 'vtt',
d91dd0ce 437 'addScenes': '1',
0931ba94 438 'addChapters': '1',
d91dd0ce 439 'addHotSpots': '1',
0931ba94 440 'addConnectedMedia': 'persons',
d91dd0ce 441 'addBumpers': '1',
d91dd0ce
S
442 }, headers={
443 'X-Request-CID': cid,
444 'X-Request-Token': request_token,
445 })
7e05df71 446 video = find_video(result)
4e826cd9
S
447
448 general = video['general']
449 title = general['title']
450
06ea7bdd 451 cdn = video['streamdata']['cdnType']
4e826cd9 452
06ea7bdd
S
453 if cdn == 'azure':
454 formats = self._extract_azure_formats(video, video_id)
455 elif cdn == 'free':
456 formats = self._extract_free_formats(video, video_id)
5f969a78
M
457 elif cdn == '3q':
458 formats = self._extract_3q_formats(video, video_id)
06ea7bdd 459 else:
48e93106 460 self.raise_no_formats(f'{cdn} formats are currently not supported', video_id)
c0f647a1 461
4e826cd9
S
462 self._sort_formats(formats)
463
0931ba94
M
464 subtitles = {}
465 for sub in video.get('captiondata') or []:
466 if sub.get('data'):
467 subtitles.setdefault(sub.get('language', 'en'), []).append({
468 'ext': 'srt',
469 'data': '\n\n'.join(
470 f'{i + 1}\n{srt_subtitles_timecode(line["fromms"] / 1000)} --> {srt_subtitles_timecode(line["toms"] / 1000)}\n{line["caption"]}'
471 for i, line in enumerate(sub['data'])),
472 'name': sub.get('language_long') or sub.get('title')
473 })
474 elif sub.get('url'):
475 subtitles.setdefault(sub.get('language', 'en'), []).append({
476 'url': sub['url'],
477 'ext': sub.get('format'),
478 'name': sub.get('language_long') or sub.get('title')
479 })
480
4e826cd9
S
481 return {
482 'id': video_id,
483 'title': title,
484 'alt_title': general.get('subtitle'),
485 'description': general.get('description'),
486 'release_year': int_or_none(general.get('year')),
0931ba94 487 'creator': general.get('studio') or general.get('studio_adref') or None,
4e826cd9
S
488 'thumbnail': try_get(
489 video, lambda x: x['imagedata']['thumb'], compat_str),
490 'duration': parse_duration(general.get('runtime')),
491 'timestamp': int_or_none(general.get('uploaded')),
0931ba94
M
492 'episode_number': traverse_obj(
493 video, (('episodedata', 'general'), 'episode'), expected_type=int, get_all=False),
494 'season_number': traverse_obj(
495 video, (('episodedata', 'general'), 'season'), expected_type=int, get_all=False),
496 'cast': traverse_obj(video, ('connectedmedia', ..., 'title'), expected_type=str),
4e826cd9 497 'formats': formats,
0931ba94 498 'subtitles': subtitles,
4e826cd9 499 }
3f59b015
S
500
501
502class NexxEmbedIE(InfoExtractor):
eb22d1b5
RA
503 _VALID_URL = r'https?://embed\.nexx(?:\.cloud|cdn\.com)/\d+/(?:video/)?(?P<id>[^/?#&]+)'
504 _TESTS = [{
3f59b015
S
505 'url': 'http://embed.nexx.cloud/748/KC1614647Z27Y7T?autoplay=1',
506 'md5': '16746bfc28c42049492385c989b26c4a',
507 'info_dict': {
508 'id': '161464',
509 'ext': 'mp4',
510 'title': 'Nervenkitzel Achterbahn',
511 'alt_title': 'Karussellbauer in Deutschland',
512 'description': 'md5:ffe7b1cc59a01f585e0569949aef73cc',
3f59b015
S
513 'creator': 'SPIEGEL TV',
514 'thumbnail': r're:^https?://.*\.jpg$',
515 'duration': 2761,
516 'timestamp': 1394021479,
517 'upload_date': '20140305',
518 },
519 'params': {
3f59b015
S
520 'skip_download': True,
521 },
eb22d1b5
RA
522 }, {
523 'url': 'https://embed.nexx.cloud/11888/video/DSRTO7UVOX06S7',
524 'only_matching': True,
525 }]
3f59b015
S
526
527 @staticmethod
528 def _extract_urls(webpage):
529 # Reference:
530 # 1. https://nx-s.akamaized.net/files/201510/44.pdf
531
532 # iFrame Embed Integration
533 return [mobj.group('url') for mobj in re.finditer(
13eb526f 534 r'<iframe[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//embed\.nexx(?:\.cloud|cdn\.com)/\d+/(?:(?!\1).)+)\1',
3f59b015
S
535 webpage)]
536
537 def _real_extract(self, url):
538 embed_id = self._match_id(url)
539
540 webpage = self._download_webpage(url, embed_id)
541
542 return self.url_result(NexxIE._extract_url(webpage), ie=NexxIE.ie_key())