[limelight] Add new extractor
[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     int_or_none,
9     determine_ext,
10 )
11
12
13 class LimeLightBaseIE(InfoExtractor):
14
15     def get_playlist_service(self, id, method):
16         return self._download_json(self.PLAYLIST_SERVICE_URL % (id, method), id)
17
18     def get_api(self, orgId, id, method):
19         return self._download_json(self.API_URL % (orgId, id, method), id)
20
21     def process_data(self, mobileUrls, streams, properties):
22         video_id = properties['media_id']
23         formats = []
24
25         for mobileUrl in mobileUrls:
26             if '.m3u8' in mobileUrl['mobileUrl']:
27                 formats.extend(self._extract_m3u8_formats(mobileUrl['mobileUrl'], video_id))
28             else:
29                 formats.append({'url': mobileUrl['mobileUrl']})
30
31         for stream in streams:
32             if '.f4m' in stream['url']:
33                 formats.extend(self._extract_f4m_formats(stream['url'], video_id))
34             else:
35                 fmt = {
36                     'url': stream.get('url'),
37                     'abr': stream.get('audioBitRate'),
38                     'vbr': stream.get('videoBitRate'),
39                     'fps': stream.get('videoFrameRate'),
40                     'width': stream.get('videoWidthInPixels'),
41                     'height': stream.get('videoHeightInPixels'),
42                     'ext': determine_ext(stream.get('url'))
43                 }
44                 rtmp = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>.+))/(?P<playpath>mp4:.+)$', stream['url'])
45                 if rtmp:
46                     fmt.update({
47                         'url': rtmp.group('url'),
48                         'play_path': rtmp.group('playpath'),
49                         'app': rtmp.group('app'),
50                     })
51                 formats.append(fmt)
52
53         self._sort_formats(formats)
54
55         title = properties['title']
56         description = properties.get('description')
57         timestamp = properties.get('create_date')
58         duration = int_or_none(properties.get('duration_in_milliseconds'))
59         filesize = properties.get('total_storage_in_bytes')
60         categories = [properties.get('category')]
61         thumbnails = [{
62             'url': thumbnail.get('url'),
63             'width': int_or_none(thumbnail.get('width')),
64             'height': int_or_none(thumbnail.get('height')),
65         } for thumbnail in properties.get('thumbnails')]
66         subtitles = {caption.get('language_code'): [{'url': caption.get('url')}] for caption in properties.get('captions')}
67
68         return {
69             'id': video_id,
70             'title': title,
71             'description': description,
72             'formats': formats,
73             'timestamp': timestamp,
74             'duration': duration,
75             'filesize': filesize,
76             'categories': categories,
77             'thumbnails': thumbnails,
78             'subtitles': subtitles,
79         }
80
81
82 class LimeLightMediaIE(LimeLightBaseIE):
83     IE_NAME = 'limelight'
84     _VALID_URL = r'http://link\.videoplatform\.limelight\.com/media/?.*mediaId=(?P<id>[a-z0-9]{32})'
85     _TEST = {
86         'url': 'http://link.videoplatform.limelight.com/media/?mediaId=3ffd040b522b4485b6d84effc750cd86',
87         'md5': '3213605088be599705677ef785db6972',
88         'info_dict': {
89             'id': '3ffd040b522b4485b6d84effc750cd86',
90             'ext': 'mp4',
91             'title': 'HaP and the HB Prince Trailer',
92             'description': 'As Harry Potter begins his 6th year at Hogwarts School of Witchcraft and Wizardry, he discovers an old book marked mysteriously "This book is the property of the Half-Blood Prince" and begins to learn more about Lord Voldemort\'s dark past.',
93             'thumbnail': 're:^https?://.*\.jpeg$',
94             'duration': 144230,
95             'timestamp': 1244136834,
96             "upload_date": "20090604",
97         }
98     }
99     PLAYLIST_SERVICE_URL = 'http://production-ps.lvp.llnw.net/r/PlaylistService/media/%s/%s'
100     API_URL = 'http://api.video.limelight.com/rest/organizations/%s/media/%s/%s.json'
101
102     def _real_extract(self, url):
103         video_id = self._match_id(url)
104
105         mobile_json_data = self.get_playlist_service(video_id, 'getMobilePlaylistByMediaId')
106         pc_json_data = self.get_playlist_service(video_id, 'getPlaylistByMediaId')
107         properties = self.get_api(pc_json_data['orgId'], video_id, 'properties')
108
109         return self.process_data(mobile_json_data['mediaList'][0]['mobileUrls'], pc_json_data['playlistItems'][0]['streams'], properties)
110
111
112 class LimeLightChannelIE(LimeLightBaseIE):
113     IE_NAME = 'limelight:channel'
114     _VALID_URL = r'http://link\.videoplatform\.limelight\.com/media/?.*channelId=(?P<id>[a-z0-9]{32})'
115     _TEST = {
116         'url': 'http://link.videoplatform.limelight.com/media/?channelId=ab6a524c379342f9b23642917020c082',
117         'info_dict': {
118             'id': 'ab6a524c379342f9b23642917020c082',
119             'title': 'Javascript Sample Code',
120         },
121         'playlist_mincount': 3,
122     }
123     PLAYLIST_SERVICE_URL = 'http://production-ps.lvp.llnw.net/r/PlaylistService/channel/%s/%s'
124     API_URL = 'http://api.video.limelight.com/rest/organizations/%s/channels/%s/%s.json'
125
126     def _real_extract(self, url):
127         channel_id = self._match_id(url)
128
129         mobile_json_data = self.get_playlist_service(channel_id, 'getMobilePlaylistWithNItemsByChannelId?begin=0&count=-1')
130         pc_json_data = self.get_playlist_service(channel_id, 'getPlaylistByChannelId')
131         medias = self.get_api(pc_json_data['orgId'], channel_id, 'media')
132
133         entries = []
134         for i in range(len(medias['media_list'])):
135             entries.append(self.process_data(mobile_json_data['mediaList'][i]['mobileUrls'], pc_json_data['playlistItems'][i]['streams'], medias['media_list'][i]))
136
137         return {
138             'id': channel_id,
139             'title': pc_json_data['title'],
140             'entries': entries,
141             '_type': 'playlist',
142         }
143
144
145 class LimeLightChannelListIE(LimeLightBaseIE):
146     IE_NAME = 'limelight:channel_list'
147     _VALID_URL = r'http://link\.videoplatform\.limelight\.com/media/?.*channelListId=(?P<id>[a-z0-9]{32})'
148     _TEST = {
149         'url': 'http://link.videoplatform.limelight.com/media/?channelListId=301b117890c4465c8179ede21fd92e2b',
150         'info_dict': {
151             'id': '301b117890c4465c8179ede21fd92e2b',
152             'title': 'Website - Hero Player',
153         },
154         'playlist_mincount': 2,
155     }
156     PLAYLIST_SERVICE_URL = 'http://production-ps.lvp.llnw.net/r/PlaylistService/channel_list/%s/%s'
157
158     def _real_extract(self, url):
159         channel_list_id = self._match_id(url)
160
161         json_data = self.get_playlist_service(channel_list_id, 'getMobileChannelListById')
162
163         entries = []
164         for channel in json_data['channelList']:
165             entries.append({
166                 'url': 'http://link.videoplatform.limelight.com/media/?channelId=%s' % channel['id'],
167                 '_type': 'url',
168                 'ie_key': 'LimeLightChannel',
169             })
170
171         return {
172             'id': channel_list_id,
173             'title': json_data['title'],
174             'entries': entries,
175             '_type': 'playlist',
176         }