summaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/limelight.py
blob: 905a0e85f78ad9fb36ea58daca2cc2f4ea55e7c3 (plain)
    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_url = 'http://cpl.delvenetworks.com/' + rtmp.group('playpath')[4:]
   63                     urls.append(http_url)
   64                     http_fmt = fmt.copy()
   65                     http_fmt.update({
   66                         'url': http_url,
   67                         'format_id': format_id.replace('rtmp', 'http'),
   68                     })
   69                     formats.append(http_fmt)
   70                     fmt.update({
   71                         'url': rtmp.group('url'),
   72                         'play_path': rtmp.group('playpath'),
   73                         'app': rtmp.group('app'),
   74                         'ext': 'flv',
   75                         'format_id': format_id,
   76                     })
   77                 formats.append(fmt)
   78 
   79         for mobile_url in mobile_urls:
   80             media_url = mobile_url.get('mobileUrl')
   81             format_id = mobile_url.get('targetMediaPlatform')
   82             if not media_url or format_id in ('Widevine', 'SmoothStreaming') or media_url in urls:
   83                 continue
   84             urls.append(media_url)
   85             ext = determine_ext(media_url)
   86             if ext == 'm3u8':
   87                 formats.extend(self._extract_m3u8_formats(
   88                     media_url, video_id, 'mp4', 'm3u8_native',
   89                     m3u8_id=format_id, fatal=False))
   90             elif ext == 'f4m':
   91                 formats.extend(self._extract_f4m_formats(
   92                     stream_url, video_id, f4m_id=format_id, fatal=False))
   93             else:
   94                 formats.append({
   95                     'url': media_url,
   96                     'format_id': format_id,
   97                     'preference': -1,
   98                     'ext': ext,
   99                 })
  100 
  101         self._sort_formats(formats)
  102 
  103         title = properties['title']
  104         description = properties.get('description')
  105         timestamp = int_or_none(properties.get('publish_date') or properties.get('create_date'))
  106         duration = float_or_none(properties.get('duration_in_milliseconds'), 1000)
  107         filesize = int_or_none(properties.get('total_storage_in_bytes'))
  108         categories = [properties.get('category')]
  109         tags = properties.get('tags', [])
  110         thumbnails = [{
  111             'url': thumbnail['url'],
  112             'width': int_or_none(thumbnail.get('width')),
  113             'height': int_or_none(thumbnail.get('height')),
  114         } for thumbnail in properties.get('thumbnails', []) if thumbnail.get('url')]
  115 
  116         subtitles = {}
  117         for caption in properties.get('captions', []):
  118             lang = caption.get('language_code')
  119             subtitles_url = caption.get('url')
  120             if lang and subtitles_url:
  121                 subtitles.setdefault(lang, []).append({
  122                     'url': subtitles_url,
  123                 })
  124         closed_captions_url = properties.get('closed_captions_url')
  125         if closed_captions_url:
  126             subtitles.setdefault('en', []).append({
  127                 'url': closed_captions_url,
  128                 'ext': 'ttml',
  129             })
  130 
  131         return {
  132             'id': video_id,
  133             'title': title,
  134             'description': description,
  135             'formats': formats,
  136             'timestamp': timestamp,
  137             'duration': duration,
  138             'filesize': filesize,
  139             'categories': categories,
  140             'tags': tags,
  141             'thumbnails': thumbnails,
  142             'subtitles': subtitles,
  143         }
  144 
  145 
  146 class LimelightMediaIE(LimelightBaseIE):
  147     IE_NAME = 'limelight'
  148     _VALID_URL = r'''(?x)
  149                         (?:
  150                             limelight:media:|
  151                             https?://
  152                                 (?:
  153                                     link\.videoplatform\.limelight\.com/media/|
  154                                     assets\.delvenetworks\.com/player/loader\.swf
  155                                 )
  156                                 \?.*?\bmediaId=
  157                         )
  158                         (?P<id>[a-z0-9]{32})
  159                     '''
  160     _TESTS = [{
  161         'url': 'http://link.videoplatform.limelight.com/media/?mediaId=3ffd040b522b4485b6d84effc750cd86',
  162         'info_dict': {
  163             'id': '3ffd040b522b4485b6d84effc750cd86',
  164             'ext': 'mp4',
  165             'title': 'HaP and the HB Prince Trailer',
  166             'description': 'md5:8005b944181778e313d95c1237ddb640',
  167             'thumbnail': r're:^https?://.*\.jpeg$',
  168             'duration': 144.23,
  169             'timestamp': 1244136834,
  170             'upload_date': '20090604',
  171         },
  172         'params': {
  173             # m3u8 download
  174             'skip_download': True,
  175         },
  176     }, {
  177         # video with subtitles
  178         'url': 'limelight:media:a3e00274d4564ec4a9b29b9466432335',
  179         'md5': '2fa3bad9ac321e23860ca23bc2c69e3d',
  180         'info_dict': {
  181             'id': 'a3e00274d4564ec4a9b29b9466432335',
  182             'ext': 'mp4',
  183             'title': '3Play Media Overview Video',
  184             'thumbnail': r're:^https?://.*\.jpeg$',
  185             'duration': 78.101,
  186             'timestamp': 1338929955,
  187             'upload_date': '20120605',
  188             'subtitles': 'mincount:9',
  189         },
  190     }, {
  191         'url': 'https://assets.delvenetworks.com/player/loader.swf?mediaId=8018a574f08d416e95ceaccae4ba0452',
  192         'only_matching': True,
  193     }]
  194     _PLAYLIST_SERVICE_PATH = 'media'
  195     _API_PATH = 'media'
  196 
  197     def _real_extract(self, url):
  198         video_id = self._match_id(url)
  199 
  200         pc, mobile, metadata = self._extract(
  201             video_id, 'getPlaylistByMediaId', 'getMobilePlaylistByMediaId', 'properties')
  202 
  203         return self._extract_info(
  204             pc['playlistItems'][0].get('streams', []),
  205             mobile['mediaList'][0].get('mobileUrls', []) if mobile else [],
  206             metadata)
  207 
  208 
  209 class LimelightChannelIE(LimelightBaseIE):
  210     IE_NAME = 'limelight:channel'
  211     _VALID_URL = r'''(?x)
  212                         (?:
  213                             limelight:channel:|
  214                             https?://
  215                                 (?:
  216                                     link\.videoplatform\.limelight\.com/media/|
  217                                     assets\.delvenetworks\.com/player/loader\.swf
  218                                 )
  219                                 \?.*?\bchannelId=
  220                         )
  221                         (?P<id>[a-z0-9]{32})
  222                     '''
  223     _TESTS = [{
  224         'url': 'http://link.videoplatform.limelight.com/media/?channelId=ab6a524c379342f9b23642917020c082',
  225         'info_dict': {
  226             'id': 'ab6a524c379342f9b23642917020c082',
  227             'title': 'Javascript Sample Code',
  228         },
  229         'playlist_mincount': 3,
  230     }, {
  231         'url': 'http://assets.delvenetworks.com/player/loader.swf?channelId=ab6a524c379342f9b23642917020c082',
  232         'only_matching': True,
  233     }]
  234     _PLAYLIST_SERVICE_PATH = 'channel'
  235     _API_PATH = 'channels'
  236 
  237     def _real_extract(self, url):
  238         channel_id = self._match_id(url)
  239 
  240         pc, mobile, medias = self._extract(
  241             channel_id, 'getPlaylistByChannelId',
  242             'getMobilePlaylistWithNItemsByChannelId?begin=0&count=-1', 'media')
  243 
  244         entries = [
  245             self._extract_info(
  246                 pc['playlistItems'][i].get('streams', []),
  247                 mobile['mediaList'][i].get('mobileUrls', []) if mobile else [],
  248                 medias['media_list'][i])
  249             for i in range(len(medias['media_list']))]
  250 
  251         return self.playlist_result(entries, channel_id, pc['title'])
  252 
  253 
  254 class LimelightChannelListIE(LimelightBaseIE):
  255     IE_NAME = 'limelight:channel_list'
  256     _VALID_URL = r'''(?x)
  257                         (?:
  258                             limelight:channel_list:|
  259                             https?://
  260                                 (?:
  261                                     link\.videoplatform\.limelight\.com/media/|
  262                                     assets\.delvenetworks\.com/player/loader\.swf
  263                                 )
  264                                 \?.*?\bchannelListId=
  265                         )
  266                         (?P<id>[a-z0-9]{32})
  267                     '''
  268     _TESTS = [{
  269         'url': 'http://link.videoplatform.limelight.com/media/?channelListId=301b117890c4465c8179ede21fd92e2b',
  270         'info_dict': {
  271             'id': '301b117890c4465c8179ede21fd92e2b',
  272             'title': 'Website - Hero Player',
  273         },
  274         'playlist_mincount': 2,
  275     }, {
  276         'url': 'https://assets.delvenetworks.com/player/loader.swf?channelListId=301b117890c4465c8179ede21fd92e2b',
  277         'only_matching': True,
  278     }]
  279     _PLAYLIST_SERVICE_PATH = 'channel_list'
  280 
  281     def _real_extract(self, url):
  282         channel_list_id = self._match_id(url)
  283 
  284         channel_list = self._call_playlist_service(channel_list_id, 'getMobileChannelListById')
  285 
  286         entries = [
  287             self.url_result('limelight:channel:%s' % channel['id'], 'LimelightChannel')
  288             for channel in channel_list['channelList']]
  289 
  290         return self.playlist_result(entries, channel_list_id, channel_list['title'])

Generated by cgit