jwplatform.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. float_or_none,
  7. int_or_none,
  8. )
  9. class JWPlatformBaseIE(InfoExtractor):
  10. def _parse_jwplayer_data(self, jwplayer_data, video_id, require_title=True):
  11. video_data = jwplayer_data['playlist'][0]
  12. subtitles = {}
  13. for track in video_data['tracks']:
  14. if track['kind'] == 'captions':
  15. subtitles[track['label']] = [{'url': self._proto_relative_url(track['file'])}]
  16. formats = []
  17. for source in video_data['sources']:
  18. source_url = self._proto_relative_url(source['file'])
  19. source_type = source.get('type') or ''
  20. if source_type in ('application/vnd.apple.mpegurl', 'hls'):
  21. formats.extend(self._extract_m3u8_formats(
  22. source_url, video_id, 'mp4', 'm3u8_native', fatal=False))
  23. elif source_type.startswith('audio'):
  24. formats.append({
  25. 'url': source_url,
  26. 'vcodec': 'none',
  27. })
  28. else:
  29. formats.append({
  30. 'url': source_url,
  31. 'width': int_or_none(source.get('width')),
  32. 'height': int_or_none(source.get('height')),
  33. })
  34. self._sort_formats(formats)
  35. return {
  36. 'id': video_id,
  37. 'title': video_data['title'] if require_title else video_data.get('title'),
  38. 'description': video_data.get('description'),
  39. 'thumbnail': self._proto_relative_url(video_data.get('image')),
  40. 'timestamp': int_or_none(video_data.get('pubdate')),
  41. 'duration': float_or_none(jwplayer_data.get('duration')),
  42. 'subtitles': subtitles,
  43. 'formats': formats,
  44. }
  45. class JWPlatformIE(JWPlatformBaseIE):
  46. _VALID_URL = r'(?:https?://content\.jwplatform\.com/(?:feeds|players|jw6)/|jwplatform:)(?P<id>[a-zA-Z0-9]{8})'
  47. _TEST = {
  48. 'url': 'http://content.jwplatform.com/players/nPripu9l-ALJ3XQCI.js',
  49. 'md5': 'fa8899fa601eb7c83a64e9d568bdf325',
  50. 'info_dict': {
  51. 'id': 'nPripu9l',
  52. 'ext': 'mov',
  53. 'title': 'Big Buck Bunny Trailer',
  54. 'description': 'Big Buck Bunny is a short animated film by the Blender Institute. It is made using free and open source software.',
  55. 'upload_date': '20081127',
  56. 'timestamp': 1227796140,
  57. }
  58. }
  59. @staticmethod
  60. def _extract_url(webpage):
  61. mobj = re.search(
  62. r'<script[^>]+?src=["\'](?P<url>(?:https?:)?//content.jwplatform.com/players/[a-zA-Z0-9]{8})',
  63. webpage)
  64. if mobj:
  65. return mobj.group('url')
  66. def _real_extract(self, url):
  67. video_id = self._match_id(url)
  68. json_data = self._download_json('http://content.jwplatform.com/feeds/%s.json' % video_id, video_id)
  69. return self._parse_jwplayer_data(json_data, video_id)