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