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