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.

230 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(
  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'(?:limelight:media:|http://link\.videoplatform\.limelight\.com/media/\??\bmediaId=)(?P<id>[a-z0-9]{32})'
  111. _TESTS = [{
  112. 'url': 'http://link.videoplatform.limelight.com/media/?mediaId=3ffd040b522b4485b6d84effc750cd86',
  113. 'info_dict': {
  114. 'id': '3ffd040b522b4485b6d84effc750cd86',
  115. 'ext': 'flv',
  116. 'title': 'HaP and the HB Prince Trailer',
  117. 'description': 'md5:8005b944181778e313d95c1237ddb640',
  118. 'thumbnail': 're:^https?://.*\.jpeg$',
  119. 'duration': 144.23,
  120. 'timestamp': 1244136834,
  121. 'upload_date': '20090604',
  122. },
  123. 'params': {
  124. # rtmp download
  125. 'skip_download': True,
  126. },
  127. }, {
  128. # video with subtitles
  129. 'url': 'limelight:media:a3e00274d4564ec4a9b29b9466432335',
  130. 'info_dict': {
  131. 'id': 'a3e00274d4564ec4a9b29b9466432335',
  132. 'ext': 'flv',
  133. 'title': '3Play Media Overview Video',
  134. 'description': '',
  135. 'thumbnail': 're:^https?://.*\.jpeg$',
  136. 'duration': 78.101,
  137. 'timestamp': 1338929955,
  138. 'upload_date': '20120605',
  139. 'subtitles': 'mincount:9',
  140. },
  141. 'params': {
  142. # rtmp download
  143. 'skip_download': True,
  144. },
  145. }]
  146. _PLAYLIST_SERVICE_PATH = 'media'
  147. _API_PATH = 'media'
  148. def _real_extract(self, url):
  149. video_id = self._match_id(url)
  150. pc, mobile, metadata = self._extract(
  151. video_id, 'getPlaylistByMediaId', 'getMobilePlaylistByMediaId', 'properties')
  152. return self._extract_info(
  153. pc['playlistItems'][0].get('streams', []),
  154. mobile['mediaList'][0].get('mobileUrls', []) if mobile else [],
  155. metadata)
  156. class LimelightChannelIE(LimelightBaseIE):
  157. IE_NAME = 'limelight:channel'
  158. _VALID_URL = r'(?:limelight:channel:|http://link\.videoplatform\.limelight\.com/media/\??\bchannelId=)(?P<id>[a-z0-9]{32})'
  159. _TEST = {
  160. 'url': 'http://link.videoplatform.limelight.com/media/?channelId=ab6a524c379342f9b23642917020c082',
  161. 'info_dict': {
  162. 'id': 'ab6a524c379342f9b23642917020c082',
  163. 'title': 'Javascript Sample Code',
  164. },
  165. 'playlist_mincount': 3,
  166. }
  167. _PLAYLIST_SERVICE_PATH = 'channel'
  168. _API_PATH = 'channels'
  169. def _real_extract(self, url):
  170. channel_id = self._match_id(url)
  171. pc, mobile, medias = self._extract(
  172. channel_id, 'getPlaylistByChannelId',
  173. 'getMobilePlaylistWithNItemsByChannelId?begin=0&count=-1', 'media')
  174. entries = [
  175. self._extract_info(
  176. pc['playlistItems'][i].get('streams', []),
  177. mobile['mediaList'][i].get('mobileUrls', []) if mobile else [],
  178. medias['media_list'][i])
  179. for i in range(len(medias['media_list']))]
  180. return self.playlist_result(entries, channel_id, pc['title'])
  181. class LimelightChannelListIE(LimelightBaseIE):
  182. IE_NAME = 'limelight:channel_list'
  183. _VALID_URL = r'(?:limelight:channel_list:|http://link\.videoplatform\.limelight\.com/media/\?.*?\bchannelListId=)(?P<id>[a-z0-9]{32})'
  184. _TEST = {
  185. 'url': 'http://link.videoplatform.limelight.com/media/?channelListId=301b117890c4465c8179ede21fd92e2b',
  186. 'info_dict': {
  187. 'id': '301b117890c4465c8179ede21fd92e2b',
  188. 'title': 'Website - Hero Player',
  189. },
  190. 'playlist_mincount': 2,
  191. }
  192. _PLAYLIST_SERVICE_PATH = 'channel_list'
  193. def _real_extract(self, url):
  194. channel_list_id = self._match_id(url)
  195. channel_list = self._call_playlist_service(channel_list_id, 'getMobileChannelListById')
  196. entries = [
  197. self.url_result('limelight:channel:%s' % channel['id'], 'LimelightChannel')
  198. for channel in channel_list['channelList']]
  199. return self.playlist_result(entries, channel_list_id, channel_list['title'])