nexx.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/\d+/videos/byid/|
  15. nexx:(?:\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. 'url': 'nexx:128907',
  63. 'only_matching': True,
  64. }]
  65. @staticmethod
  66. def _extract_domain_id(webpage):
  67. mobj = re.search(
  68. r'<script\b[^>]+\bsrc=["\'](?:https?:)?//require\.nexx(?:\.cloud|cdn\.com)/(?P<id>\d+)',
  69. webpage)
  70. return mobj.group('id') if mobj else None
  71. @staticmethod
  72. def _extract_urls(webpage):
  73. # Reference:
  74. # 1. https://nx-s.akamaized.net/files/201510/44.pdf
  75. entries = []
  76. # JavaScript Integration
  77. domain_id = NexxIE._extract_domain_id(webpage)
  78. if domain_id:
  79. for video_id in re.findall(
  80. r'(?is)onPLAYReady.+?_play\.init\s*\(.+?\s*,\s*["\']?(\d+)',
  81. webpage):
  82. entries.append(
  83. 'https://api.nexx.cloud/v3/%s/videos/byid/%s'
  84. % (domain_id, video_id))
  85. # TODO: support more embed formats
  86. return entries
  87. @staticmethod
  88. def _extract_url(webpage):
  89. return NexxIE._extract_urls(webpage)[0]
  90. def _real_extract(self, url):
  91. video_id = self._match_id(url)
  92. video = self._download_json(
  93. 'https://arc.nexx.cloud/api/video/%s.json' % video_id,
  94. video_id)['result']
  95. general = video['general']
  96. title = general['title']
  97. stream_data = video['streamdata']
  98. language = general.get('language_raw') or ''
  99. # TODO: reverse more cdns
  100. cdn = stream_data['cdnType']
  101. assert cdn == 'azure'
  102. azure_locator = stream_data['azureLocator']
  103. AZURE_URL = 'http://nx%s%02d.akamaized.net/'
  104. def get_cdn_shield_base(shield_type='', prefix='-p'):
  105. for secure in ('', 's'):
  106. cdn_shield = stream_data.get('cdnShield%sHTTP%s' % (shield_type, secure.upper()))
  107. if cdn_shield:
  108. return 'http%s://%s' % (secure, cdn_shield)
  109. else:
  110. return AZURE_URL % (prefix, int(stream_data['azureAccount'].replace('nexxplayplus', '')))
  111. azure_stream_base = get_cdn_shield_base()
  112. is_ml = ',' in language
  113. azure_manifest_url = '%s%s/%s_src%s.ism/Manifest' % (
  114. azure_stream_base, azure_locator, video_id, ('_manifest' if is_ml else '')) + '%s'
  115. protection_token = try_get(
  116. video, lambda x: x['protectiondata']['token'], compat_str)
  117. if protection_token:
  118. azure_manifest_url += '?hdnts=%s' % protection_token
  119. formats = self._extract_m3u8_formats(
  120. azure_manifest_url % '(format=m3u8-aapl)',
  121. video_id, 'mp4', 'm3u8_native',
  122. m3u8_id='%s-hls' % cdn, fatal=False)
  123. formats.extend(self._extract_mpd_formats(
  124. azure_manifest_url % '(format=mpd-time-csf)',
  125. video_id, mpd_id='%s-dash' % cdn, fatal=False))
  126. formats.extend(self._extract_ism_formats(
  127. azure_manifest_url % '', video_id, ism_id='%s-mss' % cdn, fatal=False))
  128. azure_progressive_base = get_cdn_shield_base('Prog', '-d')
  129. azure_file_distribution = stream_data.get('azureFileDistribution')
  130. if azure_file_distribution:
  131. fds = azure_file_distribution.split(',')
  132. if fds:
  133. for fd in fds:
  134. ss = fd.split(':')
  135. if len(ss) == 2:
  136. tbr = int_or_none(ss[0])
  137. if tbr:
  138. f = {
  139. 'url': '%s%s/%s_src_%s_%d.mp4' % (
  140. azure_progressive_base, azure_locator, video_id, ss[1], tbr),
  141. 'format_id': '%s-http-%d' % (cdn, tbr),
  142. 'tbr': tbr,
  143. }
  144. width_height = ss[1].split('x')
  145. if len(width_height) == 2:
  146. f.update({
  147. 'width': int_or_none(width_height[0]),
  148. 'height': int_or_none(width_height[1]),
  149. })
  150. formats.append(f)
  151. self._sort_formats(formats)
  152. return {
  153. 'id': video_id,
  154. 'title': title,
  155. 'alt_title': general.get('subtitle'),
  156. 'description': general.get('description'),
  157. 'release_year': int_or_none(general.get('year')),
  158. 'creator': general.get('studio') or general.get('studio_adref'),
  159. 'thumbnail': try_get(
  160. video, lambda x: x['imagedata']['thumb'], compat_str),
  161. 'duration': parse_duration(general.get('runtime')),
  162. 'timestamp': int_or_none(general.get('uploaded')),
  163. 'episode_number': int_or_none(try_get(
  164. video, lambda x: x['episodedata']['episode'])),
  165. 'season_number': int_or_none(try_get(
  166. video, lambda x: x['episodedata']['season'])),
  167. 'formats': formats,
  168. }
  169. class NexxEmbedIE(InfoExtractor):
  170. _VALID_URL = r'https?://embed\.nexx(?:\.cloud|cdn\.com)/\d+/(?P<id>[^/?#&]+)'
  171. _TEST = {
  172. 'url': 'http://embed.nexx.cloud/748/KC1614647Z27Y7T?autoplay=1',
  173. 'md5': '16746bfc28c42049492385c989b26c4a',
  174. 'info_dict': {
  175. 'id': '161464',
  176. 'ext': 'mp4',
  177. 'title': 'Nervenkitzel Achterbahn',
  178. 'alt_title': 'Karussellbauer in Deutschland',
  179. 'description': 'md5:ffe7b1cc59a01f585e0569949aef73cc',
  180. 'release_year': 2005,
  181. 'creator': 'SPIEGEL TV',
  182. 'thumbnail': r're:^https?://.*\.jpg$',
  183. 'duration': 2761,
  184. 'timestamp': 1394021479,
  185. 'upload_date': '20140305',
  186. },
  187. 'params': {
  188. 'format': 'bestvideo',
  189. 'skip_download': True,
  190. },
  191. }
  192. @staticmethod
  193. def _extract_urls(webpage):
  194. # Reference:
  195. # 1. https://nx-s.akamaized.net/files/201510/44.pdf
  196. # iFrame Embed Integration
  197. return [mobj.group('url') for mobj in re.finditer(
  198. r'<iframe[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//embed\.nexx(?:\.cloud|cdn\.com)/\d+/(?:(?!\1).)+)\1',
  199. webpage)]
  200. def _real_extract(self, url):
  201. embed_id = self._match_id(url)
  202. webpage = self._download_webpage(url, embed_id)
  203. return self.url_result(NexxIE._extract_url(webpage), ie=NexxIE.ie_key())