turner.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. xpath_text,
  7. int_or_none,
  8. determine_ext,
  9. parse_duration,
  10. xpath_attr,
  11. update_url_query,
  12. compat_urlparse,
  13. )
  14. class TurnerBaseIE(InfoExtractor):
  15. def _extract_cvp_info(self, data_src, video_id, path_data={}):
  16. video_data = self._download_xml(data_src, video_id)
  17. video_id = video_data.attrib['id'].split('/')[-1].split('.')[0]
  18. title = xpath_text(video_data, 'headline', fatal=True)
  19. # rtmp_src = xpath_text(video_data, 'akamai/src')
  20. # if rtmp_src:
  21. # splited_rtmp_src = rtmp_src.split(',')
  22. # if len(splited_rtmp_src) == 2:
  23. # rtmp_src = splited_rtmp_src[1]
  24. # aifp = xpath_text(video_data, 'akamai/aifp', default='')
  25. tokens = {}
  26. urls = []
  27. formats = []
  28. rex = re.compile(r'''(?x)
  29. (?P<width>[0-9]+)x(?P<height>[0-9]+)
  30. (?:_(?P<bitrate>[0-9]+))?
  31. ''')
  32. for video_file in video_data.findall('files/file'):
  33. video_url = video_file.text.strip()
  34. if not video_url:
  35. continue
  36. ext = determine_ext(video_url)
  37. if video_url.startswith('/mp4:protected/'):
  38. continue
  39. # TODO Correct extraction for these files
  40. # protected_path_data = path_data.get('protected')
  41. # if not protected_path_data or not rtmp_src:
  42. # continue
  43. # protected_path = self._search_regex(
  44. # r'/mp4:(.+)\.[a-z0-9]', video_url, 'secure path')
  45. # auth = self._download_webpage(
  46. # protected_path_data['tokenizer_src'], query={
  47. # 'path': protected_path,
  48. # 'videoId': video_id,
  49. # 'aifp': aifp,
  50. # })
  51. # token = xpath_text(auth, 'token')
  52. # if not token:
  53. # continue
  54. # video_url = rtmp_src + video_url + '?' + token
  55. elif video_url.startswith('/secure/'):
  56. secure_path_data = path_data.get('secure')
  57. if not secure_path_data:
  58. continue
  59. video_url = secure_path_data['media_src'] + video_url
  60. secure_path = self._search_regex(r'https?://[^/]+(.+/)', video_url, 'secure path') + '*'
  61. token = tokens.get(secure_path)
  62. if not token:
  63. auth = self._download_xml(
  64. secure_path_data['tokenizer_src'], video_id, query={
  65. 'path': secure_path,
  66. 'videoId': video_id,
  67. })
  68. token = xpath_text(auth, 'token')
  69. if not token:
  70. continue
  71. tokens[secure_path] = token
  72. video_url = video_url + '?hdnea=' + token
  73. elif not re.match('https?://', video_url):
  74. base_path_data = path_data.get(ext, path_data.get('default', {}))
  75. media_src = base_path_data.get('media_src')
  76. if not media_src:
  77. continue
  78. video_url = media_src + video_url
  79. if video_url in urls:
  80. continue
  81. urls.append(video_url)
  82. format_id = video_file.attrib['bitrate']
  83. if ext == 'smil':
  84. formats.extend(self._extract_smil_formats(video_url, video_id, fatal=False))
  85. elif ext == 'm3u8':
  86. m3u8_formats = self._extract_m3u8_formats(
  87. video_url, video_id, 'mp4', m3u8_id=format_id, fatal=False)
  88. if m3u8_formats:
  89. # Sometimes final URLs inside m3u8 are unsigned, let's fix this
  90. # ourselves
  91. qs = compat_urlparse.urlparse(video_url).query
  92. if qs:
  93. query = compat_urlparse.parse_qs(qs)
  94. for m3u8_format in m3u8_formats:
  95. m3u8_format['url'] = update_url_query(m3u8_format['url'], query)
  96. m3u8_format['extra_param_to_segment_url'] = qs
  97. formats.extend(m3u8_formats)
  98. elif ext == 'f4m':
  99. formats.extend(self._extract_f4m_formats(
  100. update_url_query(video_url, {'hdcore': '3.7.0'}),
  101. video_id, f4m_id=format_id, fatal=False))
  102. else:
  103. f = {
  104. 'format_id': format_id,
  105. 'url': video_url,
  106. 'ext': ext,
  107. }
  108. mobj = rex.search(format_id + video_url)
  109. if mobj:
  110. f.update({
  111. 'width': int(mobj.group('width')),
  112. 'height': int(mobj.group('height')),
  113. 'tbr': int_or_none(mobj.group('bitrate')),
  114. })
  115. elif format_id.isdigit():
  116. f['tbr'] = int(format_id)
  117. else:
  118. mobj = re.match(r'ios_(audio|[0-9]+)$', format_id)
  119. if mobj:
  120. if mobj.group(1) == 'audio':
  121. f.update({
  122. 'vcodec': 'none',
  123. 'ext': 'm4a',
  124. })
  125. else:
  126. f['tbr'] = int(mobj.group(1))
  127. formats.append(f)
  128. self._sort_formats(formats)
  129. subtitles = {}
  130. for source in video_data.findall('closedCaptions/source'):
  131. for track in source.findall('track'):
  132. source_url = source.get('url')
  133. if not source_url:
  134. continue
  135. subtitles.set_default(source.get('lang') or source.get('label') or 'en', []).append({
  136. 'url': source_url,
  137. 'ext': {
  138. 'scc': 'scc',
  139. 'webvtt': 'vtt',
  140. 'smptett': 'tt',
  141. }.get(source.get('format'))
  142. })
  143. thumbnails = [{
  144. 'id': image.get('cut'),
  145. 'url': image.text,
  146. 'width': int_or_none(image.get('width')),
  147. 'height': int_or_none(image.get('height')),
  148. } for image in video_data.findall('images/image')]
  149. timestamp = None
  150. if 'cnn.com' not in data_src:
  151. timestamp = int_or_none(xpath_attr(video_data, 'dateCreated', 'uts'))
  152. return {
  153. 'id': video_id,
  154. 'title': title,
  155. 'formats': formats,
  156. 'subtitles': subtitles,
  157. 'thumbnails': thumbnails,
  158. 'description': xpath_text(video_data, 'description'),
  159. 'duration': parse_duration(xpath_text(video_data, 'length') or xpath_text(video_data, 'trt')),
  160. 'timestamp': timestamp,
  161. 'upload_date': xpath_attr(video_data, 'metas', 'version'),
  162. 'series': xpath_text(video_data, 'showTitle'),
  163. 'season_number': int_or_none(xpath_text(video_data, 'seasonNumber')),
  164. 'episode_number': int_or_none(xpath_text(video_data, 'episodeNumber')),
  165. }