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.

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