]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/redbulltv.py
Merge pull request #14225 from Tithen-Firion/openload-phantomjs-method
[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', entry_protocol='m3u8_native',
102             m3u8_id='hls')
103         self._sort_formats(formats)
104
105         subtitles = {}
106         for _, captions in (try_get(
107                 video, lambda x: x['attachments']['captions'],
108                 dict) or {}).items():
109             if not captions or not isinstance(captions, list):
110                 continue
111             for caption in captions:
112                 caption_url = caption.get('url')
113                 if not caption_url:
114                     continue
115                 ext = caption.get('format')
116                 if ext == 'xml':
117                     ext = 'ttml'
118                 subtitles.setdefault(caption.get('lang') or 'en', []).append({
119                     'url': caption_url,
120                     'ext': ext,
121                 })
122
123         subheading = info.get('subheading')
124         if subheading:
125             title += ' - %s' % subheading
126
127         return {
128             'id': video_id,
129             'title': title,
130             'description': info.get('long_description') or info.get(
131                 'short_description'),
132             'duration': float_or_none(video.get('duration'), scale=1000),
133             # 'timestamp': unified_timestamp(info.get('published')),
134             'series': info.get('show_title'),
135             'season_number': int_or_none(info.get('season_number')),
136             'episode_number': int_or_none(info.get('episode_number')),
137             'formats': formats,
138             'subtitles': subtitles,
139         }