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