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