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.

175 lines
7.4 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. formats.extend(self._extract_m3u8_formats(
  98. video_url, video_id, 'mp4',
  99. m3u8_id=format_id or 'hls', fatal=False))
  100. elif ext == 'f4m':
  101. formats.extend(self._extract_f4m_formats(
  102. update_url_query(video_url, {'hdcore': '3.7.0'}),
  103. video_id, f4m_id=format_id or 'hds', fatal=False))
  104. else:
  105. f = {
  106. 'format_id': format_id,
  107. 'url': video_url,
  108. 'ext': ext,
  109. }
  110. mobj = rex.search(format_id + video_url)
  111. if mobj:
  112. f.update({
  113. 'width': int(mobj.group('width')),
  114. 'height': int(mobj.group('height')),
  115. 'tbr': int_or_none(mobj.group('bitrate')),
  116. })
  117. elif isinstance(format_id, compat_str):
  118. if format_id.isdigit():
  119. f['tbr'] = int(format_id)
  120. else:
  121. mobj = re.match(r'ios_(audio|[0-9]+)$', format_id)
  122. if mobj:
  123. if mobj.group(1) == 'audio':
  124. f.update({
  125. 'vcodec': 'none',
  126. 'ext': 'm4a',
  127. })
  128. else:
  129. f['tbr'] = int(mobj.group(1))
  130. formats.append(f)
  131. self._sort_formats(formats)
  132. subtitles = {}
  133. for source in video_data.findall('closedCaptions/source'):
  134. for track in source.findall('track'):
  135. track_url = track.get('url')
  136. if not isinstance(track_url, compat_str) or track_url.endswith('/big'):
  137. continue
  138. lang = track.get('lang') or track.get('label') or 'en'
  139. subtitles.setdefault(lang, []).append({
  140. 'url': track_url,
  141. 'ext': {
  142. 'scc': 'scc',
  143. 'webvtt': 'vtt',
  144. 'smptett': 'tt',
  145. }.get(source.get('format'))
  146. })
  147. thumbnails = [{
  148. 'id': image.get('cut'),
  149. 'url': image.text,
  150. 'width': int_or_none(image.get('width')),
  151. 'height': int_or_none(image.get('height')),
  152. } for image in video_data.findall('images/image')]
  153. return {
  154. 'id': video_id,
  155. 'title': title,
  156. 'formats': formats,
  157. 'subtitles': subtitles,
  158. 'thumbnails': thumbnails,
  159. 'description': xpath_text(video_data, 'description'),
  160. 'duration': parse_duration(xpath_text(video_data, 'length') or xpath_text(video_data, 'trt')),
  161. 'timestamp': self._extract_timestamp(video_data),
  162. 'upload_date': xpath_attr(video_data, 'metas', 'version'),
  163. 'series': xpath_text(video_data, 'showTitle'),
  164. 'season_number': int_or_none(xpath_text(video_data, 'seasonNumber')),
  165. 'episode_number': int_or_none(xpath_text(video_data, 'episodeNumber')),
  166. }