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