]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/limelight.py
release 2017.02.21
[yt-dlp.git] / youtube_dl / extractor / limelight.py
CommitLineData
ef5acfe3 1# coding: utf-8
2from __future__ import unicode_literals
3
4import re
5
6from .common import InfoExtractor
7from ..utils import (
ef5acfe3 8 determine_ext,
d7fc5631
S
9 float_or_none,
10 int_or_none,
454e5cdb 11 unsmuggle_url,
ef5acfe3 12)
13
14
d7fc5631
S
15class LimelightBaseIE(InfoExtractor):
16 _PLAYLIST_SERVICE_URL = 'http://production-ps.lvp.llnw.net/r/PlaylistService/%s/%s/%s'
17 _API_URL = 'http://api.video.limelight.com/rest/organizations/%s/%s/%s/%s.json'
ef5acfe3 18
454e5cdb
RA
19 def _call_playlist_service(self, item_id, method, fatal=True, referer=None):
20 headers = {}
21 if referer:
22 headers['Referer'] = referer
d7fc5631
S
23 return self._download_json(
24 self._PLAYLIST_SERVICE_URL % (self._PLAYLIST_SERVICE_PATH, item_id, method),
454e5cdb 25 item_id, 'Downloading PlaylistService %s JSON' % method, fatal=fatal, headers=headers)
ef5acfe3 26
d7fc5631
S
27 def _call_api(self, organization_id, item_id, method):
28 return self._download_json(
29 self._API_URL % (organization_id, self._API_PATH, item_id, method),
30 item_id, 'Downloading API %s JSON' % method)
ef5acfe3 31
454e5cdb
RA
32 def _extract(self, item_id, pc_method, mobile_method, meta_method, referer=None):
33 pc = self._call_playlist_service(item_id, pc_method, referer=referer)
d7fc5631 34 metadata = self._call_api(pc['orgId'], item_id, meta_method)
454e5cdb 35 mobile = self._call_playlist_service(item_id, mobile_method, fatal=False, referer=referer)
d7fc5631
S
36 return pc, mobile, metadata
37
38 def _extract_info(self, streams, mobile_urls, properties):
ef5acfe3 39 video_id = properties['media_id']
40 formats = []
f8fd510e 41 urls = []
ef5acfe3 42 for stream in streams:
d7fc5631 43 stream_url = stream.get('url')
f8fd510e 44 if not stream_url or stream.get('drmProtected') or stream_url in urls:
d7fc5631 45 continue
f8fd510e 46 urls.append(stream_url)
e382b953
RA
47 ext = determine_ext(stream_url)
48 if ext == 'f4m':
8f1fddc8 49 formats.extend(self._extract_f4m_formats(
e382b953 50 stream_url, video_id, f4m_id='hds', fatal=False))
ef5acfe3 51 else:
52 fmt = {
d7fc5631
S
53 'url': stream_url,
54 'abr': float_or_none(stream.get('audioBitRate')),
55 'vbr': float_or_none(stream.get('videoBitRate')),
56 'fps': float_or_none(stream.get('videoFrameRate')),
57 'width': int_or_none(stream.get('videoWidthInPixels')),
58 'height': int_or_none(stream.get('videoHeightInPixels')),
e382b953 59 'ext': ext,
ef5acfe3 60 }
42b7a5af 61 rtmp = re.search(r'^(?P<url>rtmpe?://(?P<host>[^/]+)/(?P<app>.+))/(?P<playpath>mp4:.+)$', stream_url)
ef5acfe3 62 if rtmp:
d7fc5631
S
63 format_id = 'rtmp'
64 if stream.get('videoBitRate'):
65 format_id += '-%d' % int_or_none(stream['videoBitRate'])
906420ca
S
66 http_format_id = format_id.replace('rtmp', 'http')
67
68 CDN_HOSTS = (
69 ('delvenetworks.com', 'cpl.delvenetworks.com'),
70 ('video.llnw.net', 's2.content.video.llnw.net'),
71 )
72 for cdn_host, http_host in CDN_HOSTS:
73 if cdn_host not in rtmp.group('host').lower():
74 continue
75 http_url = 'http://%s/%s' % (http_host, rtmp.group('playpath')[4:])
76 urls.append(http_url)
77 if self._is_valid_url(http_url, video_id, http_format_id):
78 http_fmt = fmt.copy()
79 http_fmt.update({
80 'url': http_url,
81 'format_id': http_format_id,
82 })
83 formats.append(http_fmt)
84 break
85
ef5acfe3 86 fmt.update({
87 'url': rtmp.group('url'),
88 'play_path': rtmp.group('playpath'),
89 'app': rtmp.group('app'),
d7fc5631
S
90 'ext': 'flv',
91 'format_id': format_id,
ef5acfe3 92 })
93 formats.append(fmt)
94
d7fc5631
S
95 for mobile_url in mobile_urls:
96 media_url = mobile_url.get('mobileUrl')
d7fc5631 97 format_id = mobile_url.get('targetMediaPlatform')
f8fd510e 98 if not media_url or format_id in ('Widevine', 'SmoothStreaming') or media_url in urls:
e382b953 99 continue
f8fd510e 100 urls.append(media_url)
e382b953
RA
101 ext = determine_ext(media_url)
102 if ext == 'm3u8':
d7fc5631 103 formats.extend(self._extract_m3u8_formats(
8f1fddc8 104 media_url, video_id, 'mp4', 'm3u8_native',
105 m3u8_id=format_id, fatal=False))
e382b953
RA
106 elif ext == 'f4m':
107 formats.extend(self._extract_f4m_formats(
108 stream_url, video_id, f4m_id=format_id, fatal=False))
d7fc5631
S
109 else:
110 formats.append({
111 'url': media_url,
112 'format_id': format_id,
113 'preference': -1,
e382b953 114 'ext': ext,
d7fc5631
S
115 })
116
ef5acfe3 117 self._sort_formats(formats)
118
119 title = properties['title']
120 description = properties.get('description')
d7fc5631
S
121 timestamp = int_or_none(properties.get('publish_date') or properties.get('create_date'))
122 duration = float_or_none(properties.get('duration_in_milliseconds'), 1000)
123 filesize = int_or_none(properties.get('total_storage_in_bytes'))
ef5acfe3 124 categories = [properties.get('category')]
d7fc5631 125 tags = properties.get('tags', [])
ef5acfe3 126 thumbnails = [{
d7fc5631 127 'url': thumbnail['url'],
ef5acfe3 128 'width': int_or_none(thumbnail.get('width')),
129 'height': int_or_none(thumbnail.get('height')),
d7fc5631
S
130 } for thumbnail in properties.get('thumbnails', []) if thumbnail.get('url')]
131
132 subtitles = {}
21ac1a8a 133 for caption in properties.get('captions', []):
d7fc5631
S
134 lang = caption.get('language_code')
135 subtitles_url = caption.get('url')
136 if lang and subtitles_url:
fe458b65 137 subtitles.setdefault(lang, []).append({
d7fc5631 138 'url': subtitles_url,
fe458b65
S
139 })
140 closed_captions_url = properties.get('closed_captions_url')
141 if closed_captions_url:
142 subtitles.setdefault('en', []).append({
143 'url': closed_captions_url,
144 'ext': 'ttml',
145 })
ef5acfe3 146
147 return {
148 'id': video_id,
149 'title': title,
150 'description': description,
151 'formats': formats,
152 'timestamp': timestamp,
153 'duration': duration,
154 'filesize': filesize,
155 'categories': categories,
d7fc5631 156 'tags': tags,
ef5acfe3 157 'thumbnails': thumbnails,
158 'subtitles': subtitles,
159 }
160
161
d7fc5631 162class LimelightMediaIE(LimelightBaseIE):
ef5acfe3 163 IE_NAME = 'limelight'
79027c0e
S
164 _VALID_URL = r'''(?x)
165 (?:
166 limelight:media:|
167 https?://
168 (?:
169 link\.videoplatform\.limelight\.com/media/|
170 assets\.delvenetworks\.com/player/loader\.swf
171 )
172 \?.*?\bmediaId=
173 )
174 (?P<id>[a-z0-9]{32})
175 '''
9c544e25 176 _TESTS = [{
ef5acfe3 177 'url': 'http://link.videoplatform.limelight.com/media/?mediaId=3ffd040b522b4485b6d84effc750cd86',
ef5acfe3 178 'info_dict': {
179 'id': '3ffd040b522b4485b6d84effc750cd86',
e382b953 180 'ext': 'mp4',
ef5acfe3 181 'title': 'HaP and the HB Prince Trailer',
9c544e25 182 'description': 'md5:8005b944181778e313d95c1237ddb640',
ec85ded8 183 'thumbnail': r're:^https?://.*\.jpeg$',
d7fc5631 184 'duration': 144.23,
ef5acfe3 185 'timestamp': 1244136834,
d7fc5631
S
186 'upload_date': '20090604',
187 },
188 'params': {
e382b953 189 # m3u8 download
d7fc5631
S
190 'skip_download': True,
191 },
9c544e25
S
192 }, {
193 # video with subtitles
194 'url': 'limelight:media:a3e00274d4564ec4a9b29b9466432335',
42b7a5af 195 'md5': '2fa3bad9ac321e23860ca23bc2c69e3d',
9c544e25
S
196 'info_dict': {
197 'id': 'a3e00274d4564ec4a9b29b9466432335',
42b7a5af 198 'ext': 'mp4',
9c544e25 199 'title': '3Play Media Overview Video',
ec85ded8 200 'thumbnail': r're:^https?://.*\.jpeg$',
9c544e25
S
201 'duration': 78.101,
202 'timestamp': 1338929955,
203 'upload_date': '20120605',
204 'subtitles': 'mincount:9',
205 },
79027c0e
S
206 }, {
207 'url': 'https://assets.delvenetworks.com/player/loader.swf?mediaId=8018a574f08d416e95ceaccae4ba0452',
208 'only_matching': True,
9c544e25 209 }]
d7fc5631
S
210 _PLAYLIST_SERVICE_PATH = 'media'
211 _API_PATH = 'media'
ef5acfe3 212
213 def _real_extract(self, url):
454e5cdb 214 url, smuggled_data = unsmuggle_url(url, {})
ef5acfe3 215 video_id = self._match_id(url)
216
d7fc5631 217 pc, mobile, metadata = self._extract(
454e5cdb
RA
218 video_id, 'getPlaylistByMediaId',
219 'getMobilePlaylistByMediaId', 'properties',
220 smuggled_data.get('source_url'))
ef5acfe3 221
d7fc5631
S
222 return self._extract_info(
223 pc['playlistItems'][0].get('streams', []),
224 mobile['mediaList'][0].get('mobileUrls', []) if mobile else [],
225 metadata)
ef5acfe3 226
227
d7fc5631 228class LimelightChannelIE(LimelightBaseIE):
ef5acfe3 229 IE_NAME = 'limelight:channel'
79027c0e
S
230 _VALID_URL = r'''(?x)
231 (?:
232 limelight:channel:|
233 https?://
234 (?:
235 link\.videoplatform\.limelight\.com/media/|
236 assets\.delvenetworks\.com/player/loader\.swf
237 )
238 \?.*?\bchannelId=
239 )
240 (?P<id>[a-z0-9]{32})
241 '''
242 _TESTS = [{
ef5acfe3 243 'url': 'http://link.videoplatform.limelight.com/media/?channelId=ab6a524c379342f9b23642917020c082',
244 'info_dict': {
245 'id': 'ab6a524c379342f9b23642917020c082',
246 'title': 'Javascript Sample Code',
247 },
248 'playlist_mincount': 3,
79027c0e
S
249 }, {
250 'url': 'http://assets.delvenetworks.com/player/loader.swf?channelId=ab6a524c379342f9b23642917020c082',
251 'only_matching': True,
252 }]
d7fc5631
S
253 _PLAYLIST_SERVICE_PATH = 'channel'
254 _API_PATH = 'channels'
ef5acfe3 255
256 def _real_extract(self, url):
454e5cdb 257 url, smuggled_data = unsmuggle_url(url, {})
ef5acfe3 258 channel_id = self._match_id(url)
259
d7fc5631
S
260 pc, mobile, medias = self._extract(
261 channel_id, 'getPlaylistByChannelId',
454e5cdb
RA
262 'getMobilePlaylistWithNItemsByChannelId?begin=0&count=-1',
263 'media', smuggled_data.get('source_url'))
ef5acfe3 264
d7fc5631
S
265 entries = [
266 self._extract_info(
267 pc['playlistItems'][i].get('streams', []),
268 mobile['mediaList'][i].get('mobileUrls', []) if mobile else [],
269 medias['media_list'][i])
270 for i in range(len(medias['media_list']))]
ef5acfe3 271
d7fc5631 272 return self.playlist_result(entries, channel_id, pc['title'])
ef5acfe3 273
274
d7fc5631 275class LimelightChannelListIE(LimelightBaseIE):
ef5acfe3 276 IE_NAME = 'limelight:channel_list'
79027c0e
S
277 _VALID_URL = r'''(?x)
278 (?:
279 limelight:channel_list:|
280 https?://
281 (?:
282 link\.videoplatform\.limelight\.com/media/|
283 assets\.delvenetworks\.com/player/loader\.swf
284 )
285 \?.*?\bchannelListId=
286 )
287 (?P<id>[a-z0-9]{32})
288 '''
289 _TESTS = [{
ef5acfe3 290 'url': 'http://link.videoplatform.limelight.com/media/?channelListId=301b117890c4465c8179ede21fd92e2b',
291 'info_dict': {
292 'id': '301b117890c4465c8179ede21fd92e2b',
293 'title': 'Website - Hero Player',
294 },
295 'playlist_mincount': 2,
79027c0e
S
296 }, {
297 'url': 'https://assets.delvenetworks.com/player/loader.swf?channelListId=301b117890c4465c8179ede21fd92e2b',
298 'only_matching': True,
299 }]
d7fc5631 300 _PLAYLIST_SERVICE_PATH = 'channel_list'
ef5acfe3 301
302 def _real_extract(self, url):
303 channel_list_id = self._match_id(url)
304
d7fc5631 305 channel_list = self._call_playlist_service(channel_list_id, 'getMobileChannelListById')
ef5acfe3 306
d7fc5631
S
307 entries = [
308 self.url_result('limelight:channel:%s' % channel['id'], 'LimelightChannel')
309 for channel in channel_list['channelList']]
ef5acfe3 310
d7fc5631 311 return self.playlist_result(entries, channel_list_id, channel_list['title'])