]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/redbulltv.py
[cbs] extract smpte and vtt subtitles
[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     ExtractorError,
9 )
10
11
12 class RedBullTVIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?redbull(?:\.tv|\.com/(?:[^/]+/)?tv)/video/(?P<id>AP-\w+)'
14     _TESTS = [{
15         # film
16         'url': 'https://www.redbull.tv/video/AP-1Q6XCDTAN1W11',
17         'md5': 'fb0445b98aa4394e504b413d98031d1f',
18         'info_dict': {
19             'id': 'AP-1Q6XCDTAN1W11',
20             'ext': 'mp4',
21             'title': 'ABC of... WRC - ABC of... S1E6',
22             'description': 'md5:5c7ed8f4015c8492ecf64b6ab31e7d31',
23             'duration': 1582.04,
24         },
25     }, {
26         # episode
27         'url': 'https://www.redbull.tv/video/AP-1PMHKJFCW1W11',
28         'info_dict': {
29             'id': 'AP-1PMHKJFCW1W11',
30             'ext': 'mp4',
31             'title': 'Grime - Hashtags S2E4',
32             'description': 'md5:b5f522b89b72e1e23216e5018810bb25',
33             'duration': 904.6,
34         },
35         'params': {
36             'skip_download': True,
37         },
38     }, {
39         'url': 'https://www.redbull.com/int-en/tv/video/AP-1UWHCAR9S1W11/rob-meets-sam-gaze?playlist=playlists::3f81040a-2f31-4832-8e2e-545b1d39d173',
40         'only_matching': True,
41     }]
42
43     def _real_extract(self, url):
44         video_id = self._match_id(url)
45
46         session = self._download_json(
47             'https://api.redbull.tv/v3/session', video_id,
48             note='Downloading access token', query={
49                 'category': 'personal_computer',
50                 'os_family': 'http',
51             })
52         if session.get('code') == 'error':
53             raise ExtractorError('%s said: %s' % (
54                 self.IE_NAME, session['message']))
55         token = session['token']
56
57         try:
58             video = self._download_json(
59                 'https://api.redbull.tv/v3/products/' + video_id,
60                 video_id, note='Downloading video information',
61                 headers={'Authorization': token}
62             )
63         except ExtractorError as e:
64             if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
65                 error_message = self._parse_json(
66                     e.cause.read().decode(), video_id)['error']
67                 raise ExtractorError('%s said: %s' % (
68                     self.IE_NAME, error_message), expected=True)
69             raise
70
71         title = video['title'].strip()
72
73         formats = self._extract_m3u8_formats(
74             'https://dms.redbull.tv/v3/%s/%s/playlist.m3u8' % (video_id, token),
75             video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls')
76         self._sort_formats(formats)
77
78         subtitles = {}
79         for resource in video.get('resources', []):
80             if resource.startswith('closed_caption_'):
81                 splitted_resource = resource.split('_')
82                 if splitted_resource[2]:
83                     subtitles.setdefault('en', []).append({
84                         'url': 'https://resources.redbull.tv/%s/%s' % (video_id, resource),
85                         'ext': splitted_resource[2],
86                     })
87
88         subheading = video.get('subheading')
89         if subheading:
90             title += ' - %s' % subheading
91
92         return {
93             'id': video_id,
94             'title': title,
95             'description': video.get('long_description') or video.get(
96                 'short_description'),
97             'duration': float_or_none(video.get('duration'), scale=1000),
98             'formats': formats,
99             'subtitles': subtitles,
100         }