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.

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