turner.py 6.6 KB

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