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