]>
Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/nexx.py
c7029d29ebc0f4f9b28ee4474a1cfa04600d3650
   2 from __future__ 
import unicode_literals
 
   9 from .common 
import InfoExtractor
 
  10 from ..compat 
import compat_str
 
  20 class NexxIE(InfoExtractor
): 
  23                             https?://api\.nexx(?:\.cloud|cdn\.com)/v3/(?P<domain_id>\d+)/videos/byid/| 
  24                             nexx:(?:(?P<domain_id_s>\d+):)?| 
  25                             https?://arc\.nexx\.cloud/api/video/ 
  31         'url': 'https://api.nexx.cloud/v3/748/videos/byid/128907', 
  32         'md5': '828cea195be04e66057b846288295ba1', 
  36             'title': 'Stiftung Warentest', 
  37             'alt_title': 'Wie ein Test abläuft', 
  38             'description': 'md5:d1ddb1ef63de721132abd38639cc2fd2', 
  40             'creator': 'SPIEGEL TV', 
  41             'thumbnail': r
're:^https?://.*\.jpg$', 
  43             'timestamp': 1384264416, 
  44             'upload_date': '20131112', 
  48         'url': 'https://api.nexx.cloud/v3/741/videos/byid/247858', 
  52             'title': 'Return of the Golden Child (OV)', 
  53             'description': 'md5:5d969537509a92b733de21bae249dc63', 
  55             'thumbnail': r
're:^https?://.*\.jpg$', 
  57             'timestamp': 1495033267, 
  58             'upload_date': '20170517', 
  63             'skip_download': True, 
  66         # does not work via arc 
  67         'url': 'nexx:741:1269984', 
  68         'md5': 'c714b5b238b2958dc8d5642addba6886', 
  72             'title': '1 TAG ohne KLO... wortwörtlich! 😑', 
  73             'alt_title': '1 TAG ohne KLO... wortwörtlich! 😑', 
  74             'description': 'md5:4604539793c49eda9443ab5c5b1d612f', 
  75             'thumbnail': r
're:^https?://.*\.jpg$', 
  77             'timestamp': 1518614955, 
  78             'upload_date': '20180214', 
  81         'url': 'https://api.nexxcdn.com/v3/748/videos/byid/128907', 
  82         'only_matching': True, 
  84         'url': 'nexx:748:128907', 
  85         'only_matching': True, 
  88         'only_matching': True, 
  90         'url': 'https://arc.nexx.cloud/api/video/128907.json', 
  91         'only_matching': True, 
  95     def _extract_domain_id(webpage
): 
  97             r
