nexx.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. int_or_none,
  8. parse_duration,
  9. try_get,
  10. )
  11. class NexxIE(InfoExtractor):
  12. _VALID_URL = r'''(?x)
  13. (?:
  14. https?://api\.nexx(?:\.cloud|cdn\.com)/v3/(?P<domain_id>\d+)/videos/byid/|
  15. nexx:(?P<domain_id_s>\d+):
  16. )
  17. (?P<id>\d+)
  18. '''
  19. _TESTS = [{
  20. # movie
  21. 'url': 'https://api.nexx.cloud/v3/748/videos/byid/128907',
  22. 'md5': '828cea195be04e66057b846288295ba1',
  23. 'info_dict': {
  24. 'id': '128907',
  25. 'ext': 'mp4',
  26. 'title': 'Stiftung Warentest',
  27. 'alt_title': 'Wie ein Test abläuft',
  28. 'description': 'md5:d1ddb1ef63de721132abd38639cc2fd2',
  29. 'release_year': 2013,
  30. 'creator': 'SPIEGEL TV',
  31. 'thumbnail': r're:^https?://.*\.jpg$',
  32. 'duration': 2509,
  33. 'timestamp': 1384264416,
  34. 'upload_date': '20131112',
  35. },
  36. }, {
  37. # episode
  38. 'url': 'https://api.nexx.cloud/v3/741/videos/byid/247858',
  39. 'info_dict': {
  40. 'id': '247858',
  41. 'ext': 'mp4',
  42. 'title': 'Return of the Golden Child (OV)',
  43. 'description': 'md5:5d969537509a92b733de21bae249dc63',
  44. 'release_year': 2017,
  45. 'thumbnail': r're:^https?://.*\.jpg$',
  46. 'duration': 1397,
  47. 'timestamp': 1495033267,
  48. 'upload_date': '20170517',
  49. 'episode_number': 2,
  50. 'season_number': 2,
  51. },
  52. 'params': {
  53. 'skip_download': True,
  54. },
  55. }, {
  56. 'url': 'https://api.nexxcdn.com/v3/748/videos/byid/128907',
  57. 'only_matching': True,
  58. }, {
  59. 'url': 'nexx:748:128907',
  60. 'only_matching': True,
  61. }]
  62. @staticmethod
  63. def _extract_domain_id(webpage):
  64. mobj = re.search(
  65. r'<script\b[^>]+\bsrc=["\'](?:https?:)?//require\.nexx(?:\.cloud|cdn\.com)/(?P<id>\d+)',
  66. webpage)
  67. return mobj.group('id') if mobj else None
  68. @staticmethod
  69. def _extract_urls(webpage):
  70. # Reference:
  71. # 1. https://nx-s.akamaized.net/files/201510/44.pdf
  72. entries = []
  73. # JavaScript Integration
  74. domain_id = NexxIE._extract_domain_id(webpage)
  75. if domain_id:
  76. for video_id in re.findall(
  77. r'(?is)onPLAYReady.+?_play\.init\s*\(.+?\s*,\s*["\']?(\d+)',
  78. webpage):
  79. entries.append(
  80. 'https://api.nexx.cloud/v3/%s/videos/byid/%s'
  81. % (domain_id, video_id))
  82. # TODO: support more embed formats
  83. return entries
  84. @staticmethod
  85. def _extract_url(webpage):
  86. return NexxIE._extract_urls(webpage)[0]
  87. def _real_extract(self, url):
  88. video_id = self._match_id(url)
  89. video = self._download_json(
  90. 'https://arc.nexx.cloud/api/video/%s.json' % video_id,
  91. video_id)['result']
  92. general = video['general']
  93. title = general['title']
  94. stream_data = video['streamdata']
  95. language = general.get('language_raw') or ''
  96. # TODO: reverse more cdns
  97. cdn = stream_data['cdnType']
  98. assert cdn == 'azure'
  99. azure_locator = stream_data['azureLocator']
  100. AZURE_URL = 'http://nx%s%02d.akamaized.net/'
  101. def get_cdn_shield_base(shield_type='', prefix='-p'):
  102. for secure in ('', 's'):
  103. cdn_shield = stream_data.get('cdnShield%sHTTP%s' % (shield_type, secure.upper()))
  104. if cdn_shield:
  105. return 'http%s://%s' % (secure, cdn_shield)
  106. else:
  107. return AZURE_URL % (prefix, int(stream_data['azureAccount'].replace('nexxplayplus', '')))
  108. azure_stream_base = get_cdn_shield_base()
  109. is_ml = ',' in language
  110. azure_manifest_url = '%s%s/%s_src%s.ism/Manifest' % (
  111. azure_stream_base, azure_locator, video_id, ('_manifest' if is_ml else '')) + '%s'
  112. protection_token = try_get(
  113. video, lambda x: x['protectiondata']['token'], compat_str)
  114. if protection_token:
  115. azure_manifest_url += '?hdnts=%s' % protection_token
  116. formats = self._extract_m3u8_formats(
  117. azure_manifest_url % '(format=m3u8-aapl)',
  118. video_id, 'mp4', 'm3u8_native',
  119. m3u8_id='%s-hls' % cdn, fatal=False)
  120. formats.extend(self._extract_mpd_formats(
  121. azure_manifest_url % '(format=mpd-time-csf)',
  122. video_id, mpd_id='%s-dash' % cdn, fatal=False))
  123. formats.extend(self._extract_ism_formats(
  124. azure_manifest_url % '', video_id, ism_id='%s-mss' % cdn, fatal=False))
  125. azure_progressive_base = get_cdn_shield_base('Prog', '-d')
  126. azure_file_distribution = stream_data.get('azureFileDistribution')
  127. if azure_file_distribution:
  128. fds = azure_file_distribution.split(',')
  129. if fds:
  130. for fd in fds:
  131. ss = fd.split(':')
  132. if len(ss) == 2:
  133. tbr = int_or_none(ss[0])
  134. if tbr:
  135. f = {
  136. 'url': '%s%s/%s_src_%s_%d.mp4' % (
  137. azure_progressive_base, azure_locator, video_id, ss[1], tbr),
  138. 'format_id': '%s-http-%d' % (cdn, tbr),
  139. 'tbr': tbr,
  140. }
  141. width_height = ss[1].split('x')
  142. if len(width_height) == 2:
  143. f.update({
  144. 'width': int_or_none(width_height[0]),
  145. 'height': int_or_none(width_height[1]),
  146. })
  147. formats.append(f)
  148. self._sort_formats(formats)
  149. return {
  150. 'id': video_id,
  151. 'title': title,
  152. 'alt_title': general.get('subtitle'),
  153. 'description': general.get('description'),
  154. 'release_year': int_or_none(general.get('year')),
  155. 'creator': general.get('studio') or general.get('studio_adref'),
  156. 'thumbnail': try_get(
  157. video, lambda x: x['imagedata']['thumb'], compat_str),
  158. 'duration': parse_duration(general.get('runtime')),
  159. 'timestamp': int_or_none(general.get('uploaded')),
  160. 'episode_number': int_or_none(try_get(
  161. video, lambda x: x['episodedata']['episode'])),
  162. 'season_number': int_or_none(try_get(
  163. video, lambda x: x['episodedata']['season'])),
  164. 'formats': formats,
  165. }
  166. class NexxEmbedIE(InfoExtractor):
  167. _VALID_URL = r'https?://embed\.nexx(?:\.cloud|cdn\.com)/\d+/(?P<id>[^/?#&]+)'
  168. _TEST = {
  169. 'url': 'http://embed.nexx.cloud/748/KC1614647Z27Y7T?autoplay=1',
  170. 'md5': '16746bfc28c42049492385c989b26c4a',
  171. 'info_dict': {
  172. 'id': '161464',
  173. 'ext': 'mp4',
  174. 'title': 'Nervenkitzel Achterbahn',
  175. 'alt_title': 'Karussellbauer in Deutschland',
  176. 'description': 'md5:ffe7b1cc59a01f585e0569949aef73cc',
  177. 'release_year': 2005,
  178. 'creator': 'SPIEGEL TV',
  179. 'thumbnail': r're:^https?://.*\.jpg$',
  180. 'duration': 2761,
  181. 'timestamp': 1394021479,
  182. 'upload_date': '20140305',
  183. },
  184. 'params': {
  185. 'format': 'bestvideo',
  186. 'skip_download': True,
  187. },
  188. }
  189. @staticmethod
  190. def _extract_urls(webpage):
  191. # Reference:
  192. # 1. https://nx-s.akamaized.net/files/201510/44.pdf
  193. # iFrame Embed Integration
  194. return [mobj.group('url') for mobj in re.finditer(
  195. r'<iframe[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//embed\.nexx(?:\.cloud|cdn\.com)/\d+/(?:(?!\1).)+)\1',
  196. webpage)]
  197. def _real_extract(self, url):
  198. embed_id = self._match_id(url)
  199. webpage = self._download_webpage(url, embed_id)
  200. return self.url_result(NexxIE._extract_url(webpage), ie=NexxIE.ie_key())