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.

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