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.

367 lines
15 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_HTTPError
  6. from ..utils import (
  7. determine_ext,
  8. float_or_none,
  9. int_or_none,
  10. smuggle_url,
  11. unsmuggle_url,
  12. ExtractorError,
  13. )
  14. class LimelightBaseIE(InfoExtractor):
  15. _PLAYLIST_SERVICE_URL = 'http://production-ps.lvp.llnw.net/r/PlaylistService/%s/%s/%s'
  16. _API_URL = 'http://api.video.limelight.com/rest/organizations/%s/%s/%s/%s.json'
  17. @classmethod
  18. def _extract_urls(cls, webpage, source_url):
  19. lm = {
  20. 'Media': 'media',
  21. 'Channel': 'channel',
  22. 'ChannelList': 'channel_list',
  23. }
  24. entries = []
  25. for kind, video_id in re.findall(
  26. r'LimelightPlayer\.doLoad(Media|Channel|ChannelList)\(["\'](?P<id>[a-z0-9]{32})',
  27. webpage):
  28. entries.append(cls.url_result(
  29. smuggle_url(
  30. 'limelight:%s:%s' % (lm[kind], video_id),
  31. {'source_url': source_url}),
  32. 'Limelight%s' % kind, video_id))
  33. for mobj in re.finditer(
  34. # As per [1] class attribute should be exactly equal to
  35. # LimelightEmbeddedPlayerFlash but numerous examples seen
  36. # that don't exactly match it (e.g. [2]).
  37. # 1. http://support.3playmedia.com/hc/en-us/articles/227732408-Limelight-Embedding-the-Captions-Plugin-with-the-Limelight-Player-on-Your-Webpage
  38. # 2. http://www.sedona.com/FacilitatorTraining2017
  39. r'''(?sx)
  40. <object[^>]+class=(["\'])(?:(?!\1).)*\bLimelightEmbeddedPlayerFlash\b(?:(?!\1).)*\1[^>]*>.*?
  41. <param[^>]+
  42. name=(["\'])flashVars\2[^>]+
  43. value=(["\'])(?:(?!\3).)*(?P<kind>media|channel(?:List)?)Id=(?P<id>[a-z0-9]{32})
  44. ''', webpage):
  45. kind, video_id = mobj.group('kind'), mobj.group('id')
  46. entries.append(cls.url_result(
  47. smuggle_url(
  48. 'limelight:%s:%s' % (kind, video_id),
  49. {'source_url': source_url}),
  50. 'Limelight%s' % kind.capitalize(), video_id))
  51. return entries
  52. def _call_playlist_service(self, item_id, method, fatal=True, referer=None):
  53. headers = {}
  54. if referer:
  55. headers['Referer'] = referer
  56. try:
  57. return self._download_json(
  58. self._PLAYLIST_SERVICE_URL % (self._PLAYLIST_SERVICE_PATH, item_id, method),
  59. item_id, 'Downloading PlaylistService %s JSON' % method, fatal=fatal, headers=headers)
  60. except ExtractorError as e:
  61. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  62. error = self._parse_json(e.cause.read().decode(), item_id)['detail']['contentAccessPermission']
  63. if error == 'CountryDisabled':
  64. self.raise_geo_restricted()
  65. raise ExtractorError(error, expected=True)
  66. raise
  67. def _call_api(self, organization_id, item_id, method):
  68. return self._download_json(
  69. self._API_URL % (organization_id, self._API_PATH, item_id, method),
  70. item_id, 'Downloading API %s JSON' % method)
  71. def _extract(self, item_id, pc_method, mobile_method, meta_method, referer=None):
  72. pc = self._call_playlist_service(item_id, pc_method, referer=referer)
  73. metadata = self._call_api(pc['orgId'], item_id, meta_method)
  74. mobile = self._call_playlist_service(item_id, mobile_method, fatal=False, referer=referer)
  75. return pc, mobile, metadata
  76. def _extract_info(self, streams, mobile_urls, properties):
  77. video_id = properties['media_id']
  78. formats = []
  79. urls = []
  80. for stream in streams:
  81. stream_url = stream.get('url')
  82. if not stream_url or stream.get('drmProtected') or stream_url in urls:
  83. continue
  84. urls.append(stream_url)
  85. ext = determine_ext(stream_url)
  86. if ext == 'f4m':
  87. formats.extend(self._extract_f4m_formats(
  88. stream_url, video_id, f4m_id='hds', fatal=False))
  89. else:
  90. fmt = {
  91. 'url': stream_url,
  92. 'abr': float_or_none(stream.get('audioBitRate')),
  93. 'fps': float_or_none(stream.get('videoFrameRate')),
  94. 'ext': ext,
  95. }
  96. width = int_or_none(stream.get('videoWidthInPixels'))
  97. height = int_or_none(stream.get('videoHeightInPixels'))
  98. vbr = float_or_none(stream.get('videoBitRate'))
  99. if width or height or vbr:
  100. fmt.update({
  101. 'width': width,
  102. 'height': height,
  103. 'vbr': vbr,
  104. })
  105. else:
  106. fmt['vcodec'] = 'none'
  107. rtmp = re.search(r'^(?P<url>rtmpe?://(?P<host>[^/]+)/(?P<app>.+))/(?P<playpath>mp[34]:.+)$', stream_url)
  108. if rtmp:
  109. format_id = 'rtmp'
  110. if stream.get('videoBitRate'):
  111. format_id += '-%d' % int_or_none(stream['videoBitRate'])
  112. http_format_id = format_id.replace('rtmp', 'http')
  113. CDN_HOSTS = (
  114. ('delvenetworks.com', 'cpl.delvenetworks.com'),
  115. ('video.llnw.net', 's2.content.video.llnw.net'),
  116. )
  117. for cdn_host, http_host in CDN_HOSTS:
  118. if cdn_host not in rtmp.group('host').lower():
  119. continue
  120. http_url = 'http://%s/%s' % (http_host, rtmp.group('playpath')[4:])
  121. urls.append(http_url)
  122. if self._is_valid_url(http_url, video_id, http_format_id):
  123. http_fmt = fmt.copy()
  124. http_fmt.update({
  125. 'url': http_url,
  126. 'format_id': http_format_id,
  127. })
  128. formats.append(http_fmt)
  129. break
  130. fmt.update({
  131. 'url': rtmp.group('url'),
  132. 'play_path': rtmp.group('playpath'),
  133. 'app': rtmp.group('app'),
  134. 'ext': 'flv',
  135. 'format_id': format_id,
  136. })
  137. formats.append(fmt)
  138. for mobile_url in mobile_urls:
  139. media_url = mobile_url.get('mobileUrl')
  140. format_id = mobile_url.get('targetMediaPlatform')
  141. if not media_url or format_id in ('Widevine', 'SmoothStreaming') or media_url in urls:
  142. continue
  143. urls.append(media_url)
  144. ext = determine_ext(media_url)
  145. if ext == 'm3u8':
  146. formats.extend(self._extract_m3u8_formats(
  147. media_url, video_id, 'mp4', 'm3u8_native',
  148. m3u8_id=format_id, fatal=False))
  149. elif ext == 'f4m':
  150. formats.extend(self._extract_f4m_formats(
  151. stream_url, video_id, f4m_id=format_id, fatal=False))
  152. else:
  153. formats.append({
  154. 'url': media_url,
  155. 'format_id': format_id,
  156. 'preference': -1,
  157. 'ext': ext,
  158. })
  159. self._sort_formats(formats)
  160. title = properties['title']
  161. description = properties.get('description')
  162. timestamp = int_or_none(properties.get('publish_date') or properties.get('create_date'))
  163. duration = float_or_none(properties.get('duration_in_milliseconds'), 1000)
  164. filesize = int_or_none(properties.get('total_storage_in_bytes'))
  165. categories = [properties.get('category')]
  166. tags = properties.get('tags', [])
  167. thumbnails = [{
  168. 'url': thumbnail['url'],
  169. 'width': int_or_none(thumbnail.get('width')),
  170. 'height': int_or_none(thumbnail.get('height')),
  171. } for thumbnail in properties.get('thumbnails', []) if thumbnail.get('url')]
  172. subtitles = {}
  173. for caption in properties.get('captions', []):
  174. lang = caption.get('language_code')
  175. subtitles_url = caption.get('url')
  176. if lang and subtitles_url:
  177. subtitles.setdefault(lang, []).append({
  178. 'url': subtitles_url,
  179. })
  180. closed_captions_url = properties.get('closed_captions_url')
  181. if closed_captions_url:
  182. subtitles.setdefault('en', []).append({
  183. 'url': closed_captions_url,
  184. 'ext': 'ttml',
  185. })
  186. return {
  187. 'id': video_id,
  188. 'title': title,
  189. 'description': description,
  190. 'formats': formats,
  191. 'timestamp': timestamp,
  192. 'duration': duration,
  193. 'filesize': filesize,
  194. 'categories': categories,
  195. 'tags': tags,
  196. 'thumbnails': thumbnails,
  197. 'subtitles': subtitles,
  198. }
  199. class LimelightMediaIE(LimelightBaseIE):
  200. IE_NAME = 'limelight'
  201. _VALID_URL = r'''(?x)
  202. (?:
  203. limelight:media:|
  204. https?://
  205. (?:
  206. link\.videoplatform\.limelight\.com/media/|
  207. assets\.delvenetworks\.com/player/loader\.swf
  208. )
  209. \?.*?\bmediaId=
  210. )
  211. (?P<id>[a-z0-9]{32})
  212. '''
  213. _TESTS = [{
  214. 'url': 'http://link.videoplatform.limelight.com/media/?mediaId=3ffd040b522b4485b6d84effc750cd86',
  215. 'info_dict': {
  216. 'id': '3ffd040b522b4485b6d84effc750cd86',
  217. 'ext': 'mp4',
  218. 'title': 'HaP and the HB Prince Trailer',
  219. 'description': 'md5:8005b944181778e313d95c1237ddb640',
  220. 'thumbnail': r're:^https?://.*\.jpeg$',
  221. 'duration': 144.23,
  222. 'timestamp': 1244136834,
  223. 'upload_date': '20090604',
  224. },
  225. 'params': {
  226. # m3u8 download
  227. 'skip_download': True,
  228. },
  229. }, {
  230. # video with subtitles
  231. 'url': 'limelight:media:a3e00274d4564ec4a9b29b9466432335',
  232. 'md5': '2fa3bad9ac321e23860ca23bc2c69e3d',
  233. 'info_dict': {
  234. 'id': 'a3e00274d4564ec4a9b29b9466432335',
  235. 'ext': 'mp4',
  236. 'title': '3Play Media Overview Video',
  237. 'thumbnail': r're:^https?://.*\.jpeg$',
  238. 'duration': 78.101,
  239. 'timestamp': 1338929955,
  240. 'upload_date': '20120605',
  241. 'subtitles': 'mincount:9',
  242. },
  243. }, {
  244. 'url': 'https://assets.delvenetworks.com/player/loader.swf?mediaId=8018a574f08d416e95ceaccae4ba0452',
  245. 'only_matching': True,
  246. }]
  247. _PLAYLIST_SERVICE_PATH = 'media'
  248. _API_PATH = 'media'
  249. def _real_extract(self, url):
  250. url, smuggled_data = unsmuggle_url(url, {})
  251. video_id = self._match_id(url)
  252. self._initialize_geo_bypass(smuggled_data.get('geo_countries'))
  253. pc, mobile, metadata = self._extract(
  254. video_id, 'getPlaylistByMediaId',
  255. 'getMobilePlaylistByMediaId', 'properties',
  256. smuggled_data.get('source_url'))
  257. return self._extract_info(
  258. pc['playlistItems'][0].get('streams', []),
  259. mobile['mediaList'][0].get('mobileUrls', []) if mobile else [],
  260. metadata)
  261. class LimelightChannelIE(LimelightBaseIE):
  262. IE_NAME = 'limelight:channel'
  263. _VALID_URL = r'''(?x)
  264. (?:
  265. limelight:channel:|
  266. https?://
  267. (?:
  268. link\.videoplatform\.limelight\.com/media/|
  269. assets\.delvenetworks\.com/player/loader\.swf
  270. )
  271. \?.*?\bchannelId=
  272. )
  273. (?P<id>[a-z0-9]{32})
  274. '''
  275. _TESTS = [{
  276. 'url': 'http://link.videoplatform.limelight.com/media/?channelId=ab6a524c379342f9b23642917020c082',
  277. 'info_dict': {
  278. 'id': 'ab6a524c379342f9b23642917020c082',
  279. 'title': 'Javascript Sample Code',
  280. },
  281. 'playlist_mincount': 3,
  282. }, {
  283. 'url': 'http://assets.delvenetworks.com/player/loader.swf?channelId=ab6a524c379342f9b23642917020c082',
  284. 'only_matching': True,
  285. }]
  286. _PLAYLIST_SERVICE_PATH = 'channel'
  287. _API_PATH = 'channels'
  288. def _real_extract(self, url):
  289. url, smuggled_data = unsmuggle_url(url, {})
  290. channel_id = self._match_id(url)
  291. pc, mobile, medias = self._extract(
  292. channel_id, 'getPlaylistByChannelId',
  293. 'getMobilePlaylistWithNItemsByChannelId?begin=0&count=-1',
  294. 'media', smuggled_data.get('source_url'))
  295. entries = [
  296. self._extract_info(
  297. pc['playlistItems'][i].get('streams', []),
  298. mobile['mediaList'][i].get('mobileUrls', []) if mobile else [],
  299. medias['media_list'][i])
  300. for i in range(len(medias['media_list']))]
  301. return self.playlist_result(entries, channel_id, pc['title'])
  302. class LimelightChannelListIE(LimelightBaseIE):
  303. IE_NAME = 'limelight:channel_list'
  304. _VALID_URL = r'''(?x)
  305. (?:
  306. limelight:channel_list:|
  307. https?://
  308. (?:
  309. link\.videoplatform\.limelight\.com/media/|
  310. assets\.delvenetworks\.com/player/loader\.swf
  311. )
  312. \?.*?\bchannelListId=
  313. )
  314. (?P<id>[a-z0-9]{32})
  315. '''
  316. _TESTS = [{
  317. 'url': 'http://link.videoplatform.limelight.com/media/?channelListId=301b117890c4465c8179ede21fd92e2b',
  318. 'info_dict': {
  319. 'id': '301b117890c4465c8179ede21fd92e2b',
  320. 'title': 'Website - Hero Player',
  321. },
  322. 'playlist_mincount': 2,
  323. }, {
  324. 'url': 'https://assets.delvenetworks.com/player/loader.swf?channelListId=301b117890c4465c8179ede21fd92e2b',
  325. 'only_matching': True,
  326. }]
  327. _PLAYLIST_SERVICE_PATH = 'channel_list'
  328. def _real_extract(self, url):
  329. channel_list_id = self._match_id(url)
  330. channel_list = self._call_playlist_service(channel_list_id, 'getMobileChannelListById')
  331. entries = [
  332. self.url_result('limelight:channel:%s' % channel['id'], 'LimelightChannel')
  333. for channel in channel_list['channelList']]
  334. return self.playlist_result(entries, channel_list_id, channel_list['title'])