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.

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