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