You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
9.8KB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .adobepass import AdobePassIE
  5. from ..compat import compat_str
  6. from ..utils import (
  7. xpath_text,
  8. int_or_none,
  9. determine_ext,
  10. float_or_none,
  11. parse_duration,
  12. xpath_attr,
  13. update_url_query,
  14. ExtractorError,
  15. strip_or_none,
  16. url_or_none,
  17. )
  18. class TurnerBaseIE(AdobePassIE):
  19. _AKAMAI_SPE_TOKEN_CACHE = {}
  20. def _extract_timestamp(self, video_data):
  21. return int_or_none(xpath_attr(video_data, 'dateCreated', 'uts'))
  22. def _add_akamai_spe_token(self, tokenizer_src, video_url, content_id, ap_data, custom_tokenizer_query=None):
  23. secure_path = self._search_regex(r'https?://[^/]+(.+/)', video_url, 'secure path') + '*'
  24. token = self._AKAMAI_SPE_TOKEN_CACHE.get(secure_path)
  25. if not token:
  26. query = {
  27. 'path': secure_path,
  28. }
  29. if custom_tokenizer_query:
  30. query.update(custom_tokenizer_query)
  31. else:
  32. query['videoId'] = content_id
  33. if ap_data.get('auth_required'):
  34. query['accessToken'] = self._extract_mvpd_auth(ap_data['url'], content_id, ap_data['site_name'], ap_data['site_name'])
  35. auth = self._download_xml(
  36. tokenizer_src, content_id, query=query)
  37. error_msg = xpath_text(auth, 'error/msg')
  38. if error_msg:
  39. raise ExtractorError(error_msg, expected=True)
  40. token = xpath_text(auth, 'token')
  41. if not token:
  42. return video_url
  43. self._AKAMAI_SPE_TOKEN_CACHE[secure_path] = token
  44. return video_url + '?hdnea=' + token
  45. def _extract_cvp_info(self, data_src, video_id, path_data={}, ap_data={}):
  46. video_data = self._download_xml(data_src, video_id)
  47. video_id = video_data.attrib['id']
  48. title = xpath_text(video_data, 'headline', fatal=True)
  49. content_id = xpath_text(video_data, 'contentId') or video_id
  50. # rtmp_src = xpath_text(video_data, 'akamai/src')
  51. # if rtmp_src:
  52. # splited_rtmp_src = rtmp_src.split(',')
  53. # if len(splited_rtmp_src) == 2:
  54. # rtmp_src = splited_rtmp_src[1]
  55. # aifp = xpath_text(video_data, 'akamai/aifp', default='')
  56. urls = []
  57. formats = []
  58. rex = re.compile(
  59. r'(?P<width>[0-9]+)x(?P<height>[0-9]+)(?:_(?P<bitrate>[0-9]+))?')
  60. # Possible formats locations: files/file, files/groupFiles/files
  61. # and maybe others
  62. for video_file in video_data.findall('.//file'):
  63. video_url = video_file.text.strip()
  64. if not video_url:
  65. continue
  66. ext = determine_ext(video_url)
  67. if video_url.startswith('/mp4:protected/'):
  68. continue
  69. # TODO Correct extraction for these files
  70. # protected_path_data = path_data.get('protected')
  71. # if not protected_path_data or not rtmp_src:
  72. # continue
  73. # protected_path = self._search_regex(
  74. # r'/mp4:(.+)\.[a-z0-9]', video_url, 'secure path')
  75. # auth = self._download_webpage(
  76. # protected_path_data['tokenizer_src'], query={
  77. # 'path': protected_path,
  78. # 'videoId': content_id,
  79. # 'aifp': aifp,
  80. # })
  81. # token = xpath_text(auth, 'token')
  82. # if not token:
  83. # continue
  84. # video_url = rtmp_src + video_url + '?' + token
  85. elif video_url.startswith('/secure/'):
  86. secure_path_data = path_data.get('secure')
  87. if not secure_path_data:
  88. continue
  89. video_url = self._add_akamai_spe_token(
  90. secure_path_data['tokenizer_src'],
  91. secure_path_data['media_src'] + video_url,
  92. content_id, ap_data)
  93. elif not re.match('https?://', video_url):
  94. base_path_data = path_data.get(ext, path_data.get('default', {}))
  95. media_src = base_path_data.get('media_src')
  96. if not media_src:
  97. continue
  98. video_url = media_src + video_url
  99. if video_url in urls:
  100. continue
  101. urls.append(video_url)
  102. format_id = video_file.get('bitrate')
  103. if ext == 'smil':
  104. formats.extend(self._extract_smil_formats(
  105. video_url, video_id, fatal=False))
  106. elif ext == 'm3u8':
  107. m3u8_formats = self._extract_m3u8_formats(
  108. video_url, video_id, 'mp4',
  109. m3u8_id=format_id or 'hls', fatal=False)
  110. if '/secure/' in video_url and '?hdnea=' in video_url:
  111. for f in m3u8_formats:
  112. f['_seekable'] = False
  113. formats.extend(m3u8_formats)
  114. elif ext == 'f4m':
  115. formats.extend(self._extract_f4m_formats(
  116. update_url_query(video_url, {'hdcore': '3.7.0'}),
  117. video_id, f4m_id=format_id or 'hds', fatal=False))
  118. else:
  119. f = {
  120. 'format_id': format_id,
  121. 'url': video_url,
  122. 'ext': ext,
  123. }
  124. mobj = rex.search(format_id + video_url)
  125. if mobj:
  126. f.update({
  127. 'width': int(mobj.group('width')),
  128. 'height': int(mobj.group('height')),
  129. 'tbr': int_or_none(mobj.group('bitrate')),
  130. })
  131. elif isinstance(format_id, compat_str):
  132. if format_id.isdigit():
  133. f['tbr'] = int(format_id)
  134. else:
  135. mobj = re.match(r'ios_(audio|[0-9]+)$', format_id)
  136. if mobj:
  137. if mobj.group(1) == 'audio':
  138. f.update({
  139. 'vcodec': 'none',
  140. 'ext': 'm4a',
  141. })
  142. else:
  143. f['tbr'] = int(mobj.group(1))
  144. formats.append(f)
  145. self._sort_formats(formats)
  146. subtitles = {}
  147. for source in video_data.findall('closedCaptions/source'):
  148. for track in source.findall('track'):
  149. track_url = url_or_none(track.get('url'))
  150. if not track_url or track_url.endswith('/big'):
  151. continue
  152. lang = track.get('lang') or track.get('label') or 'en'
  153. subtitles.setdefault(lang, []).append({
  154. 'url': track_url,
  155. 'ext': {
  156. 'scc': 'scc',
  157. 'webvtt': 'vtt',
  158. 'smptett': 'tt',
  159. }.get(source.get('format'))
  160. })
  161. thumbnails = [{
  162. 'id': image.get('cut'),
  163. 'url': image.text,
  164. 'width': int_or_none(image.get('width')),
  165. 'height': int_or_none(image.get('height')),
  166. } for image in video_data.findall('images/image')]
  167. is_live = xpath_text(video_data, 'isLive') == 'true'
  168. return {
  169. 'id': video_id,
  170. 'title': self._live_title(title) if is_live else title,
  171. 'formats': formats,
  172. 'subtitles': subtitles,
  173. 'thumbnails': thumbnails,
  174. 'thumbnail': xpath_text(video_data, 'poster'),
  175. 'description': strip_or_none(xpath_text(video_data, 'description')),
  176. 'duration': parse_duration(xpath_text(video_data, 'length') or xpath_text(video_data, 'trt')),
  177. 'timestamp': self._extract_timestamp(video_data),
  178. 'upload_date': xpath_attr(video_data, 'metas', 'version'),
  179. 'series': xpath_text(video_data, 'showTitle'),
  180. 'season_number': int_or_none(xpath_text(video_data, 'seasonNumber')),
  181. 'episode_number': int_or_none(xpath_text(video_data, 'episodeNumber')),
  182. 'is_live': is_live,
  183. }
  184. def _extract_ngtv_info(self, media_id, tokenizer_query, ap_data=None):
  185. streams_data = self._download_json(
  186. 'http://medium.ngtv.io/media/%s/tv' % media_id,
  187. media_id)['media']['tv']
  188. duration = None
  189. chapters = []
  190. formats = []
  191. for supported_type in ('unprotected', 'bulkaes'):
  192. stream_data = streams_data.get(supported_type, {})
  193. m3u8_url = stream_data.get('secureUrl') or stream_data.get('url')
  194. if not m3u8_url:
  195. continue
  196. if stream_data.get('playlistProtection') == 'spe':
  197. m3u8_url = self._add_akamai_spe_token(
  198. 'http://token.ngtv.io/token/token_spe',
  199. m3u8_url, media_id, ap_data or {}, tokenizer_query)
  200. formats.extend(self._extract_m3u8_formats(
  201. m3u8_url, media_id, 'mp4', m3u8_id='hls', fatal=False))
  202. duration = float_or_none(stream_data.get('totalRuntime'))
  203. if not chapters:
  204. for chapter in stream_data.get('contentSegments', []):
  205. start_time = float_or_none(chapter.get('start'))
  206. chapter_duration = float_or_none(chapter.get('duration'))
  207. if start_time is None or chapter_duration is None:
  208. continue
  209. chapters.append({
  210. 'start_time': start_time,
  211. 'end_time': start_time + chapter_duration,
  212. })
  213. self._sort_formats(formats)
  214. return {
  215. 'formats': formats,
  216. 'chapters': chapters,
  217. 'duration': duration,
  218. }