]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/turner.py
New upstream version 2018.09.10
[youtubedl] / youtube_dl / extractor / turner.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .adobepass import AdobePassIE
7 from ..compat import compat_str
8 from ..utils import (
9 xpath_text,
10 int_or_none,
11 determine_ext,
12 float_or_none,
13 parse_duration,
14 xpath_attr,
15 update_url_query,
16 ExtractorError,
17 strip_or_none,
18 url_or_none,
19 )
20
21
22 class TurnerBaseIE(AdobePassIE):
23 _AKAMAI_SPE_TOKEN_CACHE = {}
24
25 def _extract_timestamp(self, video_data):
26 return int_or_none(xpath_attr(video_data, 'dateCreated', 'uts'))
27
28 def _add_akamai_spe_token(self, tokenizer_src, video_url, content_id, ap_data, custom_tokenizer_query=None):
29 secure_path = self._search_regex(r'https?://[^/]+(.+/)', video_url, 'secure path') + '*'
30 token = self._AKAMAI_SPE_TOKEN_CACHE.get(secure_path)
31 if not token:
32 query = {
33 'path': secure_path,
34 }
35 if custom_tokenizer_query:
36 query.update(custom_tokenizer_query)
37 else:
38 query['videoId'] = content_id
39 if ap_data.get('auth_required'):
40 query['accessToken'] = self._extract_mvpd_auth(ap_data['url'], content_id, ap_data['site_name'], ap_data['site_name'])
41 auth = self._download_xml(
42 tokenizer_src, content_id, query=query)
43 error_msg = xpath_text(auth, 'error/msg')
44 if error_msg:
45 raise ExtractorError(error_msg, expected=True)
46 token = xpath_text(auth, 'token')
47 if not token:
48 return video_url
49 self._AKAMAI_SPE_TOKEN_CACHE[secure_path] = token
50 return video_url + '?hdnea=' + token
51
52 def _extract_cvp_info(self, data_src, video_id, path_data={}, ap_data={}):
53 video_data = self._download_xml(data_src, video_id)
54 video_id = video_data.attrib['id']
55 title = xpath_text(video_data, 'headline', fatal=True)
56 content_id = xpath_text(video_data, 'contentId') or video_id
57 # rtmp_src = xpath_text(video_data, 'akamai/src')
58 # if rtmp_src:
59 # splited_rtmp_src = rtmp_src.split(',')
60 # if len(splited_rtmp_src) == 2:
61 # rtmp_src = splited_rtmp_src[1]
62 # aifp = xpath_text(video_data, 'akamai/aifp', default='')
63
64 urls = []
65 formats = []
66 rex = re.compile(
67 r'(?P<width>[0-9]+)x(?P<height>[0-9]+)(?:_(?P<bitrate>[0-9]+))?')
68 # Possible formats locations: files/file, files/groupFiles/files
69 # and maybe others
70 for video_file in video_data.findall('.//file'):
71 video_url = video_file.text.strip()
72 if not video_url:
73 continue
74 ext = determine_ext(video_url)
75 if video_url.startswith('/mp4:protected/'):
76 continue
77 # TODO Correct extraction for these files
78 # protected_path_data = path_data.get('protected')
79 # if not protected_path_data or not rtmp_src:
80 # continue
81 # protected_path = self._search_regex(
82 # r'/mp4:(.+)\.[a-z0-9]', video_url, 'secure path')
83 # auth = self._download_webpage(
84 # protected_path_data['tokenizer_src'], query={
85 # 'path': protected_path,
86 # 'videoId': content_id,
87 # 'aifp': aifp,
88 # })
89 # token = xpath_text(auth, 'token')
90 # if not token:
91 # continue
92 # video_url = rtmp_src + video_url + '?' + token
93 elif video_url.startswith('/secure/'):
94 secure_path_data = path_data.get('secure')
95 if not secure_path_data:
96 continue
97 video_url = self._add_akamai_spe_token(
98 secure_path_data['tokenizer_src'],
99 secure_path_data['media_src'] + video_url,
100 content_id, ap_data)
101 elif not re.match('https?://', video_url):
102 base_path_data = path_data.get(ext, path_data.get('default', {}))
103 media_src = base_path_data.get('media_src')
104 if not media_src:
105 continue
106 video_url = media_src + video_url
107 if video_url in urls:
108 continue
109 urls.append(video_url)
110 format_id = video_file.get('bitrate')
111 if ext == 'smil':
112 formats.extend(self._extract_smil_formats(
113 video_url, video_id, fatal=False))
114 elif ext == 'm3u8':
115 m3u8_formats = self._extract_m3u8_formats(
116 video_url, video_id, 'mp4',
117 m3u8_id=format_id or 'hls', fatal=False)
118 if '/secure/' in video_url and '?hdnea=' in video_url:
119 for f in m3u8_formats:
120 f['_seekable'] = False
121 formats.extend(m3u8_formats)
122 elif ext == 'f4m':
123 formats.extend(self._extract_f4m_formats(
124 update_url_query(video_url, {'hdcore': '3.7.0'}),
125 video_id, f4m_id=format_id or 'hds', fatal=False))
126 else:
127 f = {
128 'format_id': format_id,
129 'url': video_url,
130 'ext': ext,
131 }
132 mobj = rex.search(format_id + video_url)
133 if mobj:
134 f.update({
135 'width': int(mobj.group('width')),
136 'height': int(mobj.group('height')),
137 'tbr': int_or_none(mobj.group('bitrate')),
138 })
139 elif isinstance(format_id, compat_str):
140 if format_id.isdigit():
141 f['tbr'] = int(format_id)
142 else:
143 mobj = re.match(r'ios_(audio|[0-9]+)$', format_id)
144 if mobj:
145 if mobj.group(1) == 'audio':
146 f.update({
147 'vcodec': 'none',
148 'ext': 'm4a',
149 })
150 else:
151 f['tbr'] = int(mobj.group(1))
152 formats.append(f)
153 self._sort_formats(formats)
154
155 subtitles = {}
156 for source in video_data.findall('closedCaptions/source'):
157 for track in source.findall('track'):
158 track_url = url_or_none(track.get('url'))
159 if not track_url or track_url.endswith('/big'):
160 continue
161 lang = track.get('lang') or track.get('label') or 'en'
162 subtitles.setdefault(lang, []).append({
163 'url': track_url,
164 'ext': {
165 'scc': 'scc',
166 'webvtt': 'vtt',
167 'smptett': 'tt',
168 }.get(source.get('format'))
169 })
170
171 thumbnails = [{
172 'id': image.get('cut'),
173 'url': image.text,
174 'width': int_or_none(image.get('width')),
175 'height': int_or_none(image.get('height')),
176 } for image in video_data.findall('images/image')]
177
178 is_live = xpath_text(video_data, 'isLive') == 'true'
179
180 return {
181 'id': video_id,
182 'title': self._live_title(title) if is_live else title,
183 'formats': formats,
184 'subtitles': subtitles,
185 'thumbnails': thumbnails,
186 'thumbnail': xpath_text(video_data, 'poster'),
187 'description': strip_or_none(xpath_text(video_data, 'description')),
188 'duration': parse_duration(xpath_text(video_data, 'length') or xpath_text(video_data, 'trt')),
189 'timestamp': self._extract_timestamp(video_data),
190 'upload_date': xpath_attr(video_data, 'metas', 'version'),
191 'series': xpath_text(video_data, 'showTitle'),
192 'season_number': int_or_none(xpath_text(video_data, 'seasonNumber')),
193 'episode_number': int_or_none(xpath_text(video_data, 'episodeNumber')),
194 'is_live': is_live,
195 }
196
197 def _extract_ngtv_info(self, media_id, tokenizer_query, ap_data=None):
198 streams_data = self._download_json(
199 'http://medium.ngtv.io/media/%s/tv' % media_id,
200 media_id)['media']['tv']
201 duration = None
202 chapters = []
203 formats = []
204 for supported_type in ('unprotected', 'bulkaes'):
205 stream_data = streams_data.get(supported_type, {})
206 m3u8_url = stream_data.get('secureUrl') or stream_data.get('url')
207 if not m3u8_url:
208 continue
209 if stream_data.get('playlistProtection') == 'spe':
210 m3u8_url = self._add_akamai_spe_token(
211 'http://token.ngtv.io/token/token_spe',
212 m3u8_url, media_id, ap_data or {}, tokenizer_query)
213 formats.extend(self._extract_m3u8_formats(
214 m3u8_url, media_id, 'mp4', m3u8_id='hls', fatal=False))
215
216 duration = float_or_none(stream_data.get('totalRuntime'))
217
218 if not chapters:
219 for chapter in stream_data.get('contentSegments', []):
220 start_time = float_or_none(chapter.get('start'))
221 chapter_duration = float_or_none(chapter.get('duration'))
222 if start_time is None or chapter_duration is None:
223 continue
224 chapters.append({
225 'start_time': start_time,
226 'end_time': start_time + chapter_duration,
227 })
228 self._sort_formats(formats)
229
230 return {
231 'formats': formats,
232 'chapters': chapters,
233 'duration': duration,
234 }