]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/redbulltv.py
[redbulltv] Improve extraction (closes #11948, closes #3919)
[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 ..utils import (
6     float_or_none,
7     int_or_none,
8     try_get,
9     unified_timestamp,
10 )
11
12
13 class RedBullTVIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?redbull\.tv/(?:video|film)/(?P<id>AP-\w+)'
15     _TESTS = [{
16         # film
17         'url': 'https://www.redbull.tv/video/AP-1Q756YYX51W11/abc-of-wrc',
18         'md5': '78e860f631d7a846e712fab8c5fe2c38',
19         'info_dict': {
20             'id': 'AP-1Q756YYX51W11',
21             'ext': 'mp4',
22             'title': 'ABC of...WRC',
23             'description': 'md5:5c7ed8f4015c8492ecf64b6ab31e7d31',
24             'duration': 1582.04,
25             'timestamp': 1488405786,
26             'upload_date': '20170301',
27         },
28     }, {
29         # episode
30         'url': 'https://www.redbull.tv/video/AP-1PMT5JCWH1W11/grime?playlist=shows:shows-playall:web',
31         'info_dict': {
32             'id': 'AP-1PMT5JCWH1W11',
33             'ext': 'mp4',
34             'title': 'Grime - Hashtags S2 E4',
35             'description': 'md5:334b741c8c1ce65be057eab6773c1cf5',
36             'duration': 904.6,
37             'timestamp': 1487290093,
38             'upload_date': '20170217',
39             'series': 'Hashtags',
40             'season_number': 2,
41             'episode_number': 4,
42         },
43     }, {
44         'url': 'https://www.redbull.tv/film/AP-1MSKKF5T92111/in-motion',
45         'only_matching': True,
46     }]
47
48     def _real_extract(self, url):
49         video_id = self._match_id(url)
50
51         access_token = self._download_json(
52             'https://api-v2.redbull.tv/start', video_id,
53             note='Downloading access token', query={
54                 'build': '4.0.9',
55                 'category': 'smartphone',
56                 'os_version': 23,
57                 'os_family': 'android',
58             })['auth']['access_token']
59
60         info = self._download_json(
61             'https://api-v2.redbull.tv/views/%s' % video_id,
62             video_id, note='Downloading video information',
63             headers={'Authorization': 'Bearer ' + access_token}
64         )['blocks'][0]['top'][0]
65
66         video = info['video_product']
67
68         title = info['title'].strip()
69         m3u8_url = video['url']
70
71         formats = self._extract_m3u8_formats(
72             m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
73             m3u8_id='hls')
74
75         subtitles = {}
76         for _, captions in (try_get(
77                 video, lambda x: x['attachments']['captions'],
78                 dict) or {}).items():
79             if not captions or not isinstance(captions, list):
80                 continue
81             for caption in captions:
82                 caption_url = caption.get('url')
83                 if not caption_url:
84                     continue
85                 subtitles.setdefault(caption.get('lang') or 'en', []).append({
86                     'url': caption_url,
87                     'ext': caption.get('format'),
88                 })
89
90         subheading = info.get('subheading')
91         if subheading:
92             title += ' - %s' % subheading
93
94         return {
95             'id': video_id,
96             'title': title,
97             'description': info.get('long_description') or info.get(
98                 'short_description'),
99             'duration': float_or_none(video.get('duration'), scale=1000),
100             'timestamp': unified_timestamp(info.get('published')),
101             'series': info.get('show_title'),
102             'season_number': int_or_none(info.get('season_number')),
103             'episode_number': int_or_none(info.get('episode_number')),
104             'formats': formats,
105             'subtitles': subtitles,
106         }