3 from .adobepass
import AdobePassIE
19 class TurnerBaseIE(AdobePassIE
):
20 _AKAMAI_SPE_TOKEN_CACHE
= {}
22 def _extract_timestamp(self
, video_data
):
23 return int_or_none(xpath_attr(video_data
, 'dateCreated', 'uts'))
25 def _add_akamai_spe_token(self
, tokenizer_src
, video_url
, content_id
, ap_data
, custom_tokenizer_query
=None):
26 secure_path
= self
._search
_regex
(r
'https?://[^/]+(.+/)', video_url
, 'secure path') + '*'
27 token
= self
._AKAMAI
_SPE
_TOKEN
_CACHE
.get(secure_path
)
32 if custom_tokenizer_query
:
33 query
.update(custom_tokenizer_query
)
35 query
['videoId'] = content_id
36 if ap_data
.get('auth_required'):
37 query
['accessToken'] = self
._extract
_mvpd
_auth
(ap_data
['url'], content_id
, ap_data
['site_name'], ap_data
['site_name'])
38 auth
= self
._download
_xml
(
39 tokenizer_src
, content_id
, query
=query
)
40 error_msg
= xpath_text(auth
, 'error/msg')
42 raise ExtractorError(error_msg
, expected
=True)
43 token
= xpath_text(auth
, 'token')
46 self
._AKAMAI
_SPE
_TOKEN
_CACHE
[secure_path
] = token
47 return video_url
+ '?hdnea=' + token
49 def _extract_cvp_info(self
, data_src
, video_id
, path_data
={}, ap_data
={}, fatal
=False):
50 video_data
= self
._download
_xml
(
52 transform_source
=lambda s
: fix_xml_ampersands(s
).strip(),
56 video_id
= video_data
.attrib
['id']
57 title
= xpath_text(video_data
, 'headline', fatal
=True)
58 content_id
= xpath_text(video_data
, 'contentId') or video_id
59 # rtmp_src = xpath_text(video_data, 'akamai/src')
61 # split_rtmp_src = rtmp_src.split(',')
62 # if len(split_rtmp_src) == 2:
63 # rtmp_src = split_rtmp_src[1]
64 # aifp = xpath_text(video_data, 'akamai/aifp', default='')
71 r
'(?P<width>[0-9]+)x(?P<height>[0-9]+)(?:_(?P<bitrate>[0-9]+))?')
72 # Possible formats locations: files/file, files/groupFiles/files
74 for video_file
in video_data
.findall('.//file'):
75 video_url
= url_or_none(video_file
.text
.strip())
78 ext
= determine_ext(video_url
)
79 if video_url
.startswith('/mp4:protected/'):
81 # TODO: Correct extraction for these files
82 # protected_path_data = path_data.get('protected')
83 # if not protected_path_data or not rtmp_src:
85 # protected_path = self._search_regex(
86 # r'/mp4:(.+)\.[a-z0-9]', video_url, 'secure path')
87 # auth = self._download_webpage(
88 # protected_path_data['tokenizer_src'], query={
89 # 'path': protected_path,
90 # 'videoId': content_id,
93 # token = xpath_text(auth, 'token')
96 # video_url = rtmp_src + video_url + '?' + token
97 elif video_url
.startswith('/secure/'):
98 secure_path_data
= path_data
.get('secure')
99 if not secure_path_data
:
101 video_url
= self
._add
_akamai
_spe
_token
(
102 secure_path_data
['tokenizer_src'],
103 secure_path_data
['media_src'] + video_url
,
105 elif not re
.match('https?://', video_url
):
106 base_path_data
= path_data
.get(ext
, path_data
.get('default', {}))
107 media_src
= base_path_data
.get('media_src')
110 video_url
= media_src
+ video_url
111 if video_url
in urls
:
113 urls
.append(video_url
)
114 format_id
= video_file
.get('bitrate')
115 if ext
in ('scc', 'srt', 'vtt'):
116 subtitles
.setdefault('en', []).append({
126 formats
.extend(self
._extract
_smil
_formats
(
127 video_url
, video_id
, fatal
=False))
128 elif re
.match(r
'https?://[^/]+\.akamaihd\.net/[iz]/', video_url
):
129 formats
.extend(self
._extract
_akamai
_formats
(
130 video_url
, video_id
, {
131 'hds': path_data
.get('f4m', {}).get('host'),
132 # nba.cdn.turner.com, ht.cdn.turner.com, ht2.cdn.turner.com
133 # ht3.cdn.turner.com, i.cdn.turner.com, s.cdn.turner.com
135 'http': 'pmd.cdn.turner.com',
138 m3u8_formats
= self
._extract
_m
3u8_formats
(
139 video_url
, video_id
, 'mp4',
140 m3u8_id
=format_id
or 'hls', fatal
=False)
141 if '/secure/' in video_url
and '?hdnea=' in video_url
:
142 for f
in m3u8_formats
:
143 f
['downloader_options'] = {'ffmpeg_args': ['-seekable', '0']}
144 formats
.extend(m3u8_formats
)
146 formats
.extend(self
._extract
_f
4m
_formats
(
147 update_url_query(video_url
, {'hdcore': '3.7.0'}),
148 video_id
, f4m_id
=format_id
or 'hds', fatal
=False))
151 'format_id': format_id
,
155 mobj
= rex
.search(video_url
)
158 'width': int(mobj
.group('width')),
159 'height': int(mobj
.group('height')),
160 'tbr': int_or_none(mobj
.group('bitrate')),
162 elif isinstance(format_id
, str):
163 if format_id
.isdigit():
164 f
['tbr'] = int(format_id
)
166 mobj
= re
.match(r
'ios_(audio|[0-9]+)$', format_id
)
168 if mobj
.group(1) == 'audio':
174 f
['tbr'] = int(mobj
.group(1))
177 for source
in video_data
.findall('closedCaptions/source'):
178 for track
in source
.findall('track'):
179 track_url
= url_or_none(track
.get('url'))
180 if not track_url
or track_url
.endswith('/big'):
182 lang
= track
.get('lang') or track
.get('label') or 'en'
183 subtitles
.setdefault(lang
, []).append({
189 }.get(source
.get('format')),
193 'id': image
.get('cut') or image
.get('name'),
195 'width': int_or_none(image
.get('width')),
196 'height': int_or_none(image
.get('height')),
197 } for image
in video_data
.findall('images/image'))
199 is_live
= xpath_text(video_data
, 'isLive') == 'true'
205 'subtitles': subtitles
,
206 'thumbnails': thumbnails
,
207 'thumbnail': xpath_text(video_data
, 'poster'),
208 'description': strip_or_none(xpath_text(video_data
, 'description')),
209 'duration': parse_duration(xpath_text(video_data
, 'length') or xpath_text(video_data
, 'trt')),
210 'timestamp': self
._extract
_timestamp
(video_data
),
211 'upload_date': xpath_attr(video_data
, 'metas', 'version'),
212 'series': xpath_text(video_data
, 'showTitle'),
213 'season_number': int_or_none(xpath_text(video_data
, 'seasonNumber')),
214 'episode_number': int_or_none(xpath_text(video_data
, 'episodeNumber')),
218 def _extract_ngtv_info(self
, media_id
, tokenizer_query
, ap_data
=None):
219 is_live
= ap_data
.get('is_live')
220 streams_data
= self
._download
_json
(
221 f
'http://medium.ngtv.io/media/{media_id}/tv',
222 media_id
)['media']['tv']
226 for supported_type
in ('unprotected', 'bulkaes'):
227 stream_data
= streams_data
.get(supported_type
, {})
228 m3u8_url
= stream_data
.get('secureUrl') or stream_data
.get('url')
231 if stream_data
.get('playlistProtection') == 'spe':
232 m3u8_url
= self
._add
_akamai
_spe
_token
(
233 'http://token.ngtv.io/token/token_spe',
234 m3u8_url
, media_id
, ap_data
or {}, tokenizer_query
)
235 formats
.extend(self
._extract
_m
3u8_formats
(
236 m3u8_url
, media_id
, 'mp4', m3u8_id
='hls', live
=is_live
, fatal
=False))
238 duration
= float_or_none(stream_data
.get('totalRuntime'))
240 if not chapters
and not is_live
:
241 for chapter
in stream_data
.get('contentSegments', []):
242 start_time
= float_or_none(chapter
.get('start'))
243 chapter_duration
= float_or_none(chapter
.get('duration'))
244 if start_time
is None or chapter_duration
is None:
247 'start_time': start_time
,
248 'end_time': start_time
+ chapter_duration
,
253 'chapters': chapters
,
254 'duration': duration
,