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