]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/redbulltv.py
[redbulltv] Add support for lives and segments (closes #13486))
[youtube-dl.git] / youtube_dl / extractor / redbulltv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_HTTPError
6 from ..utils import (
7     float_or_none,
8     int_or_none,
9     try_get,
10     # unified_timestamp,
11     ExtractorError,
12 )
13
14
15 class RedBullTVIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?redbull\.tv/(?:video|film|live)/(?:AP-\w+/segment/)?(?P<id>AP-\w+)'
17     _TESTS = [{
18         # film
19         'url': 'https://www.redbull.tv/video/AP-1Q756YYX51W11/abc-of-wrc',
20         'md5': 'fb0445b98aa4394e504b413d98031d1f',
21         'info_dict': {
22             'id': 'AP-1Q756YYX51W11',
23             'ext': 'mp4',
24             'title': 'ABC of...WRC',
25             'description': 'md5:5c7ed8f4015c8492ecf64b6ab31e7d31',
26             'duration': 1582.04,
27             # 'timestamp': 1488405786,
28             # 'upload_date': '20170301',
29         },
30     }, {
31         # episode
32         'url': 'https://www.redbull.tv/video/AP-1PMT5JCWH1W11/grime?playlist=shows:shows-playall:web',
33         'info_dict': {
34             'id': 'AP-1PMT5JCWH1W11',
35             'ext': 'mp4',
36             'title': 'Grime - Hashtags S2 E4',
37             'description': 'md5:334b741c8c1ce65be057eab6773c1cf5',
38             'duration': 904.6,
39             # 'timestamp': 1487290093,
40             # 'upload_date': '20170217',
41             'series': 'Hashtags',
42             'season_number': 2,
43             'episode_number': 4,
44         },
45         'params': {
46             'skip_download': True,
47         },
48     }, {
49         # segment
50         'url': 'https://www.redbull.tv/live/AP-1R5DX49XS1W11/segment/AP-1QSAQJ6V52111/semi-finals',
51         'info_dict': {
52             'id': 'AP-1QSAQJ6V52111',
53             'ext': 'mp4',
54             'title': 'Semi Finals - Vans Park Series Pro Tour',
55             'description': 'md5:306a2783cdafa9e65e39aa62f514fd97',
56             'duration': 11791.991,
57         },
58         'params': {
59             'skip_download': True,
60         },
61     }, {
62         'url': 'https://www.redbull.tv/film/AP-1MSKKF5T92111/in-motion',
63         'only_matching': True,
64     }]
65
66     def _real_extract(self, url):
67         video_id = self._match_id(url)
68
69         session = self._download_json(
70             'https://api-v2.redbull.tv/session', video_id,
71             note='Downloading access token', query={
72                 'build': '4.370.0',
73                 'category': 'personal_computer',
74                 'os_version': '1.0',
75                 'os_family': 'http',
76             })
77         if session.get('code') == 'error':
78             raise ExtractorError('%s said: %s' % (
79                 self.IE_NAME, session['message']))
80         auth = '%s %s' % (session.get('token_type', 'Bearer'), session['access_token'])
81
82         try:
83             info = self._download_json(
84                 'https://api-v2.redbull.tv/content/%s' % video_id,
85                 video_id, note='Downloading video information',
86                 headers={'Authorization': auth}
87             )
88         except ExtractorError as e:
89             if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
90                 error_message = self._parse_json(
91                     e.cause.read().decode(), video_id)['message']
92                 raise ExtractorError('%s said: %s' % (
93                     self.IE_NAME, error_message), expected=True)
94             raise
95
96         video = info['video_product']
97
98         title = info['title'].strip()
99
100         formats = self._extract_m3u8_formats(
101             video['url'], video_id, 'mp4', 'm3u8_native')
102         self._sort_formats(formats)
103
104         subtitles = {}
105         for _, captions in (try_get(
106                 video, lambda x: x['attachments']['captions'],
107                 dict) or {}).items():
108             if not captions or not isinstance(captions, list):
109                 continue
110             for caption in captions:
111                 caption_url = caption.get('url')
112                 if not caption_url:
113                     continue
114                 ext = caption.get('format')
115                 if ext == 'xml':
116                     ext = 'ttml'
117                 subtitles.setdefault(caption.get('lang') or 'en', []).append({
118                     'url': caption_url,
119                     'ext': ext,
120                 })
121
122         subheading = info.get('subheading')
123         if subheading:
124             title += ' - %s' % subheading
125
126         return {
127             'id': video_id,
128             'title': title,
129             'description': info.get('long_description') or info.get(
130                 'short_description'),
131             'duration': float_or_none(video.get('duration'), scale=1000),
132             # 'timestamp': unified_timestamp(info.get('published')),
133             'series': info.get('show_title'),
134             'season_number': int_or_none(info.get('season_number')),
135             'episode_number': int_or_none(info.get('episode_number')),
136             'formats': formats,
137             'subtitles': subtitles,
138         }