'<script\b[^>]+\bsrc=["\'](?
:https?
:)?
//require\
.nexx(?
:\
.cloud|cdn\
.com
)/(?P
<id>\d
+)', 
  99         return mobj.group('id') if mobj else None 
 102     def _extract_urls(webpage): 
 104         # 1. https://nx-s.akamaized.net/files/201510/44.pdf 
 108         # JavaScript Integration 
 109         domain_id = NexxIE._extract_domain_id(webpage) 
 111             for video_id in re.findall( 
 112                     r'(?
is)onPLAYReady
.+?_play\
.init\s
*\
(.+?\s
*,\s
*["\']?(\d+)', 
 115                     'https://api.nexx.cloud/v3/%s/videos/byid/%s' 
 116                     % (domain_id, video_id)) 
 118         # TODO: support more embed formats 
 123     def _extract_url(webpage): 
 124         return NexxIE._extract_urls(webpage)[0] 
 126     def _handle_error(self, response): 
 127         status = int_or_none(try_get( 
 128             response, lambda x: x['metadata']['status']) or 200) 
 129         if 200 <= status < 300: 
 131         raise ExtractorError( 
 132             '%s said: %s' % (self.IE_NAME, response['metadata']['errorhint']), 
 135     def _call_api(self, domain_id, path, video_id, data=None, headers={}): 
 136         headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8' 
 137         result = self._download_json( 
 138             'https://api.nexx.cloud/v3/%s/%s' % (domain_id, path), video_id, 
 139             'Downloading %s JSON' % path, data=urlencode_postdata(data), 
 141         self._handle_error(result) 
 142         return result['result'] 
 144     def _real_extract(self, url): 
 145         mobj = re.match(self._VALID_URL, url) 
 146         domain_id = mobj.group('domain_id') or mobj.group('domain_id_s') 
 147         video_id = mobj.group('id') 
 151         response = self._download_json( 
 152             'https://arc.nexx.cloud/api/video/%s.json' % video_id, 
 153             video_id, fatal=False) 
 154         if response and isinstance(response, dict): 
 155             result = response.get('result') 
 156             if result and isinstance(result, dict): 
 159         # not all videos work via arc, e.g. nexx:741:1269984 
 161             # Reverse engineered from JS code (see getDeviceID function) 
 162             device_id = '%d:%d:%d%d' % ( 
 163                 random.randint(1, 4), int(time.time()), 
 164                 random.randint(1e4, 99999), random.randint(1, 9)) 
 166             result = self._call_api(domain_id, 'session/init', video_id, data={ 
 167                 'nxp_devh': device_id, 
 173                 'playerversion': '6.0.00', 
 176                 'explicitlanguage': 'en-US', 
 177                 'addTextTemplates': '1', 
 178                 'addDomainData': '1', 
 181                 'X-Request-Enable-Auth-Fallback': '1', 
 184             cid = result['general']['cid'] 
 186             # As described in [1] X-Request-Token generation algorithm is 
 188             #   md5( operation + domain_id + domain_secret ) 
 189             # where domain_secret is a static value that will be given by nexx.tv 
 190             # as per [1]. Here is how this "secret
" is generated (reversed 
 191             # from _play.api.init function, search for clienttoken). So it's 
 192             # actually not static and not that much of a secret. 
 193             # 1. https://nexxtvstorage.blob.core.windows.net/files/201610/27.pdf 
 194             secret = result['device']['clienttoken'][int(device_id[0]):] 
 195             secret = secret[0:len(secret) - int(device_id[-1])] 
 199             # Reversed from JS code for _play.api.call function (search for 
 201             request_token = hashlib.md5( 
 202                 ''.join((op, domain_id, secret)).encode('utf-8')).hexdigest() 
 204             video = self._call_api( 
 205                 domain_id, 'videos/%s/%s' % (op, video_id), video_id, data={ 
 206                     'additionalfields': 'language,channel,actors,studio,licenseby,slug,subtitle,teaser,description', 
 207                     'addInteractionOptions': '1', 
 208                     'addStatusDetails': '1', 
 209                     'addStreamDetails': '1', 
 214                     'captionFormat': 'data', 
 216                     'X-Request-CID': cid, 
 217                     'X-Request-Token': request_token, 
 220         general = video['general'] 
 221         title = general['title'] 
 223         stream_data = video['streamdata'] 
 224         language = general.get('language_raw') or '' 
 226         # TODO: reverse more cdns 
 228         cdn = stream_data['cdnType'] 
 229         assert cdn == 'azure' 
 231         azure_locator = stream_data['azureLocator'] 
 233         AZURE_URL = 'http://nx%s%02d.akamaized.net/' 
 235         def get_cdn_shield_base(shield_type='', prefix='-p'): 
 236             for secure in ('', 's'): 
 237                 cdn_shield = stream_data.get('cdnShield%sHTTP%s' % (shield_type, secure.upper())) 
 239                     return 'http%s://%s' % (secure, cdn_shield) 
 241                 return AZURE_URL % (prefix, int(stream_data['azureAccount'].replace('nexxplayplus', ''))) 
 243         azure_stream_base = get_cdn_shield_base() 
 244         is_ml = ',' in language 
 245         azure_manifest_url = '%s%s/%s_src%s.ism/Manifest' % ( 
 246             azure_stream_base, azure_locator, video_id, ('_manifest' if is_ml else '')) + '%s' 
 248         protection_token = try_get( 
 249             video, lambda x: x['protectiondata']['token'], compat_str) 
 251             azure_manifest_url += '?hdnts=%s' % protection_token 
 253         formats = self._extract_m3u8_formats( 
 254             azure_manifest_url % '(format=m3u8-aapl)', 
 255             video_id, 'mp4', 'm3u8_native', 
 256             m3u8_id='%s-hls' % cdn, fatal=False) 
 257         formats.extend(self._extract_mpd_formats( 
 258             azure_manifest_url % '(format=mpd-time-csf)', 
 259             video_id, mpd_id='%s-dash' % cdn, fatal=False)) 
 260         formats.extend(self._extract_ism_formats( 
 261             azure_manifest_url % '', video_id, ism_id='%s-mss' % cdn, fatal=False)) 
 263         azure_progressive_base = get_cdn_shield_base('Prog', '-d') 
 264         azure_file_distribution = stream_data.get('azureFileDistribution') 
 265         if azure_file_distribution: 
 266             fds = azure_file_distribution.split(',') 
 271                         tbr = int_or_none(ss[0]) 
 274                                 'url': '%s%s/%s_src_%s_%d.mp4' % ( 
 275                                     azure_progressive_base, azure_locator, video_id, ss[1], tbr), 
 276                                 'format_id': '%s-http-%d' % (cdn, tbr), 
 279                             width_height = ss[1].split('x') 
 280                             if len(width_height) == 2: 
 282                                     'width': int_or_none(width_height[0]), 
 283                                     'height': int_or_none(width_height[1]), 
 287         self._sort_formats(formats) 
 292             'alt_title': general.get('subtitle'), 
 293             'description': general.get('description'), 
 294             'release_year': int_or_none(general.get('year')), 
 295             'creator': general.get('studio') or general.get('studio_adref'), 
 296             'thumbnail': try_get( 
 297                 video, lambda x: x['imagedata']['thumb'], compat_str), 
 298             'duration': parse_duration(general.get('runtime')), 
 299             'timestamp': int_or_none(general.get('uploaded')), 
 300             'episode_number': int_or_none(try_get( 
 301                 video, lambda x: x['episodedata']['episode'])), 
 302             'season_number': int_or_none(try_get( 
 303                 video, lambda x: x['episodedata']['season'])), 
 308 class NexxEmbedIE(InfoExtractor): 
 309     _VALID_URL = r'https?://embed\.nexx(?:\.cloud|cdn\.com)/\d+/(?P<id>[^/?#&]+)' 
 311         'url': 'http://embed.nexx.cloud/748/KC1614647Z27Y7T?autoplay=1', 
 312         'md5': '16746bfc28c42049492385c989b26c4a', 
 316             'title': 'Nervenkitzel Achterbahn', 
 317             'alt_title': 'Karussellbauer in Deutschland', 
 318             'description': 'md5:ffe7b1cc59a01f585e0569949aef73cc', 
 319             'release_year': 2005, 
 320             'creator': 'SPIEGEL TV', 
 321             'thumbnail': r're:^https?://.*\.jpg$', 
 323             'timestamp': 1394021479, 
 324             'upload_date': '20140305', 
 327             'format': 'bestvideo', 
 328             'skip_download': True, 
 333     def _extract_urls(webpage): 
 335         # 1. https://nx-s.akamaized.net/files/201510/44.pdf 
 337         # iFrame Embed Integration 
 338         return [mobj.group('url') for mobj in re.finditer( 
 339             r'<iframe[^>]+\bsrc=(["\'])(?P
<url
>(?
:https?
:)?
//embed\
.nexx(?
:\
.cloud|cdn\
.com
)/\d
+/(?
:(?
!\
1).)+)\
1', 
 342     def _real_extract(self, url): 
 343         embed_id = self._match_id(url) 
 345         webpage = self._download_webpage(url, embed_id) 
 347         return self.url_result(NexxIE._extract_url(webpage), ie=NexxIE.ie_key())