logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

limelight.py (14889B)


  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. @classmethod
  18. def _extract_urls(cls, webpage, source_url):
  19. lm = {
  20. 'Media': 'media',
  21. 'Channel': 'channel',
  22. 'ChannelList': 'channel_list',
  23. }
  24. def smuggle(url):
  25. return smuggle_url(url, {'source_url': source_url})
  26. entries = []
  27. for kind, video_id in re.findall(
  28. r'LimelightPlayer\.doLoad(Media|Channel|ChannelList)\(["\'](?P<id>[a-z0-9]{32})',
  29. webpage):
  30. entries.append(cls.url_result(
  31. smuggle('limelight:%s:%s' % (lm[kind], video_id)),
  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('limelight:%s:%s' % (kind, video_id)),
  48. 'Limelight%s' % kind.capitalize(), video_id))
  49. # http://support.3playmedia.com/hc/en-us/articles/115009517327-Limelight-Embedding-the-Audio-Description-Plugin-with-the-Limelight-Player-on-Your-Web-Page)
  50. for video_id in re.findall(
  51. r'(?s)LimelightPlayerUtil\.embed\s*\(\s*{.*?\bmediaId["\']\s*:\s*["\'](?P<id>[a-z0-9]{32})',
  52. webpage):
  53. entries.append(cls.url_result(
  54. smuggle('limelight:media:%s' % video_id),
  55. LimelightMediaIE.ie_key(), video_id))
  56. return entries
  57. def _call_playlist_service(self, item_id, method, fatal=True, referer=None):
  58. headers = {}
  59. if referer:
  60. headers['Referer'] = referer
  61. try:
  62. return self._download_json(
  63. self._PLAYLIST_SERVICE_URL % (self._PLAYLIST_SERVICE_PATH, item_id, method),
  64. item_id, 'Downloading PlaylistService %s JSON' % method,
  65. 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 _extract(self, item_id, pc_method, mobile_method, referer=None):
  74. pc = self._call_playlist_service(item_id, pc_method, referer=referer)
  75. mobile = self._call_playlist_service(
  76. item_id, mobile_method, fatal=False, referer=referer)
  77. return pc, mobile
  78. def _extract_info(self, pc, mobile, i, referer):
  79. get_item = lambda x, y: try_get(x, lambda x: x[y][i], dict) or {}
  80. pc_item = get_item(pc, 'playlistItems')
  81. mobile_item = get_item(mobile, 'mediaList')
  82. video_id = pc_item.get('mediaId') or mobile_item['mediaId']
  83. title = pc_item.get('title') or mobile_item['title']
  84. formats = []
  85. urls = []
  86. for stream in pc_item.get('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_item.get('mobileUrls', []):
  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. subtitles = {}
  167. for flag in mobile_item.get('flags'):
  168. if flag == 'ClosedCaptions':
  169. closed_captions = self._call_playlist_service(
  170. video_id, 'getClosedCaptionsDetailsByMediaId',
  171. False, referer) or []
  172. for cc in closed_captions:
  173. cc_url = cc.get('webvttFileUrl')
  174. if not cc_url:
  175. continue
  176. lang = cc.get('languageCode') or self._search_regex(r'/[a-z]{2}\.vtt', cc_url, 'lang', default='en')
  177. subtitles.setdefault(lang, []).append({
  178. 'url': cc_url,
  179. })
  180. break
  181. get_meta = lambda x: pc_item.get(x) or mobile_item.get(x)
  182. return {
  183. 'id': video_id,
  184. 'title': title,
  185. 'description': get_meta('description'),
  186. 'formats': formats,
  187. 'duration': float_or_none(get_meta('durationInMilliseconds'), 1000),
  188. 'thumbnail': get_meta('previewImageUrl') or get_meta('thumbnailImageUrl'),
  189. 'subtitles': subtitles,
  190. }
  191. class LimelightMediaIE(LimelightBaseIE):
  192. IE_NAME = 'limelight'
  193. _VALID_URL = r'''(?x)
  194. (?:
  195. limelight:media:|
  196. https?://
  197. (?:
  198. link\.videoplatform\.limelight\.com/media/|
  199. assets\.delvenetworks\.com/player/loader\.swf
  200. )
  201. \?.*?\bmediaId=
  202. )
  203. (?P<id>[a-z0-9]{32})
  204. '''
  205. _TESTS = [{
  206. 'url': 'http://link.videoplatform.limelight.com/media/?mediaId=3ffd040b522b4485b6d84effc750cd86',
  207. 'info_dict': {
  208. 'id': '3ffd040b522b4485b6d84effc750cd86',
  209. 'ext': 'mp4',
  210. 'title': 'HaP and the HB Prince Trailer',
  211. 'description': 'md5:8005b944181778e313d95c1237ddb640',
  212. 'thumbnail': r're:^https?://.*\.jpeg$',
  213. 'duration': 144.23,
  214. },
  215. 'params': {
  216. # m3u8 download
  217. 'skip_download': True,
  218. },
  219. }, {
  220. # video with subtitles
  221. 'url': 'limelight:media:a3e00274d4564ec4a9b29b9466432335',
  222. 'md5': '2fa3bad9ac321e23860ca23bc2c69e3d',
  223. 'info_dict': {
  224. 'id': 'a3e00274d4564ec4a9b29b9466432335',
  225. 'ext': 'mp4',
  226. 'title': '3Play Media Overview Video',
  227. 'thumbnail': r're:^https?://.*\.jpeg$',
  228. 'duration': 78.101,
  229. # TODO: extract all languages that were accessible via API
  230. # 'subtitles': 'mincount:9',
  231. 'subtitles': 'mincount:1',
  232. },
  233. }, {
  234. 'url': 'https://assets.delvenetworks.com/player/loader.swf?mediaId=8018a574f08d416e95ceaccae4ba0452',
  235. 'only_matching': True,
  236. }]
  237. _PLAYLIST_SERVICE_PATH = 'media'
  238. def _real_extract(self, url):
  239. url, smuggled_data = unsmuggle_url(url, {})
  240. video_id = self._match_id(url)
  241. source_url = smuggled_data.get('source_url')
  242. self._initialize_geo_bypass({
  243. 'countries': smuggled_data.get('geo_countries'),
  244. })
  245. pc, mobile = self._extract(
  246. video_id, 'getPlaylistByMediaId',
  247. 'getMobilePlaylistByMediaId', source_url)
  248. return self._extract_info(pc, mobile, 0, source_url)
  249. class LimelightChannelIE(LimelightBaseIE):
  250. IE_NAME = 'limelight:channel'
  251. _VALID_URL = r'''(?x)
  252. (?:
  253. limelight:channel:|
  254. https?://
  255. (?:
  256. link\.videoplatform\.limelight\.com/media/|
  257. assets\.delvenetworks\.com/player/loader\.swf
  258. )
  259. \?.*?\bchannelId=
  260. )
  261. (?P<id>[a-z0-9]{32})
  262. '''
  263. _TESTS = [{
  264. 'url': 'http://link.videoplatform.limelight.com/media/?channelId=ab6a524c379342f9b23642917020c082',
  265. 'info_dict': {
  266. 'id': 'ab6a524c379342f9b23642917020c082',
  267. 'title': 'Javascript Sample Code',
  268. 'description': 'Javascript Sample Code - http://www.delvenetworks.com/sample-code/playerCode-demo.html',
  269. },
  270. 'playlist_mincount': 3,
  271. }, {
  272. 'url': 'http://assets.delvenetworks.com/player/loader.swf?channelId=ab6a524c379342f9b23642917020c082',
  273. 'only_matching': True,
  274. }]
  275. _PLAYLIST_SERVICE_PATH = 'channel'
  276. def _real_extract(self, url):
  277. url, smuggled_data = unsmuggle_url(url, {})
  278. channel_id = self._match_id(url)
  279. source_url = smuggled_data.get('source_url')
  280. pc, mobile = self._extract(
  281. channel_id, 'getPlaylistByChannelId',
  282. 'getMobilePlaylistWithNItemsByChannelId?begin=0&count=-1',
  283. source_url)
  284. entries = [
  285. self._extract_info(pc, mobile, i, source_url)
  286. for i in range(len(pc['playlistItems']))]
  287. return self.playlist_result(
  288. entries, channel_id, pc.get('title'), mobile.get('description'))
  289. class LimelightChannelListIE(LimelightBaseIE):
  290. IE_NAME = 'limelight:channel_list'
  291. _VALID_URL = r'''(?x)
  292. (?:
  293. limelight:channel_list:|
  294. https?://
  295. (?:
  296. link\.videoplatform\.limelight\.com/media/|
  297. assets\.delvenetworks\.com/player/loader\.swf
  298. )
  299. \?.*?\bchannelListId=
  300. )
  301. (?P<id>[a-z0-9]{32})
  302. '''
  303. _TESTS = [{
  304. 'url': 'http://link.videoplatform.limelight.com/media/?channelListId=301b117890c4465c8179ede21fd92e2b',
  305. 'info_dict': {
  306. 'id': '301b117890c4465c8179ede21fd92e2b',
  307. 'title': 'Website - Hero Player',
  308. },
  309. 'playlist_mincount': 2,
  310. }, {
  311. 'url': 'https://assets.delvenetworks.com/player/loader.swf?channelListId=301b117890c4465c8179ede21fd92e2b',
  312. 'only_matching': True,
  313. }]
  314. _PLAYLIST_SERVICE_PATH = 'channel_list'
  315. def _real_extract(self, url):
  316. channel_list_id = self._match_id(url)
  317. channel_list = self._call_playlist_service(
  318. channel_list_id, 'getMobileChannelListById')
  319. entries = [
  320. self.url_result('limelight:channel:%s' % channel['id'], 'LimelightChannel')
  321. for channel in channel_list['channelList']]
  322. return self.playlist_result(
  323. entries, channel_list_id, channel_list['title'])