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.

139 lines
5.8 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_urlparse
  6. from ..utils import (
  7. determine_ext,
  8. float_or_none,
  9. int_or_none,
  10. mimetype2ext,
  11. )
  12. class JWPlatformBaseIE(InfoExtractor):
  13. @staticmethod
  14. def _find_jwplayer_data(webpage):
  15. # TODO: Merge this with JWPlayer-related codes in generic.py
  16. mobj = re.search(
  17. 'jwplayer\((?P<quote>[\'"])[^\'" ]+(?P=quote)\)\.setup\((?P<options>[^)]+)\)',
  18. webpage)
  19. if mobj:
  20. return mobj.group('options')
  21. def _extract_jwplayer_data(self, webpage, video_id, *args, **kwargs):
  22. jwplayer_data = self._parse_json(
  23. self._find_jwplayer_data(webpage), video_id)
  24. return self._parse_jwplayer_data(
  25. jwplayer_data, video_id, *args, **kwargs)
  26. def _parse_jwplayer_data(self, jwplayer_data, video_id, require_title=True, m3u8_id=None, rtmp_params=None, base_url=None):
  27. # JWPlayer backward compatibility: flattened playlists
  28. # https://github.com/jwplayer/jwplayer/blob/v7.4.3/src/js/api/config.js#L81-L96
  29. if 'playlist' not in jwplayer_data:
  30. jwplayer_data = {'playlist': [jwplayer_data]}
  31. entries = []
  32. for video_data in jwplayer_data['playlist']:
  33. # JWPlayer backward compatibility: flattened sources
  34. # https://github.com/jwplayer/jwplayer/blob/v7.4.3/src/js/playlist/item.js#L29-L35
  35. if 'sources' not in video_data:
  36. video_data['sources'] = [video_data]
  37. formats = []
  38. for source in video_data['sources']:
  39. source_url = self._proto_relative_url(source['file'])
  40. if base_url:
  41. source_url = compat_urlparse.urljoin(base_url, source_url)
  42. source_type = source.get('type') or ''
  43. ext = mimetype2ext(source_type) or determine_ext(source_url)
  44. if source_type == 'hls' or ext == 'm3u8':
  45. formats.extend(self._extract_m3u8_formats(
  46. source_url, video_id, 'mp4', 'm3u8_native', m3u8_id=m3u8_id, fatal=False))
  47. # https://github.com/jwplayer/jwplayer/blob/master/src/js/providers/default.js#L67
  48. elif source_type.startswith('audio') or ext in ('oga', 'aac', 'mp3', 'mpeg', 'vorbis'):
  49. formats.append({
  50. 'url': source_url,
  51. 'vcodec': 'none',
  52. 'ext': ext,
  53. })
  54. else:
  55. a_format = {
  56. 'url': source_url,
  57. 'width': int_or_none(source.get('width')),
  58. 'height': int_or_none(source.get('height')),
  59. 'ext': ext,
  60. }
  61. if source_url.startswith('rtmp'):
  62. a_format['ext'] = 'flv',
  63. # See com/longtailvideo/jwplayer/media/RTMPMediaProvider.as
  64. # of jwplayer.flash.swf
  65. rtmp_url_parts = re.split(
  66. r'((?:mp4|mp3|flv):)', source_url, 1)
  67. if len(rtmp_url_parts) == 3:
  68. rtmp_url, prefix, play_path = rtmp_url_parts
  69. a_format.update({
  70. 'url': rtmp_url,
  71. 'play_path': prefix + play_path,
  72. })
  73. if rtmp_params:
  74. a_format.update(rtmp_params)
  75. formats.append(a_format)
  76. self._sort_formats(formats)
  77. subtitles = {}
  78. tracks = video_data.get('tracks')
  79. if tracks and isinstance(tracks, list):
  80. for track in tracks:
  81. if track.get('file') and track.get('kind') == 'captions':
  82. subtitles.setdefault(track.get('label') or 'en', []).append({
  83. 'url': self._proto_relative_url(track['file'])
  84. })
  85. entries.append({
  86. 'id': video_id,
  87. 'title': video_data['title'] if require_title else video_data.get('title'),
  88. 'description': video_data.get('description'),
  89. 'thumbnail': self._proto_relative_url(video_data.get('image')),
  90. 'timestamp': int_or_none(video_data.get('pubdate')),
  91. 'duration': float_or_none(jwplayer_data.get('duration')),
  92. 'subtitles': subtitles,
  93. 'formats': formats,
  94. })
  95. if len(entries) == 1:
  96. return entries[0]
  97. else:
  98. return self.playlist_result(entries)
  99. class JWPlatformIE(JWPlatformBaseIE):
  100. _VALID_URL = r'(?:https?://content\.jwplatform\.com/(?:feeds|players|jw6)/|jwplatform:)(?P<id>[a-zA-Z0-9]{8})'
  101. _TEST = {
  102. 'url': 'http://content.jwplatform.com/players/nPripu9l-ALJ3XQCI.js',
  103. 'md5': 'fa8899fa601eb7c83a64e9d568bdf325',
  104. 'info_dict': {
  105. 'id': 'nPripu9l',
  106. 'ext': 'mov',
  107. 'title': 'Big Buck Bunny Trailer',
  108. 'description': 'Big Buck Bunny is a short animated film by the Blender Institute. It is made using free and open source software.',
  109. 'upload_date': '20081127',
  110. 'timestamp': 1227796140,
  111. }
  112. }
  113. @staticmethod
  114. def _extract_url(webpage):
  115. mobj = re.search(
  116. r'<script[^>]+?src=["\'](?P<url>(?:https?:)?//content.jwplatform.com/players/[a-zA-Z0-9]{8})',
  117. webpage)
  118. if mobj:
  119. return mobj.group('url')
  120. def _real_extract(self, url):
  121. video_id = self._match_id(url)
  122. json_data = self._download_json('http://content.jwplatform.com/feeds/%s.json' % video_id, video_id)
  123. return self._parse_jwplayer_data(json_data, video_id)