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.

190 lines
7.9 KiB

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