limelight.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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[lang] = [{
  93. 'url': subtitles_url,
  94. }]
  95. return {
  96. 'id': video_id,
  97. 'title': title,
  98. 'description': description,
  99. 'formats': formats,
  100. 'timestamp': timestamp,
  101. 'duration': duration,
  102. 'filesize': filesize,
  103. 'categories': categories,
  104. 'tags': tags,
  105. 'thumbnails': thumbnails,
  106. 'subtitles': subtitles,
  107. }
  108. class LimelightMediaIE(LimelightBaseIE):
  109. IE_NAME = 'limelight'
  110. _VALID_URL = r'''(?x)
  111. (?:
  112. limelight:media:|
  113. https?://
  114. (?:
  115. link\.videoplatform\.limelight\.com/media/|
  116. assets\.delvenetworks\.com/player/loader\.swf
  117. )
  118. \?.*?\bmediaId=
  119. )
  120. (?P<id>[a-z0-9]{32})
  121. '''
  122. _TESTS = [{
  123. 'url': 'http://link.videoplatform.limelight.com/media/?mediaId=3ffd040b522b4485b6d84effc750cd86',
  124. 'info_dict': {
  125. 'id': '3ffd040b522b4485b6d84effc750cd86',
  126. 'ext': 'flv',
  127. 'title': 'HaP and the HB Prince Trailer',
  128. 'description': 'md5:8005b944181778e313d95c1237ddb640',
  129. 'thumbnail': 're:^https?://.*\.jpeg$',
  130. 'duration': 144.23,
  131. 'timestamp': 1244136834,
  132. 'upload_date': '20090604',
  133. },
  134. 'params': {
  135. # rtmp download
  136. 'skip_download': True,
  137. },
  138. }, {
  139. # video with subtitles
  140. 'url': 'limelight:media:a3e00274d4564ec4a9b29b9466432335',
  141. 'info_dict': {
  142. 'id': 'a3e00274d4564ec4a9b29b9466432335',
  143. 'ext': 'flv',
  144. 'title': '3Play Media Overview Video',
  145. 'description': '',
  146. 'thumbnail': 're:^https?://.*\.jpeg$',
  147. 'duration': 78.101,
  148. 'timestamp': 1338929955,
  149. 'upload_date': '20120605',
  150. 'subtitles': 'mincount:9',
  151. },
  152. 'params': {
  153. # rtmp download
  154. 'skip_download': True,
  155. },
  156. }, {
  157. 'url': 'https://assets.delvenetworks.com/player/loader.swf?mediaId=8018a574f08d416e95ceaccae4ba0452',
  158. 'only_matching': True,
  159. }]
  160. _PLAYLIST_SERVICE_PATH = 'media'
  161. _API_PATH = 'media'
  162. def _real_extract(self, url):
  163. video_id = self._match_id(url)
  164. pc, mobile, metadata = self._extract(
  165. video_id, 'getPlaylistByMediaId', 'getMobilePlaylistByMediaId', 'properties')
  166. return self._extract_info(
  167. pc['playlistItems'][0].get('streams', []),
  168. mobile['mediaList'][0].get('mobileUrls', []) if mobile else [],
  169. metadata)
  170. class LimelightChannelIE(LimelightBaseIE):
  171. IE_NAME = 'limelight:channel'
  172. _VALID_URL = r'''(?x)
  173. (?:
  174. limelight:channel:|
  175. https?://
  176. (?:
  177. link\.videoplatform\.limelight\.com/media/|
  178. assets\.delvenetworks\.com/player/loader\.swf
  179. )
  180. \?.*?\bchannelId=
  181. )
  182. (?P<id>[a-z0-9]{32})
  183. '''
  184. _TESTS = [{
  185. 'url': 'http://link.videoplatform.limelight.com/media/?channelId=ab6a524c379342f9b23642917020c082',
  186. 'info_dict': {
  187. 'id': 'ab6a524c379342f9b23642917020c082',
  188. 'title': 'Javascript Sample Code',
  189. },
  190. 'playlist_mincount': 3,
  191. }, {
  192. 'url': 'http://assets.delvenetworks.com/player/loader.swf?channelId=ab6a524c379342f9b23642917020c082',
  193. 'only_matching': True,
  194. }]
  195. _PLAYLIST_SERVICE_PATH = 'channel'
  196. _API_PATH = 'channels'
  197. def _real_extract(self, url):
  198. channel_id = self._match_id(url)
  199. pc, mobile, medias = self._extract(
  200. channel_id, 'getPlaylistByChannelId',
  201. 'getMobilePlaylistWithNItemsByChannelId?begin=0&count=-1', 'media')
  202. entries = [
  203. self._extract_info(
  204. pc['playlistItems'][i].get('streams', []),
  205. mobile['mediaList'][i].get('mobileUrls', []) if mobile else [],
  206. medias['media_list'][i])
  207. for i in range(len(medias['media_list']))]
  208. return self.playlist_result(entries, channel_id, pc['title'])
  209. class LimelightChannelListIE(LimelightBaseIE):
  210. IE_NAME = 'limelight:channel_list'
  211. _VALID_URL = r'''(?x)
  212. (?:
  213. limelight:channel_list:|
  214. https?://
  215. (?:
  216. link\.videoplatform\.limelight\.com/media/|
  217. assets\.delvenetworks\.com/player/loader\.swf
  218. )
  219. \?.*?\bchannelListId=
  220. )
  221. (?P<id>[a-z0-9]{32})
  222. '''
  223. _TESTS = [{
  224. 'url': 'http://link.videoplatform.limelight.com/media/?channelListId=301b117890c4465c8179ede21fd92e2b',
  225. 'info_dict': {
  226. 'id': '301b117890c4465c8179ede21fd92e2b',
  227. 'title': 'Website - Hero Player',
  228. },
  229. 'playlist_mincount': 2,
  230. }, {
  231. 'url': 'https://assets.delvenetworks.com/player/loader.swf?channelListId=301b117890c4465c8179ede21fd92e2b',
  232. 'only_matching': True,
  233. }]
  234. _PLAYLIST_SERVICE_PATH = 'channel_list'
  235. def _real_extract(self, url):
  236. channel_list_id = self._match_id(url)
  237. channel_list = self._call_playlist_service(channel_list_id, 'getMobileChannelListById')
  238. entries = [
  239. self.url_result('limelight:channel:%s' % channel['id'], 'LimelightChannel')
  240. for channel in channel_list['channelList']]
  241. return self.playlist_result(entries, channel_list_id, channel_list['title'])