turner.py 7.5 KB

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