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