]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/limelight.py
[extractor] Support multiple archive ids for one video (#4307)
[yt-dlp.git] / yt_dlp / extractor / limelight.py
CommitLineData
ef5acfe3 1import re
2
3from .common import InfoExtractor
3444844b 4from ..compat import compat_HTTPError
ef5acfe3 5from ..utils import (
ef5acfe3 6 determine_ext,
d7fc5631
S
7 float_or_none,
8 int_or_none,
e5d39886 9 smuggle_url,
2b4e1ace 10 try_get,
454e5cdb 11 unsmuggle_url,
3444844b 12 ExtractorError,
ef5acfe3 13)
14
15
d7fc5631
S
16class LimelightBaseIE(InfoExtractor):
17 _PLAYLIST_SERVICE_URL = 'http://production-ps.lvp.llnw.net/r/PlaylistService/%s/%s/%s'
ef5acfe3 18
e5d39886
S
19 @classmethod
20 def _extract_urls(cls, webpage, source_url):
21 lm = {
22 'Media': 'media',
23 'Channel': 'channel',
24 'ChannelList': 'channel_list',
25 }
4ef91524
S
26
27 def smuggle(url):
28 return smuggle_url(url, {'source_url': source_url})
29
e5d39886
S
30 entries = []
31 for kind, video_id in re.findall(
32 r'LimelightPlayer\.doLoad(Media|Channel|ChannelList)\(["\'](?P<id>[a-z0-9]{32})',
33 webpage):
e5d39886 34 entries.append(cls.url_result(
4ef91524 35 smuggle('limelight:%s:%s' % (lm[kind], video_id)),
e5d39886
S
36 'Limelight%s' % kind, video_id))
37 for mobj in re.finditer(
38 # As per [1] class attribute should be exactly equal to
39 # LimelightEmbeddedPlayerFlash but numerous examples seen
40 # that don't exactly match it (e.g. [2]).
41 # 1. http://support.3playmedia.com/hc/en-us/articles/227732408-Limelight-Embedding-the-Captions-Plugin-with-the-Limelight-Player-on-Your-Webpage
42 # 2. http://www.sedona.com/FacilitatorTraining2017
43 r'''(?sx)
44 <object[^>]+class=(["\'])(?:(?!\1).)*\bLimelightEmbeddedPlayerFlash\b(?:(?!\1).)*\1[^>]*>.*?
45 <param[^>]+
46 name=(["\'])flashVars\2[^>]+
91bc57e4 47 value=(["\'])(?:(?!\3).)*(?P<kind>media|channel(?:List)?)Id=(?P<id>[a-z0-9]{32})
e5d39886 48 ''', webpage):
91bc57e4 49 kind, video_id = mobj.group('kind'), mobj.group('id')
e5d39886 50 entries.append(cls.url_result(
4ef91524 51 smuggle('limelight:%s:%s' % (kind, video_id)),
91bc57e4 52 'Limelight%s' % kind.capitalize(), video_id))
4ef91524
S
53 # http://support.3playmedia.com/hc/en-us/articles/115009517327-Limelight-Embedding-the-Audio-Description-Plugin-with-the-Limelight-Player-on-Your-Web-Page)
54 for video_id in re.findall(
55 r'(?s)LimelightPlayerUtil\.embed\s*\(\s*{.*?\bmediaId["\']\s*:\s*["\'](?P<id>[a-z0-9]{32})',
56 webpage):
57 entries.append(cls.url_result(
58 smuggle('limelight:media:%s' % video_id),
59 LimelightMediaIE.ie_key(), video_id))
e5d39886
S
60 return entries
61
454e5cdb
RA
62 def _call_playlist_service(self, item_id, method, fatal=True, referer=None):
63 headers = {}
64 if referer:
65 headers['Referer'] = referer
3444844b
RA
66 try:
67 return self._download_json(
68 self._PLAYLIST_SERVICE_URL % (self._PLAYLIST_SERVICE_PATH, item_id, method),
2e20cb36
RA
69 item_id, 'Downloading PlaylistService %s JSON' % method,
70 fatal=fatal, headers=headers)
3444844b
RA
71 except ExtractorError as e:
72 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
73 error = self._parse_json(e.cause.read().decode(), item_id)['detail']['contentAccessPermission']
74 if error == 'CountryDisabled':
75 self.raise_geo_restricted()
76 raise ExtractorError(error, expected=True)
77 raise
ef5acfe3 78
2e20cb36 79 def _extract(self, item_id, pc_method, mobile_method, referer=None):
454e5cdb 80 pc = self._call_playlist_service(item_id, pc_method, referer=referer)
2e20cb36
RA
81 mobile = self._call_playlist_service(
82 item_id, mobile_method, fatal=False, referer=referer)
83 return pc, mobile
84
85 def _extract_info(self, pc, mobile, i, referer):
86 get_item = lambda x, y: try_get(x, lambda x: x[y][i], dict) or {}
87 pc_item = get_item(pc, 'playlistItems')
88 mobile_item = get_item(mobile, 'mediaList')
89 video_id = pc_item.get('mediaId') or mobile_item['mediaId']
90 title = pc_item.get('title') or mobile_item['title']
d7fc5631 91
ef5acfe3 92 formats = []
f8fd510e 93 urls = []
2e20cb36 94 for stream in pc_item.get('streams', []):
d7fc5631 95 stream_url = stream.get('url')
06869367 96 if not stream_url or stream_url in urls:
97 continue
a06916d9 98 if not self.get_param('allow_unplayable_formats') and stream.get('drmProtected'):
d7fc5631 99 continue
f8fd510e 100 urls.append(stream_url)
e382b953
RA
101 ext = determine_ext(stream_url)
102 if ext == 'f4m':
8f1fddc8 103 formats.extend(self._extract_f4m_formats(
e382b953 104 stream_url, video_id, f4m_id='hds', fatal=False))
ef5acfe3 105 else:
106 fmt = {
d7fc5631
S
107 'url': stream_url,
108 'abr': float_or_none(stream.get('audioBitRate')),
d7fc5631 109 'fps': float_or_none(stream.get('videoFrameRate')),
e382b953 110 'ext': ext,
ef5acfe3 111 }
a6f3a162
RA
112 width = int_or_none(stream.get('videoWidthInPixels'))
113 height = int_or_none(stream.get('videoHeightInPixels'))
114 vbr = float_or_none(stream.get('videoBitRate'))
115 if width or height or vbr:
116 fmt.update({
117 'width': width,
118 'height': height,
119 'vbr': vbr,
120 })
121 else:
122 fmt['vcodec'] = 'none'
123 rtmp = re.search(r'^(?P<url>rtmpe?://(?P<host>[^/]+)/(?P<app>.+))/(?P<playpath>mp[34]:.+)$', stream_url)
ef5acfe3 124 if rtmp:
d7fc5631
S
125 format_id = 'rtmp'
126 if stream.get('videoBitRate'):
127 format_id += '-%d' % int_or_none(stream['videoBitRate'])
906420ca
S
128 http_format_id = format_id.replace('rtmp', 'http')
129
130 CDN_HOSTS = (
131 ('delvenetworks.com', 'cpl.delvenetworks.com'),
132 ('video.llnw.net', 's2.content.video.llnw.net'),
133 )
134 for cdn_host, http_host in CDN_HOSTS:
135 if cdn_host not in rtmp.group('host').lower():
136 continue
137 http_url = 'http://%s/%s' % (http_host, rtmp.group('playpath')[4:])
138 urls.append(http_url)
139 if self._is_valid_url(http_url, video_id, http_format_id):
140 http_fmt = fmt.copy()
141 http_fmt.update({
142 'url': http_url,
143 'format_id': http_format_id,
144 })
145 formats.append(http_fmt)
146 break
147
ef5acfe3 148 fmt.update({
149 'url': rtmp.group('url'),
150 'play_path': rtmp.group('playpath'),
151 'app': rtmp.group('app'),
d7fc5631
S
152 'ext': 'flv',
153 'format_id': format_id,
ef5acfe3 154 })
155 formats.append(fmt)
156
2e20cb36 157 for mobile_url in mobile_item.get('mobileUrls', []):
d7fc5631 158 media_url = mobile_url.get('mobileUrl')
d7fc5631 159 format_id = mobile_url.get('targetMediaPlatform')
b982cbdd 160 if not media_url or media_url in urls:
161 continue
162 if (format_id in ('Widevine', 'SmoothStreaming')
a06916d9 163 and not self.get_param('allow_unplayable_formats', False)):
e382b953 164 continue
f8fd510e 165 urls.append(media_url)
e382b953
RA
166 ext = determine_ext(media_url)
167 if ext == 'm3u8':
d7fc5631 168 formats.extend(self._extract_m3u8_formats(
8f1fddc8 169 media_url, video_id, 'mp4', 'm3u8_native',
170 m3u8_id=format_id, fatal=False))
e382b953
RA
171 elif ext == 'f4m':
172 formats.extend(self._extract_f4m_formats(
173 stream_url, video_id, f4m_id=format_id, fatal=False))
d7fc5631
S
174 else:
175 formats.append({
176 'url': media_url,
177 'format_id': format_id,
f983b875 178 'quality': -10,
e382b953 179 'ext': ext,
d7fc5631
S
180 })
181
ef5acfe3 182 self._sort_formats(formats)
183
d7fc5631 184 subtitles = {}
2e20cb36
RA
185 for flag in mobile_item.get('flags'):
186 if flag == 'ClosedCaptions':
187 closed_captions = self._call_playlist_service(
188 video_id, 'getClosedCaptionsDetailsByMediaId',
189 False, referer) or []
190 for cc in closed_captions:
191 cc_url = cc.get('webvttFileUrl')
192 if not cc_url:
193 continue
a44ca5a4 194 lang = cc.get('languageCode') or self._search_regex(r'/([a-z]{2})\.vtt', cc_url, 'lang', default='en')
2e20cb36
RA
195 subtitles.setdefault(lang, []).append({
196 'url': cc_url,
197 })
198 break
199
200 get_meta = lambda x: pc_item.get(x) or mobile_item.get(x)
ef5acfe3 201
202 return {
203 'id': video_id,
204 'title': title,
2e20cb36 205 'description': get_meta('description'),
ef5acfe3 206 'formats': formats,
2e20cb36
RA
207 'duration': float_or_none(get_meta('durationInMilliseconds'), 1000),
208 'thumbnail': get_meta('previewImageUrl') or get_meta('thumbnailImageUrl'),
ef5acfe3 209 'subtitles': subtitles,
210 }
211
212
d7fc5631 213class LimelightMediaIE(LimelightBaseIE):
ef5acfe3 214 IE_NAME = 'limelight'
79027c0e
S
215 _VALID_URL = r'''(?x)
216 (?:
217 limelight:media:|
218 https?://
219 (?:
220 link\.videoplatform\.limelight\.com/media/|
221 assets\.delvenetworks\.com/player/loader\.swf
222 )
223 \?.*?\bmediaId=
224 )
225 (?P<id>[a-z0-9]{32})
226 '''
9c544e25 227 _TESTS = [{
ef5acfe3 228 'url': 'http://link.videoplatform.limelight.com/media/?mediaId=3ffd040b522b4485b6d84effc750cd86',
ef5acfe3 229 'info_dict': {
230 'id': '3ffd040b522b4485b6d84effc750cd86',
e382b953 231 'ext': 'mp4',
ef5acfe3 232 'title': 'HaP and the HB Prince Trailer',
9c544e25 233 'description': 'md5:8005b944181778e313d95c1237ddb640',
ec85ded8 234 'thumbnail': r're:^https?://.*\.jpeg$',
d7fc5631 235 'duration': 144.23,
d7fc5631
S
236 },
237 'params': {
e382b953 238 # m3u8 download
d7fc5631
S
239 'skip_download': True,
240 },
9c544e25
S
241 }, {
242 # video with subtitles
243 'url': 'limelight:media:a3e00274d4564ec4a9b29b9466432335',
42b7a5af 244 'md5': '2fa3bad9ac321e23860ca23bc2c69e3d',
9c544e25
S
245 'info_dict': {
246 'id': 'a3e00274d4564ec4a9b29b9466432335',
42b7a5af 247 'ext': 'mp4',
9c544e25 248 'title': '3Play Media Overview Video',
ec85ded8 249 'thumbnail': r're:^https?://.*\.jpeg$',
9c544e25 250 'duration': 78.101,
2e20cb36
RA
251 # TODO: extract all languages that were accessible via API
252 # 'subtitles': 'mincount:9',
253 'subtitles': 'mincount:1',
9c544e25 254 },
79027c0e
S
255 }, {
256 'url': 'https://assets.delvenetworks.com/player/loader.swf?mediaId=8018a574f08d416e95ceaccae4ba0452',
257 'only_matching': True,
9c544e25 258 }]
d7fc5631 259 _PLAYLIST_SERVICE_PATH = 'media'
ef5acfe3 260
261 def _real_extract(self, url):
454e5cdb 262 url, smuggled_data = unsmuggle_url(url, {})
ef5acfe3 263 video_id = self._match_id(url)
2e20cb36 264 source_url = smuggled_data.get('source_url')
5f95927a
S
265 self._initialize_geo_bypass({
266 'countries': smuggled_data.get('geo_countries'),
267 })
ef5acfe3 268
2e20cb36 269 pc, mobile = self._extract(
454e5cdb 270 video_id, 'getPlaylistByMediaId',
2e20cb36 271 'getMobilePlaylistByMediaId', source_url)
ef5acfe3 272
2e20cb36 273 return self._extract_info(pc, mobile, 0, source_url)
ef5acfe3 274
275
d7fc5631 276class LimelightChannelIE(LimelightBaseIE):
ef5acfe3 277 IE_NAME = 'limelight:channel'
79027c0e
S
278 _VALID_URL = r'''(?x)
279 (?:
280 limelight:channel:|
281 https?://
282 (?:
283 link\.videoplatform\.limelight\.com/media/|
284 assets\.delvenetworks\.com/player/loader\.swf
285 )
286 \?.*?\bchannelId=
287 )
288 (?P<id>[a-z0-9]{32})
289 '''
290 _TESTS = [{
ef5acfe3 291 'url': 'http://link.videoplatform.limelight.com/media/?channelId=ab6a524c379342f9b23642917020c082',
292 'info_dict': {
293 'id': 'ab6a524c379342f9b23642917020c082',
294 'title': 'Javascript Sample Code',
2e20cb36 295 'description': 'Javascript Sample Code - http://www.delvenetworks.com/sample-code/playerCode-demo.html',
ef5acfe3 296 },
297 'playlist_mincount': 3,
79027c0e
S
298 }, {
299 'url': 'http://assets.delvenetworks.com/player/loader.swf?channelId=ab6a524c379342f9b23642917020c082',
300 'only_matching': True,
301 }]
d7fc5631 302 _PLAYLIST_SERVICE_PATH = 'channel'
ef5acfe3 303
304 def _real_extract(self, url):
454e5cdb 305 url, smuggled_data = unsmuggle_url(url, {})
ef5acfe3 306 channel_id = self._match_id(url)
2e20cb36 307 source_url = smuggled_data.get('source_url')
ef5acfe3 308
2e20cb36 309 pc, mobile = self._extract(
d7fc5631 310 channel_id, 'getPlaylistByChannelId',
454e5cdb 311 'getMobilePlaylistWithNItemsByChannelId?begin=0&count=-1',
2e20cb36 312 source_url)
ef5acfe3 313
d7fc5631 314 entries = [
2e20cb36
RA
315 self._extract_info(pc, mobile, i, source_url)
316 for i in range(len(pc['playlistItems']))]
ef5acfe3 317
2e20cb36
RA
318 return self.playlist_result(
319 entries, channel_id, pc.get('title'), mobile.get('description'))
ef5acfe3 320
321
d7fc5631 322class LimelightChannelListIE(LimelightBaseIE):
ef5acfe3 323 IE_NAME = 'limelight:channel_list'
79027c0e
S
324 _VALID_URL = r'''(?x)
325 (?:
326 limelight:channel_list:|
327 https?://
328 (?:
329 link\.videoplatform\.limelight\.com/media/|
330 assets\.delvenetworks\.com/player/loader\.swf
331 )
332 \?.*?\bchannelListId=
333 )
334 (?P<id>[a-z0-9]{32})
335 '''
336 _TESTS = [{
ef5acfe3 337 'url': 'http://link.videoplatform.limelight.com/media/?channelListId=301b117890c4465c8179ede21fd92e2b',
338 'info_dict': {
339 'id': '301b117890c4465c8179ede21fd92e2b',
340 'title': 'Website - Hero Player',
341 },
342 'playlist_mincount': 2,
79027c0e
S
343 }, {
344 'url': 'https://assets.delvenetworks.com/player/loader.swf?channelListId=301b117890c4465c8179ede21fd92e2b',
345 'only_matching': True,
346 }]
d7fc5631 347 _PLAYLIST_SERVICE_PATH = 'channel_list'
ef5acfe3 348
349 def _real_extract(self, url):
350 channel_list_id = self._match_id(url)
351
2e20cb36
RA
352 channel_list = self._call_playlist_service(
353 channel_list_id, 'getMobileChannelListById')
ef5acfe3 354
d7fc5631
S
355 entries = [
356 self.url_result('limelight:channel:%s' % channel['id'], 'LimelightChannel')
357 for channel in channel_list['channelList']]
ef5acfe3 358
2e20cb36
RA
359 return self.playlist_result(
360 entries, channel_list_id, channel_list['title'])