jwplatform.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 JWPlatformBaseIE(InfoExtractor):
  11. def _parse_jwplayer_data(self, jwplayer_data, video_id, require_title=True, m3u8_id=None, rtmp_params=None):
  12. video_data = jwplayer_data['playlist'][0]
  13. formats = []
  14. for source in video_data['sources']:
  15. source_url = self._proto_relative_url(source['file'])
  16. source_type = source.get('type') or ''
  17. if source_type in ('application/vnd.apple.mpegurl', 'hls') or determine_ext(source_url) == 'm3u8':
  18. formats.extend(self._extract_m3u8_formats(
  19. source_url, video_id, 'mp4', 'm3u8_native', m3u8_id=m3u8_id, fatal=False))
  20. elif source_type.startswith('audio'):
  21. formats.append({
  22. 'url': source_url,
  23. 'vcodec': 'none',
  24. })
  25. else:
  26. a_format = {
  27. 'url': source_url,
  28. 'width': int_or_none(source.get('width')),
  29. 'height': int_or_none(source.get('height')),
  30. }
  31. if source_url.startswith('rtmp'):
  32. # See com/longtailvideo/jwplayer/media/RTMPMediaProvider.as
  33. # of jwplayer.flash.swf
  34. rtmp_url, prefix, play_path = re.split(
  35. r'((?:mp4|mp3|flv):)', source_url, 1)
  36. a_format.update({
  37. 'url': rtmp_url,
  38. 'ext': 'flv',
  39. 'play_path': prefix + play_path,
  40. })
  41. if rtmp_params:
  42. a_format.update(rtmp_params)
  43. formats.append(a_format)
  44. self._sort_formats(formats)
  45. subtitles = {}
  46. tracks = video_data.get('tracks')
  47. if tracks and isinstance(tracks, list):
  48. for track in tracks:
  49. if track.get('file') and track.get('kind') == 'captions':
  50. subtitles.setdefault(track.get('label') or 'en', []).append({
  51. 'url': self._proto_relative_url(track['file'])
  52. })
  53. return {
  54. 'id': video_id,
  55. 'title': video_data['title'] if require_title else video_data.get('title'),
  56. 'description': video_data.get('description'),
  57. 'thumbnail': self._proto_relative_url(video_data.get('image')),
  58. 'timestamp': int_or_none(video_data.get('pubdate')),
  59. 'duration': float_or_none(jwplayer_data.get('duration')),
  60. 'subtitles': subtitles,
  61. 'formats': formats,
  62. }
  63. class JWPlatformIE(JWPlatformBaseIE):
  64. _VALID_URL = r'(?:https?://content\.jwplatform\.com/(?:feeds|players|jw6)/|jwplatform:)(?P<id>[a-zA-Z0-9]{8})'
  65. _TEST = {
  66. 'url': 'http://content.jwplatform.com/players/nPripu9l-ALJ3XQCI.js',
  67. 'md5': 'fa8899fa601eb7c83a64e9d568bdf325',
  68. 'info_dict': {
  69. 'id': 'nPripu9l',
  70. 'ext': 'mov',
  71. 'title': 'Big Buck Bunny Trailer',
  72. 'description': 'Big Buck Bunny is a short animated film by the Blender Institute. It is made using free and open source software.',
  73. 'upload_date': '20081127',
  74. 'timestamp': 1227796140,
  75. }
  76. }
  77. @staticmethod
  78. def _extract_url(webpage):
  79. mobj = re.search(
  80. r'<script[^>]+?src=["\'](?P<url>(?:https?:)?//content.jwplatform.com/players/[a-zA-Z0-9]{8})',
  81. webpage)
  82. if mobj:
  83. return mobj.group('url')
  84. def _real_extract(self, url):
  85. video_id = self._match_id(url)
  86. json_data = self._download_json('http://content.jwplatform.com/feeds/%s.json' % video_id, video_id)
  87. return self._parse_jwplayer_data(json_data, video_id)