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.

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