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