]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/redbulltv.py
[redbull] improve extraction
[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)/(?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     }, {
46         'url': 'https://www.redbull.tv/film/AP-1MSKKF5T92111/in-motion',
47         'only_matching': True,
48     }]
49
50     def _real_extract(self, url):
51         video_id = self._match_id(url)
52
53         session = self._download_json(
54             'https://api-v2.redbull.tv/session', video_id,
55             note='Downloading access token', query={
56                 'build': '4.370.0',
57                 'category': 'personal_computer',
58                 'os_version': '1.0',
59                 'os_family': 'http',
60             })
61         if session.get('code') == 'error':
62             raise ExtractorError('%s said: %s' % (
63                 self.IE_NAME, session['message']))
64         auth = '%s %s' % (session.get('token_type', 'Bearer'), session['access_token'])
65
66         try:
67             info = self._download_json(
68                 'https://api-v2.redbull.tv/content/%s' % video_id,
69                 video_id, note='Downloading video information',
70                 headers={'Authorization': auth}
71             )
72         except ExtractorError as e:
73             if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
74                 error_message = self._parse_json(
75                     e.cause.read().decode(), video_id)['message']
76                 raise ExtractorError('%s said: %s' % (
77                     self.IE_NAME, error_message), expected=True)
78             raise
79
80         video = info['video_product']
81
82         title = info['title'].strip()
83
84         formats = self._extract_m3u8_formats(
85             video['url'], video_id, 'mp4', 'm3u8_native')
86         self._sort_formats(formats)
87
88         subtitles = {}
89         for _, captions in (try_get(
90                 video, lambda x: x['attachments']['captions'],
91                 dict) or {}).items():
92             if not captions or not isinstance(captions, list):
93                 continue
94             for caption in captions:
95                 caption_url = caption.get('url')
96                 if not caption_url:
97                     continue
98                 ext = caption.get('format')
99                 if ext == 'xml':
100                     ext = 'ttml'
101                 subtitles.setdefault(caption.get('lang') or 'en', []).append({
102                     'url': caption_url,
103                     'ext': ext,
104                 })
105
106         subheading = info.get('subheading')
107         if subheading:
108             title += ' - %s' % subheading
109
110         return {
111             'id': video_id,
112             'title': title,
113             'description': info.get('long_description') or info.get(
114                 'short_description'),
115             'duration': float_or_none(video.get('duration'), scale=1000),
116             # 'timestamp': unified_timestamp(info.get('published')),
117             'series': info.get('show_title'),
118             'season_number': int_or_none(info.get('season_number')),
119             'episode_number': int_or_none(info.get('episode_number')),
120             'formats': formats,
121             'subtitles': subtitles,
122         }