You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
11 KiB

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