3 from .adobepass
import AdobePassIE
4 from ..compat
import compat_str
20 class TurnerBaseIE(AdobePassIE
):
21 _AKAMAI_SPE_TOKEN_CACHE
= {}
23 def _extract_timestamp(self
, video_data
):
24 return int_or_none(xpath_attr(video_data
, 'dateCreated', 'uts'))
26 def _add_akamai_spe_token(self
, tokenizer_src
, video_url
, content_id
, ap_data
, custom_tokenizer_query
=None):
27 secure_path
= self
._search
_regex
(r
'https?://[^/]+(.+/)', video_url
, 'secure path') + '*'
28 token
= self
._AKAMAI
_SPE
_TOKEN
_CACHE
.get(secure_path
)
33 if custom_tokenizer_query
:
34 query
.update(custom_tokenizer_query
)
36 query
['videoId'] = content_id
37 if ap_data
.get('auth_required'):
38 query
['accessToken'] = self
._extract
_mvpd
_auth
(ap_data
['url'], content_id
, ap_data
['site_name'], ap_data
['site_name'])
39 auth
= self
._download
_xml
(
40 tokenizer_src
, content_id
, query
=query
)
41 error_msg
= xpath_text(auth
, 'error/msg')
43 raise ExtractorError(error_msg
, expected
=True)
44 token
= xpath_text(auth
, 'token')
47 self
._AKAMAI
_SPE
_TOKEN
_CACHE
[secure_path
] = token
48 return video_url
+ '?hdnea=' + token
50 def _extract_cvp_info(self
, data_src
, video_id
, path_data
={}, ap_data={}
, fatal
=False):
51 video_data
= self
._download
_xml
(
53 transform_source
=lambda s
: fix_xml_ampersands(s
).strip(),
57 video_id
= video_data
.attrib
['id']
58 title
= xpath_text(video_data
, 'headline', fatal
=True)
59 content_id
= xpath_text(video_data
, 'contentId') or video_id
60 # rtmp_src = xpath_text(video_data, 'akamai/src')
62 # split_rtmp_src = rtmp_src.split(',')
63 # if len(split_rtmp_src) == 2:
64 # rtmp_src = split_rtmp_src[1]
65 # aifp = xpath_text(video_data, 'akamai/aifp', default='')
72 r
'(?P<width>[0-9]+)x(?P<height>[0-9]+)(?:_(?P<bitrate>[0-9]+))?')
73 # Possible formats locations: files/file, files/groupFiles/files
75 for video_file
in video_data
.findall('.//file'):
76 video_url
= url_or_none(video_file
.text
.strip())
79 ext
= determine_ext(video_url
)
80 if video_url
.startswith('/mp4:protected/'):
82 # TODO Correct extraction for these files
83 # protected_path_data = path_data.get('protected')
84 # if not protected_path_data or not rtmp_src:
86 # protected_path = self._search_regex(
87 # r'/mp4:(.+)\.[a-z0-9]', video_url, 'secure path')
88 # auth = self._download_webpage(
89 # protected_path_data['tokenizer_src'], query={
90 # 'path': protected_path,
91 # 'videoId': content_id,
94 # token = xpath_text(auth, 'token')
97 # video_url = rtmp_src + video_url + '?' + token
98 elif video_url
.startswith('/secure/'):
99 secure_path_data
= path_data
.get('secure')
100 if not secure_path_data
:
102 video_url
= self
._add
_akamai
_spe
_token
(
103 secure_path_data
['tokenizer_src'],
104 secure_path_data
['media_src'] + video_url
,
106 elif not re
.match('https?://', video_url
):
107 base_path_data
= path_data
.get(ext
, path_data
.get('default', {}))
108 media_src
= base_path_data
.get('media_src')
111 video_url
= media_src
+ video_url
112 if video_url
in urls
:
114 urls
.append(video_url
)
115 format_id
= video_file
.get('bitrate')
116 if ext
in ('scc', 'srt', 'vtt'):
117 subtitles
.setdefault('en', []).append({
127 formats
.extend(self
._extract
_smil
_formats
(
128 video_url
, video_id
, fatal
=False))
129 elif re
.match(r
'https?://[^/]+\.akamaihd\.net/[iz]/', video_url
):
130 formats
.extend(self
._extract
_akamai
_formats
(
131 video_url
, video_id
, {
132 'hds': path_data
.get('f4m', {}).get('host'),
133 # nba.cdn.turner.com, ht.cdn.turner.com, ht2.cdn.turner.com
134 # ht3.cdn.turner.com, i.cdn.turner.com, s.cdn.turner.com
136 'http': 'pmd.cdn.turner.com',
139 m3u8_formats
= self
._extract
_m
3u8_formats
(
140 video_url
, video_id
, 'mp4',
141 m3u8_id
=format_id
or 'hls', fatal
=False)
142 if '/secure/' in video_url
and '?hdnea=' in video_url
:
143 for f
in m3u8_formats
:
144 f
['downloader_options'] = {'ffmpeg_args': ['-seekable', '0']}
145 formats
.extend(m3u8_formats
)
147 formats
.extend(self
._extract
_f
4m
_formats
(
148 update_url_query(video_url
, {'hdcore': '3.7.0'}
),
149 video_id
, f4m_id
=format_id
or 'hds', fatal
=False))
152 'format_id': format_id
,
156 mobj
= rex
.search(video_url
)
159 'width': int(mobj
.group('width')),
160 'height': int(mobj
.group('height')),
161 'tbr': int_or_none(mobj
.group('bitrate')),
163 elif isinstance(format_id
, compat_str
):
164 if format_id
.isdigit():
165 f
['tbr'] = int(format_id
)
167 mobj
= re
.match(r
'ios_(audio|[0-9]+)$', format_id
)
169 if mobj
.group(1) == 'audio':
175 f
['tbr'] = int(mobj
.group(1))
177 self
._sort
_formats
(formats
)
179 for source
in video_data
.findall('closedCaptions/source'):
180 for track
in source
.findall('track'):
181 track_url
= url_or_none(track
.get('url'))
182 if not track_url
or track_url
.endswith('/big'):
184 lang
= track
.get('lang') or track
.get('label') or 'en'
185 subtitles
.setdefault(lang
, []).append({
191 }.get(source
.get('format'))
195 'id': image
.get('cut') or image
.get('name'),
197 'width': int_or_none(image
.get('width')),
198 'height': int_or_none(image
.get('height')),
199 } for image
in video_data
.findall('images/image'))
201 is_live
= xpath_text(video_data
, 'isLive') == 'true'
207 'subtitles': subtitles
,
208 'thumbnails': thumbnails
,
209 'thumbnail': xpath_text(video_data
, 'poster'),
210 'description': strip_or_none(xpath_text(video_data
, 'description')),
211 'duration': parse_duration(xpath_text(video_data
, 'length') or xpath_text(video_data
, 'trt')),
212 'timestamp': self
._extract
_timestamp
(video_data
),
213 'upload_date': xpath_attr(video_data
, 'metas', 'version'),
214 'series': xpath_text(video_data
, 'showTitle'),
215 'season_number': int_or_none(xpath_text(video_data
, 'seasonNumber')),
216 'episode_number': int_or_none(xpath_text(video_data
, 'episodeNumber')),
220 def _extract_ngtv_info(self
, media_id
, tokenizer_query
, ap_data
=None):
221 is_live
= ap_data
.get('is_live')
222 streams_data
= self
._download
_json
(
223 'http://medium.ngtv.io/media/%s/tv' % media_id
,
224 media_id
)['media']['tv']
228 for supported_type
in ('unprotected', 'bulkaes'):
229 stream_data
= streams_data
.get(supported_type
, {})
230 m3u8_url
= stream_data
.get('secureUrl') or stream_data
.get('url')
233 if stream_data
.get('playlistProtection') == 'spe':
234 m3u8_url
= self
._add
_akamai
_spe
_token
(
235 'http://token.ngtv.io/token/token_spe',
236 m3u8_url
, media_id
, ap_data
or {}, tokenizer_query
)
237 formats
.extend(self
._extract
_m
3u8_formats
(
238 m3u8_url
, media_id
, 'mp4', m3u8_id
='hls', live
=is_live
, fatal
=False))
240 duration
= float_or_none(stream_data
.get('totalRuntime'))
242 if not chapters
and not is_live
:
243 for chapter
in stream_data
.get('contentSegments', []):
244 start_time
= float_or_none(chapter
.get('start'))
245 chapter_duration
= float_or_none(chapter
.get('duration'))
246 if start_time
is None or chapter_duration
is None:
249 'start_time': start_time
,
250 'end_time': start_time
+ chapter_duration
,
252 self
._sort
_formats
(formats
)
256 'chapters': chapters
,
257 'duration': duration
,