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.

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