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.

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