]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/limelight.py
Add support for https for all extractors as preventive and future-proof measure
[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,
ef5acfe3 11)
12
13
d7fc5631
S
14class 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'
ef5acfe3 17
d7fc5631
S
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)
ef5acfe3 22
d7fc5631
S
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)
ef5acfe3 27
d7fc5631
S
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):
ef5acfe3 35 video_id = properties['media_id']
36 formats = []
37
ef5acfe3 38 for stream in streams:
d7fc5631
S
39 stream_url = stream.get('url')
40 if not stream_url:
41 continue
42 if '.f4m' in stream_url:
8f1fddc8 43 formats.extend(self._extract_f4m_formats(
44 stream_url, video_id, fatal=False))
ef5acfe3 45 else:
46 fmt = {
d7fc5631
S
47 'url': stream_url,
48 'abr': float_or_none(stream.get('audioBitRate')),
49 'vbr': float_or_none(stream.get('videoBitRate')),
50 'fps': float_or_none(stream.get('videoFrameRate')),
51 'width': int_or_none(stream.get('videoWidthInPixels')),
52 'height': int_or_none(stream.get('videoHeightInPixels')),
53 'ext': determine_ext(stream_url)
ef5acfe3 54 }
d7fc5631 55 rtmp = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>.+))/(?P<playpath>mp4:.+)$', stream_url)
ef5acfe3 56 if rtmp:
d7fc5631
S
57 format_id = 'rtmp'
58 if stream.get('videoBitRate'):
59 format_id += '-%d' % int_or_none(stream['videoBitRate'])
ef5acfe3 60 fmt.update({
61 'url': rtmp.group('url'),
62 'play_path': rtmp.group('playpath'),
63 'app': rtmp.group('app'),
d7fc5631
S
64 'ext': 'flv',
65 'format_id': format_id,
ef5acfe3 66 })
67 formats.append(fmt)
68
d7fc5631
S
69 for mobile_url in mobile_urls:
70 media_url = mobile_url.get('mobileUrl')
71 if not media_url:
72 continue
73 format_id = mobile_url.get('targetMediaPlatform')
74 if determine_ext(media_url) == 'm3u8':
75 formats.extend(self._extract_m3u8_formats(
8f1fddc8 76 media_url, video_id, 'mp4', 'm3u8_native',
77 m3u8_id=format_id, fatal=False))
d7fc5631
S
78 else:
79 formats.append({
80 'url': media_url,
81 'format_id': format_id,
82 'preference': -1,
83 })
84
ef5acfe3 85 self._sort_formats(formats)
86
87 title = properties['title']
88 description = properties.get('description')
d7fc5631
S
89 timestamp = int_or_none(properties.get('publish_date') or properties.get('create_date'))
90 duration = float_or_none(properties.get('duration_in_milliseconds'), 1000)
91 filesize = int_or_none(properties.get('total_storage_in_bytes'))
ef5acfe3 92 categories = [properties.get('category')]
d7fc5631 93 tags = properties.get('tags', [])
ef5acfe3 94 thumbnails = [{
d7fc5631 95 'url': thumbnail['url'],
ef5acfe3 96 'width': int_or_none(thumbnail.get('width')),
97 'height': int_or_none(thumbnail.get('height')),
d7fc5631
S
98 } for thumbnail in properties.get('thumbnails', []) if thumbnail.get('url')]
99
100 subtitles = {}
101 for caption in properties.get('captions', {}):
102 lang = caption.get('language_code')
103 subtitles_url = caption.get('url')
104 if lang and subtitles_url:
105 subtitles[lang] = [{
106 'url': subtitles_url,
107 }]
ef5acfe3 108
109 return {
110 'id': video_id,
111 'title': title,
112 'description': description,
113 'formats': formats,
114 'timestamp': timestamp,
115 'duration': duration,
116 'filesize': filesize,
117 'categories': categories,
d7fc5631 118 'tags': tags,
ef5acfe3 119 'thumbnails': thumbnails,
120 'subtitles': subtitles,
121 }
122
123
d7fc5631 124class LimelightMediaIE(LimelightBaseIE):
ef5acfe3 125 IE_NAME = 'limelight'
5886b38d 126 _VALID_URL = r'(?:limelight:media:|https?://link\.videoplatform\.limelight\.com/media/\??\bmediaId=)(?P<id>[a-z0-9]{32})'
9c544e25 127 _TESTS = [{
ef5acfe3 128 'url': 'http://link.videoplatform.limelight.com/media/?mediaId=3ffd040b522b4485b6d84effc750cd86',
ef5acfe3 129 'info_dict': {
130 'id': '3ffd040b522b4485b6d84effc750cd86',
d7fc5631 131 'ext': 'flv',
ef5acfe3 132 'title': 'HaP and the HB Prince Trailer',
9c544e25 133 'description': 'md5:8005b944181778e313d95c1237ddb640',
ef5acfe3 134 'thumbnail': 're:^https?://.*\.jpeg$',
d7fc5631 135 'duration': 144.23,
ef5acfe3 136 'timestamp': 1244136834,
d7fc5631
S
137 'upload_date': '20090604',
138 },
139 'params': {
140 # rtmp download
141 'skip_download': True,
142 },
9c544e25
S
143 }, {
144 # video with subtitles
145 'url': 'limelight:media:a3e00274d4564ec4a9b29b9466432335',
146 'info_dict': {
147 'id': 'a3e00274d4564ec4a9b29b9466432335',
148 'ext': 'flv',
149 'title': '3Play Media Overview Video',
150 'description': '',
151 'thumbnail': 're:^https?://.*\.jpeg$',
152 'duration': 78.101,
153 'timestamp': 1338929955,
154 'upload_date': '20120605',
155 'subtitles': 'mincount:9',
156 },
157 'params': {
158 # rtmp download
159 'skip_download': True,
160 },
161 }]
d7fc5631
S
162 _PLAYLIST_SERVICE_PATH = 'media'
163 _API_PATH = 'media'
ef5acfe3 164
165 def _real_extract(self, url):
166 video_id = self._match_id(url)
167
d7fc5631
S
168 pc, mobile, metadata = self._extract(
169 video_id, 'getPlaylistByMediaId', 'getMobilePlaylistByMediaId', 'properties')
ef5acfe3 170
d7fc5631
S
171 return self._extract_info(
172 pc['playlistItems'][0].get('streams', []),
173 mobile['mediaList'][0].get('mobileUrls', []) if mobile else [],
174 metadata)
ef5acfe3 175
176
d7fc5631 177class LimelightChannelIE(LimelightBaseIE):
ef5acfe3 178 IE_NAME = 'limelight:channel'
5886b38d 179 _VALID_URL = r'(?:limelight:channel:|https?://link\.videoplatform\.limelight\.com/media/\??\bchannelId=)(?P<id>[a-z0-9]{32})'
ef5acfe3 180 _TEST = {
181 'url': 'http://link.videoplatform.limelight.com/media/?channelId=ab6a524c379342f9b23642917020c082',
182 'info_dict': {
183 'id': 'ab6a524c379342f9b23642917020c082',
184 'title': 'Javascript Sample Code',
185 },
186 'playlist_mincount': 3,
187 }
d7fc5631
S
188 _PLAYLIST_SERVICE_PATH = 'channel'
189 _API_PATH = 'channels'
ef5acfe3 190
191 def _real_extract(self, url):
192 channel_id = self._match_id(url)
193
d7fc5631
S
194 pc, mobile, medias = self._extract(
195 channel_id, 'getPlaylistByChannelId',
196 'getMobilePlaylistWithNItemsByChannelId?begin=0&count=-1', 'media')
ef5acfe3 197
d7fc5631
S
198 entries = [
199 self._extract_info(
200 pc['playlistItems'][i].get('streams', []),
201 mobile['mediaList'][i].get('mobileUrls', []) if mobile else [],
202 medias['media_list'][i])
203 for i in range(len(medias['media_list']))]
ef5acfe3 204
d7fc5631 205 return self.playlist_result(entries, channel_id, pc['title'])
ef5acfe3 206
207
d7fc5631 208class LimelightChannelListIE(LimelightBaseIE):
ef5acfe3 209 IE_NAME = 'limelight:channel_list'
5886b38d 210 _VALID_URL = r'(?:limelight:channel_list:|https?://link\.videoplatform\.limelight\.com/media/\?.*?\bchannelListId=)(?P<id>[a-z0-9]{32})'
ef5acfe3 211 _TEST = {
212 'url': 'http://link.videoplatform.limelight.com/media/?channelListId=301b117890c4465c8179ede21fd92e2b',
213 'info_dict': {
214 'id': '301b117890c4465c8179ede21fd92e2b',
215 'title': 'Website - Hero Player',
216 },
217 'playlist_mincount': 2,
218 }
d7fc5631 219 _PLAYLIST_SERVICE_PATH = 'channel_list'
ef5acfe3 220
221 def _real_extract(self, url):
222 channel_list_id = self._match_id(url)
223
d7fc5631 224 channel_list = self._call_playlist_service(channel_list_id, 'getMobileChannelListById')
ef5acfe3 225
d7fc5631
S
226 entries = [
227 self.url_result('limelight:channel:%s' % channel['id'], 'LimelightChannel')
228 for channel in channel_list['channelList']]
ef5acfe3 229
d7fc5631 230 return self.playlist_result(entries, channel_list_id, channel_list['title'])