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.

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