limelight.py 11 KB

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